Debian Codegraph @

localservices/cloudapi/service_machines.go

raw symbols SCIP
1 package cloudapi
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7 "time"
8
9 "github.com/joyent/gosdc/cloudapi"
10 "github.com/joyent/gosdc/localservices"
11 )
12
13 // ListMachines returns a list of machines in the double
14 func (c *CloudAPI) ListMachines(filters map[string]string) ([]*cloudapi.Machine, error) {
15 if err := c.ProcessFunctionHook(c, filters); err != nil {
16 return nil, err
17 }
18
19 availableMachines := c.machines
20
21 if filters != nil {
22 for k, f := range filters {
23 // check if valid filter
24 if contains(machinesFilters, k) {
25 machines := []*machine{}
26 // filter from availableMachines and add to machines
27 for _, m := range availableMachines {
28 if k == "name" && m.Name == f {
29 machines = append(machines, m)
30 } else if k == "type" && m.Type == f {
31 machines = append(machines, m)
32 } else if k == "state" && m.State == f {
33 machines = append(machines, m)
34 } else if k == "image" && m.Image == f {
35 machines = append(machines, m)
36 } else if k == "memory" {
37 i, err := strconv.Atoi(f)
38 if err == nil && m.Memory == i {
39 machines = append(machines, m)
40 }
41 } else if strings.HasPrefix(k, "tags.") {
42 for t, v := range m.Tags {
43 if t == k[strings.Index(k, ".")+1:] && v == f {
44 machines = append(machines, m)
45 }
46 }
47 }
48 }
49 availableMachines = machines
50 }
51 }
52 }
53
54 out := make([]*cloudapi.Machine, len(availableMachines))
55 for i, machine := range availableMachines {
56 out[i] = &machine.Machine
57 }
58
59 return out, nil
60 }
61
62 // CountMachines returns a count of machines the double knows about
63 func (c *CloudAPI) CountMachines() (int, error) {
64 if err := c.ProcessFunctionHook(c); err != nil {
65 return 0, err
66 }
67
68 return len(c.machines), nil
69 }
70
71 func (c *CloudAPI) getMachineWrapper(machineID string) (*machine, error) {
72 if err := c.ProcessFunctionHook(c, machineID); err != nil {
73 return nil, err
74 }
75
76 for _, machine := range c.machines {
77 if machine.Id == machineID {
78 return machine, nil
79 }
80 }
81
82 return nil, fmt.Errorf("Machine %s not found", machineID)
83 }
84
85 // GetMachine gets a single machine by ID from the double
86 func (c *CloudAPI) GetMachine(machineID string) (*cloudapi.Machine, error) {
87 wrapper, err := c.getMachineWrapper(machineID)
88 if err != nil {
89 return nil, err
90 }
91
92 return &wrapper.Machine, nil
93 }
94
95 // CreateMachine creates a new machine in the double. It will be running immediately.
96 func (c *CloudAPI) CreateMachine(name, pkg, image string, networks []string, metadata, tags map[string]string) (*cloudapi.Machine, error) {
97 if err := c.ProcessFunctionHook(c, name, pkg, image); err != nil {
98 return nil, err
99 }
100
101 machineID, err := localservices.NewUUID()
102 if err != nil {
103 return nil, err
104 }
105
106 mPkg, err := c.GetPackage(pkg)
107 if err != nil {
108 return nil, err
109 }
110
111 mImg, err := c.GetImage(image)
112 if err != nil {
113 return nil, err
114 }
115
116 mNetworks := []string{}
117 for _, network := range networks {
118 mNetwork, err := c.GetNetwork(network)
119 if err != nil {
120 return nil, err
121 }
122
123 mNetworks = append(mNetworks, mNetwork.Id)
124 }
125
126 publicIP := generatePublicIPAddress()
127
128 newMachine := cloudapi.Machine{
129 Id: machineID,
130 Name: name,
131 Type: mImg.Type,
132 State: "running",
133 Memory: mPkg.Memory,
134 Disk: mPkg.Disk,
135 IPs: []string{publicIP, generatePrivateIPAddress()},
136 Created: time.Now().Format("2013-11-26T19:47:13.448Z"),
137 Package: pkg,
138 Image: image,
139 Metadata: metadata,
140 Tags: tags,
141 PrimaryIP: publicIP,
142 Networks: mNetworks,
143 }
144
145 nics := map[string]*cloudapi.NIC{}
146 nicNetworks := map[string]string{}
147 for i, network := range mNetworks {
148 mac, err := localservices.NewMAC()
149 if err != nil {
150 return nil, err
151 }
152
153 nics[mac] = &cloudapi.NIC{
154 IP: fmt.Sprintf("10.88.88.%d", i),
155 MAC: mac,
156 Primary: i == 0,
157 Netmask: "255.255.255.0",
158 Gateway: "10.88.88.2",
159 State: cloudapi.NICStateRunning,
160 Network: network,
161 }
162 nicNetworks[mac] = network
163 }
164
165 c.machines = append(c.machines, &machine{newMachine, nics, nicNetworks})
166
167 return &newMachine, nil
168 }
169
170 // StopMachine changes a machine's status to "stopped"
171 func (c *CloudAPI) StopMachine(machineID string) error {
172 if err := c.ProcessFunctionHook(c, machineID); err != nil {
173 return err
174 }
175
176 for _, machine := range c.machines {
177 if machine.Id == machineID {
178 machine.State = "stopped"
179 machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z")
180 return nil
181 }
182 }
183
184 return fmt.Errorf("Machine %s not found", machineID)
185 }
186
187 // StartMachine changes a machine's state to "running"
188 func (c *CloudAPI) StartMachine(machineID string) error {
189 if err := c.ProcessFunctionHook(c, machineID); err != nil {
190 return err
191 }
192
193 for _, machine := range c.machines {
194 if machine.Id == machineID {
195 machine.State = "running"
196 machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z")
197 return nil
198 }
199 }
200
201 return fmt.Errorf("Machine %s not found", machineID)
202 }
203
204 // RebootMachine changes a machine's state to "running" and updates Updated
205 func (c *CloudAPI) RebootMachine(machineID string) error {
206 if err := c.ProcessFunctionHook(c, machineID); err != nil {
207 return err
208 }
209
210 for _, machine := range c.machines {
211 if machine.Id == machineID {
212 machine.State = "running"
213 machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z")
214 return nil
215 }
216 }
217
218 return fmt.Errorf("Machine %s not found", machineID)
219 }
220
221 // ResizeMachine changes a machine's package to a new size. Unlike the real API,
222 // this method lets you downsize machines.
223 func (c *CloudAPI) ResizeMachine(machineID, packageName string) error {
224 if err := c.ProcessFunctionHook(c, machineID, packageName); err != nil {
225 return err
226 }
227
228 mPkg, err := c.GetPackage(packageName)
229 if err != nil {
230 return err
231 }
232
233 for _, machine := range c.machines {
234 if machine.Id == machineID {
235 machine.Package = packageName
236 machine.Memory = mPkg.Memory
237 machine.Disk = mPkg.Disk
238 machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z")
239 return nil
240 }
241 }
242
243 return fmt.Errorf("Machine %s not found", machineID)
244 }
245
246 // RenameMachine changes a machine's name
247 func (c *CloudAPI) RenameMachine(machineID, newName string) error {
248 if err := c.ProcessFunctionHook(c, machineID, newName); err != nil {
249 return err
250 }
251
252 for _, machine := range c.machines {
253 if machine.Id == machineID {
254 machine.Name = newName
255 machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z")
256 return nil
257 }
258 }
259
260 return fmt.Errorf("Machine %s not found", machineID)
261 }
262
263 // ListMachineFirewallRules returns a list of firewall rules that apply to the
264 // given machine
265 func (c *CloudAPI) ListMachineFirewallRules(machineID string) ([]*cloudapi.FirewallRule, error) {
266 if err := c.ProcessFunctionHook(c, machineID); err != nil {
267 return nil, err
268 }
269
270 fwRules := []*cloudapi.FirewallRule{}
271 for _, r := range c.firewallRules {
272 vm := "vm " + machineID
273 if strings.Contains(r.Rule, vm) {
274 fwRules = append(fwRules, r)
275 }
276 }
277
278 return fwRules, nil
279 }
280
281 // EnableFirewallMachine enables the firewall for the given machine
282 func (c *CloudAPI) EnableFirewallMachine(machineID string) error {
283 if err := c.ProcessFunctionHook(c, machineID); err != nil {
284 return err
285 }
286
287 machine, err := c.GetMachine(machineID)
288 if err != nil {
289 return err
290 }
291
292 machine.FirewallEnabled = true
293
294 return nil
295 }
296
297 // DisableFirewallMachine disables the firewall for the given machine
298 func (c *CloudAPI) DisableFirewallMachine(machineID string) error {
299 if err := c.ProcessFunctionHook(c, machineID); err != nil {
300 return err
301 }
302
303 machine, err := c.GetMachine(machineID)
304 if err != nil {
305 return err
306 }
307
308 machine.FirewallEnabled = false
309
310 return nil
311 }
312
313 // DeleteMachine deletes the given machine from the double
314 func (c *CloudAPI) DeleteMachine(machineID string) error {
315 if err := c.ProcessFunctionHook(c, machineID); err != nil {
316 return err
317 }
318
319 for i, machine := range c.machines {
320 if machine.Id == machineID {
321 if machine.State == "stopped" {
322 c.machines = append(c.machines[:i], c.machines[i+1:]...)
323 return nil
324 }
325
326 return fmt.Errorf("Cannot Delete machine %s, machine is not stopped.", machineID)
327 }
328 }
329
330 return fmt.Errorf("Machine %s not found", machineID)
331 }