| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
package cloudapi |
| 10 |
|
| 11 |
import ( |
| 12 |
"fmt" |
| 13 |
"math/rand" |
| 14 |
"net/url" |
| 15 |
"strings" |
| 16 |
"time" |
| 17 |
|
| 18 |
"github.com/joyent/gosdc/cloudapi" |
| 19 |
"github.com/joyent/gosdc/localservices" |
| 20 |
) |
| 21 |
|
| 22 |
var ( |
| 23 |
separator = "/" |
| 24 |
packagesFilters = []string{"name", "memory", "disk", "swap", "version", "vcpus", "group"} |
| 25 |
imagesFilters = []string{"name", "os", "version", "public", "state", "owner", "type"} |
| 26 |
machinesFilters = []string{"type", "name", "image", "state", "memory", "tombstone", "limit", "offset", "credentials"} |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
type CloudAPI struct { |
| 31 |
localservices.ServiceInstance |
| 32 |
keys []cloudapi.Key |
| 33 |
packages []cloudapi.Package |
| 34 |
images []cloudapi.Image |
| 35 |
machines []*machine |
| 36 |
snapshots map[string][]cloudapi.Snapshot |
| 37 |
firewallRules []*cloudapi.FirewallRule |
| 38 |
networks []cloudapi.Network |
| 39 |
fabricVLANs map[int16]*fabricVLAN |
| 40 |
} |
| 41 |
|
| 42 |
type machine struct { |
| 43 |
cloudapi.Machine |
| 44 |
NICs map[string]*cloudapi.NIC `json:"-"` |
| 45 |
NetworkNICs map[string]string `json:"-"` |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
type fabricVLAN struct { |
| 50 |
cloudapi.FabricVLAN |
| 51 |
Networks map[string]*cloudapi.FabricNetwork `json:"-"` |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
func New(serviceURL, userAccount string) *CloudAPI { |
| 56 |
URL, err := url.Parse(serviceURL) |
| 57 |
if err != nil { |
| 58 |
panic(err) |
| 59 |
} |
| 60 |
hostname := URL.Host |
| 61 |
if !strings.HasSuffix(hostname, separator) { |
| 62 |
hostname += separator |
| 63 |
} |
| 64 |
|
| 65 |
var ( |
| 66 |
keys []cloudapi.Key |
| 67 |
machines []*machine |
| 68 |
snapshots = map[string][]cloudapi.Snapshot{} |
| 69 |
firewallRules []*cloudapi.FirewallRule |
| 70 |
fabricVLANs = map[int16]*fabricVLAN{} |
| 71 |
) |
| 72 |
|
| 73 |
cloudapiService := &CloudAPI{ |
| 74 |
keys: keys, |
| 75 |
packages: initPackages(), |
| 76 |
images: initImages(), |
| 77 |
machines: machines, |
| 78 |
snapshots: snapshots, |
| 79 |
firewallRules: firewallRules, |
| 80 |
fabricVLANs: fabricVLANs, |
| 81 |
networks: []cloudapi.Network{ |
| 82 |
{Id: "123abc4d-0011-aabb-2233-ccdd4455", Name: "Test-Joyent-Public", Public: true}, |
| 83 |
{Id: "456def0a-33ff-7f8e-9a0b-33bb44cc", Name: "Test-Joyent-Private", Public: false}, |
| 84 |
}, |
| 85 |
ServiceInstance: localservices.ServiceInstance{ |
| 86 |
Scheme: URL.Scheme, |
| 87 |
Hostname: hostname, |
| 88 |
UserAccount: userAccount, |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
return cloudapiService |
| 93 |
} |
| 94 |
|
| 95 |
func initPackages() []cloudapi.Package { |
| 96 |
return []cloudapi.Package{ |
| 97 |
{ |
| 98 |
Name: "Micro", |
| 99 |
Memory: 512, |
| 100 |
Disk: 8192, |
| 101 |
Swap: 1024, |
| 102 |
VCPUs: 1, |
| 103 |
Default: false, |
| 104 |
Id: "12345678-aaaa-bbbb-cccc-000000000000", |
| 105 |
Version: "1.0.0", |
| 106 |
}, |
| 107 |
{ |
| 108 |
Name: "Small", |
| 109 |
Memory: 1024, |
| 110 |
Disk: 16384, |
| 111 |
Swap: 2048, |
| 112 |
VCPUs: 1, |
| 113 |
Default: true, |
| 114 |
Id: "11223344-1212-abab-3434-aabbccddeeff", |
| 115 |
Version: "1.0.2", |
| 116 |
}, |
| 117 |
{ |
| 118 |
Name: "Medium", |
| 119 |
Memory: 2048, |
| 120 |
Disk: 32768, |
| 121 |
Swap: 4096, |
| 122 |
VCPUs: 2, |
| 123 |
Default: false, |
| 124 |
Id: "aabbccdd-abcd-abcd-abcd-112233445566", |
| 125 |
Version: "1.0.4", |
| 126 |
}, |
| 127 |
{ |
| 128 |
Name: "Large", |
| 129 |
Memory: 4096, |
| 130 |
Disk: 65536, |
| 131 |
Swap: 16384, |
| 132 |
VCPUs: 4, |
| 133 |
Default: false, |
| 134 |
Id: "00998877-dddd-eeee-ffff-111111111111", |
| 135 |
Version: "1.0.1", |
| 136 |
}, |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
func initImages() []cloudapi.Image { |
| 141 |
return []cloudapi.Image{ |
| 142 |
{ |
| 143 |
Id: "12345678-a1a1-b2b2-c3c3-098765432100", |
| 144 |
Name: "SmartOS Std", |
| 145 |
OS: "smartos", |
| 146 |
Version: "13.3.1", |
| 147 |
Type: "smartmachine", |
| 148 |
Description: "Test SmartOS image (32 bit)", |
| 149 |
Homepage: "http://test.joyent.com/Standard_Instance", |
| 150 |
PublishedAt: "2014-01-08T17:42:31Z", |
| 151 |
Public: true, |
| 152 |
State: "active", |
| 153 |
}, |
| 154 |
{ |
| 155 |
Id: "12345678-b1b1-a4a4-d8d8-111111999999", |
| 156 |
Name: "standard32", |
| 157 |
OS: "smartos", |
| 158 |
Version: "13.3.1", |
| 159 |
Type: "smartmachine", |
| 160 |
Description: "Test SmartOS image (64 bit)", |
| 161 |
Homepage: "http://test.joyent.com/Standard_Instance", |
| 162 |
PublishedAt: "2014-01-08T17:43:16Z", |
| 163 |
Public: true, |
| 164 |
State: "active", |
| 165 |
}, |
| 166 |
{ |
| 167 |
Id: "a1b2c3d4-0011-2233-4455-0f1e2d3c4b5a", |
| 168 |
Name: "centos6.4", |
| 169 |
OS: "linux", |
| 170 |
Version: "2.4.1", |
| 171 |
Type: "virtualmachine", |
| 172 |
Description: "Test CentOS 6.4 image (64 bit)", |
| 173 |
PublishedAt: "2014-01-02T10:58:31Z", |
| 174 |
Public: true, |
| 175 |
State: "active", |
| 176 |
}, |
| 177 |
{ |
| 178 |
Id: "11223344-0a0a-ff99-11bb-0a1b2c3d4e5f", |
| 179 |
Name: "ubuntu12.04", |
| 180 |
OS: "linux", |
| 181 |
Version: "2.3.1", |
| 182 |
Type: "virtualmachine", |
| 183 |
Description: "Test Ubuntu 12.04 image (64 bit)", |
| 184 |
PublishedAt: "2014-01-20T16:12:31Z", |
| 185 |
Public: true, |
| 186 |
State: "active", |
| 187 |
}, |
| 188 |
{ |
| 189 |
Id: "11223344-0a0a-ee88-22ab-00aa11bb22cc", |
| 190 |
Name: "ubuntu12.10", |
| 191 |
OS: "linux", |
| 192 |
Version: "2.3.2", |
| 193 |
Type: "virtualmachine", |
| 194 |
Description: "Test Ubuntu 12.10 image (64 bit)", |
| 195 |
PublishedAt: "2014-01-20T16:12:31Z", |
| 196 |
Public: true, |
| 197 |
State: "active", |
| 198 |
}, |
| 199 |
{ |
| 200 |
Id: "11223344-0a0a-dd77-33cd-abcd1234e5f6", |
| 201 |
Name: "ubuntu13.04", |
| 202 |
OS: "linux", |
| 203 |
Version: "2.2.8", |
| 204 |
Type: "virtualmachine", |
| 205 |
Description: "Test Ubuntu 13.04 image (64 bit)", |
| 206 |
PublishedAt: "2014-01-20T16:12:31Z", |
| 207 |
Public: true, |
| 208 |
State: "active", |
| 209 |
}, |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func generatePublicIPAddress() string { |
| 214 |
r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 215 |
return fmt.Sprintf("32.151.%d.%d", r.Intn(255), r.Intn(255)) |
| 216 |
} |
| 217 |
|
| 218 |
func generatePrivateIPAddress() string { |
| 219 |
r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 220 |
return fmt.Sprintf("10.201.%d.%d", r.Intn(255), r.Intn(255)) |
| 221 |
} |
| 222 |
|
| 223 |
func contains(list []string, elem string) bool { |
| 224 |
for _, t := range list { |
| 225 |
if t == elem { |
| 226 |
return true |
| 227 |
} |
| 228 |
} |
| 229 |
return false |
| 230 |
} |