Debian Codegraph @

vapp.go

raw symbols SCIP
1 /*
2 * Copyright 2014 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
3 */
4
5 package govcd
6
7 import (
8 "bytes"
9 "encoding/xml"
10 "fmt"
11 "log"
12 "net/url"
13 "os"
14 "strconv"
15
16 types "github.com/hmrc/vmware-govcd/types/v56"
17 )
18
19 type VApp struct {
20 VApp *types.VApp
21 c *Client
22 }
23
24 func NewVApp(c *Client) *VApp {
25 return &VApp{
26 VApp: new(types.VApp),
27 c: c,
28 }
29 }
30
31 func (v *VApp) Refresh() error {
32
33 if v.VApp.HREF == "" {
34 return fmt.Errorf("cannot refresh, Object is empty")
35 }
36
37 u, _ := url.ParseRequestURI(v.VApp.HREF)
38
39 req := v.c.NewRequest(map[string]string{}, "GET", *u, nil)
40
41 resp, err := checkResp(v.c.Http.Do(req))
42 if err != nil {
43 return fmt.Errorf("error retrieving task: %s", err)
44 }
45
46 // Empty struct before a new unmarshal, otherwise we end up with duplicate
47 // elements in slices.
48 v.VApp = &types.VApp{}
49
50 if err = decodeBody(resp, v.VApp); err != nil {
51 return fmt.Errorf("error decoding task response: %s", err)
52 }
53
54 // The request was successful
55 return nil
56 }
57
58 func (v *VApp) ComposeVApp(orgvdcnetwork OrgVDCNetwork, vapptemplate VAppTemplate, name string, description string) (Task, error) {
59
60 if vapptemplate.VAppTemplate.Children == nil || orgvdcnetwork.OrgVDCNetwork == nil {
61 return Task{}, fmt.Errorf("can't compose a new vApp, objects passed are not valid")
62 }
63
64 // Build request XML
65 vcomp := &types.ComposeVAppParams{
66 Ovf: "http://schemas.dmtf.org/ovf/envelope/1",
67 Xsi: "http://www.w3.org/2001/XMLSchema-instance",
68 Xmlns: "http://www.vmware.com/vcloud/v1.5",
69 Deploy: false,
70 Name: name,
71 PowerOn: false,
72 Description: description,
73 InstantiationParams: &types.InstantiationParams{
74 NetworkConfigSection: &types.NetworkConfigSection{
75 Info: "Configuration parameters for logical networks",
76 NetworkConfig: &types.VAppNetworkConfiguration{
77 NetworkName: orgvdcnetwork.OrgVDCNetwork.Name,
78 Configuration: &types.NetworkConfiguration{
79 FenceMode: "bridged",
80 ParentNetwork: &types.Reference{
81 HREF: orgvdcnetwork.OrgVDCNetwork.HREF,
82 Name: orgvdcnetwork.OrgVDCNetwork.Name,
83 Type: orgvdcnetwork.OrgVDCNetwork.Type,
84 },
85 },
86 },
87 },
88 },
89 SourcedItem: &types.SourcedCompositionItemParam{
90 Source: &types.Reference{
91 HREF: vapptemplate.VAppTemplate.Children.VM[0].HREF,
92 Name: vapptemplate.VAppTemplate.Children.VM[0].Name,
93 },
94 InstantiationParams: &types.InstantiationParams{
95 NetworkConnectionSection: &types.NetworkConnectionSection{
96 Type: vapptemplate.VAppTemplate.Children.VM[0].NetworkConnectionSection.Type,
97 HREF: vapptemplate.VAppTemplate.Children.VM[0].NetworkConnectionSection.HREF,
98 Info: "Network config for sourced item",
99 PrimaryNetworkConnectionIndex: vapptemplate.VAppTemplate.Children.VM[0].NetworkConnectionSection.PrimaryNetworkConnectionIndex,
100 NetworkConnection: &types.NetworkConnection{
101 Network: orgvdcnetwork.OrgVDCNetwork.Name,
102 NetworkConnectionIndex: vapptemplate.VAppTemplate.Children.VM[0].NetworkConnectionSection.NetworkConnection.NetworkConnectionIndex,
103 IsConnected: true,
104 IPAddressAllocationMode: "POOL",
105 },
106 },
107 },
108 NetworkAssignment: &types.NetworkAssignment{
109 InnerNetwork: orgvdcnetwork.OrgVDCNetwork.Name,
110 ContainerNetwork: orgvdcnetwork.OrgVDCNetwork.Name,
111 },
112 },
113 }
114
115 output, err := xml.MarshalIndent(vcomp, " ", " ")
116 if err != nil {
117 return Task{}, fmt.Errorf("error marshaling vapp compose: %s", err)
118 }
119
120 debug := os.Getenv("GOVCLOUDAIR_DEBUG")
121
122 if debug == "true" {
123 fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
124 }
125
126 b := bytes.NewBufferString(xml.Header + string(output))
127
128 s := v.c.VCDVDCHREF
129 s.Path += "/action/composeVApp"
130
131 req := v.c.NewRequest(map[string]string{}, "POST", s, b)
132
133 req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.composeVAppParams+xml")
134
135 resp, err := checkResp(v.c.Http.Do(req))
136 if err != nil {
137 return Task{}, fmt.Errorf("error instantiating a new vApp: %s", err)
138 }
139
140 if err = decodeBody(resp, v.VApp); err != nil {
141 return Task{}, fmt.Errorf("error decoding vApp response: %s", err)
142 }
143
144 task := NewTask(v.c)
145 task.Task = v.VApp.Tasks.Task[0]
146
147 // The request was successful
148 return *task, nil
149
150 }
151
152 func (v *VApp) PowerOn() (Task, error) {
153
154 s, _ := url.ParseRequestURI(v.VApp.HREF)
155 s.Path += "/power/action/powerOn"
156
157 req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
158
159 resp, err := checkResp(v.c.Http.Do(req))
160 if err != nil {
161 return Task{}, fmt.Errorf("error powering on vApp: %s", err)
162 }
163
164 task := NewTask(v.c)
165
166 if err = decodeBody(resp, task.Task); err != nil {
167 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
168 }
169
170 // The request was successful
171 return *task, nil
172
173 }
174
175 func (v *VApp) PowerOff() (Task, error) {
176
177 s, _ := url.ParseRequestURI(v.VApp.HREF)
178 s.Path += "/power/action/powerOff"
179
180 req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
181
182 resp, err := checkResp(v.c.Http.Do(req))
183 if err != nil {
184 return Task{}, fmt.Errorf("error powering off vApp: %s", err)
185 }
186
187 task := NewTask(v.c)
188
189 if err = decodeBody(resp, task.Task); err != nil {
190 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
191 }
192
193 // The request was successful
194 return *task, nil
195
196 }
197
198 func (v *VApp) Reboot() (Task, error) {
199
200 s, _ := url.ParseRequestURI(v.VApp.HREF)
201 s.Path += "/power/action/reboot"
202
203 req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
204
205 resp, err := checkResp(v.c.Http.Do(req))
206 if err != nil {
207 return Task{}, fmt.Errorf("error rebooting vApp: %s", err)
208 }
209
210 task := NewTask(v.c)
211
212 if err = decodeBody(resp, task.Task); err != nil {
213 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
214 }
215
216 // The request was successful
217 return *task, nil
218
219 }
220
221 func (v *VApp) Reset() (Task, error) {
222
223 s, _ := url.ParseRequestURI(v.VApp.HREF)
224 s.Path += "/power/action/reset"
225
226 req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
227
228 resp, err := checkResp(v.c.Http.Do(req))
229 if err != nil {
230 return Task{}, fmt.Errorf("error resetting vApp: %s", err)
231 }
232
233 task := NewTask(v.c)
234
235 if err = decodeBody(resp, task.Task); err != nil {
236 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
237 }
238
239 // The request was successful
240 return *task, nil
241
242 }
243
244 func (v *VApp) Suspend() (Task, error) {
245
246 s, _ := url.ParseRequestURI(v.VApp.HREF)
247 s.Path += "/power/action/suspend"
248
249 req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
250
251 resp, err := checkResp(v.c.Http.Do(req))
252 if err != nil {
253 return Task{}, fmt.Errorf("error suspending vApp: %s", err)
254 }
255
256 task := NewTask(v.c)
257
258 if err = decodeBody(resp, task.Task); err != nil {
259 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
260 }
261
262 // The request was successful
263 return *task, nil
264
265 }
266
267 func (v *VApp) Shutdown() (Task, error) {
268
269 s, _ := url.ParseRequestURI(v.VApp.HREF)
270 s.Path += "/power/action/shutdown"
271
272 req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
273
274 resp, err := checkResp(v.c.Http.Do(req))
275 if err != nil {
276 return Task{}, fmt.Errorf("error shutting down vApp: %s", err)
277 }
278
279 task := NewTask(v.c)
280
281 if err = decodeBody(resp, task.Task); err != nil {
282 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
283 }
284
285 // The request was successful
286 return *task, nil
287
288 }
289
290 func (v *VApp) Undeploy() (Task, error) {
291
292 vu := &types.UndeployVAppParams{
293 Xmlns: "http://www.vmware.com/vcloud/v1.5",
294 UndeployPowerAction: "powerOff",
295 }
296
297 output, err := xml.MarshalIndent(vu, " ", " ")
298 if err != nil {
299 fmt.Printf("error: %v\n", err)
300 }
301
302 debug := os.Getenv("GOVCLOUDAIR_DEBUG")
303
304 if debug == "true" {
305 fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
306 }
307
308 b := bytes.NewBufferString(xml.Header + string(output))
309
310 s, _ := url.ParseRequestURI(v.VApp.HREF)
311 s.Path += "/action/undeploy"
312
313 req := v.c.NewRequest(map[string]string{}, "POST", *s, b)
314
315 req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.undeployVAppParams+xml")
316
317 resp, err := checkResp(v.c.Http.Do(req))
318 if err != nil {
319 return Task{}, fmt.Errorf("error undeploy vApp: %s", err)
320 }
321
322 task := NewTask(v.c)
323
324 if err = decodeBody(resp, task.Task); err != nil {
325 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
326 }
327
328 // The request was successful
329 return *task, nil
330
331 }
332
333 func (v *VApp) Deploy() (Task, error) {
334
335 vu := &types.DeployVAppParams{
336 Xmlns: "http://www.vmware.com/vcloud/v1.5",
337 PowerOn: false,
338 }
339
340 output, err := xml.MarshalIndent(vu, " ", " ")
341 if err != nil {
342 fmt.Printf("error: %v\n", err)
343 }
344
345 debug := os.Getenv("GOVCLOUDAIR_DEBUG")
346
347 if debug == "true" {
348 fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
349 }
350
351 b := bytes.NewBufferString(xml.Header + string(output))
352
353 s, _ := url.ParseRequestURI(v.VApp.HREF)
354 s.Path += "/action/deploy"
355
356 req := v.c.NewRequest(map[string]string{}, "POST", *s, b)
357
358 req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.deployVAppParams+xml")
359
360 resp, err := checkResp(v.c.Http.Do(req))
361 if err != nil {
362 return Task{}, fmt.Errorf("error undeploy vApp: %s", err)
363 }
364
365 task := NewTask(v.c)
366
367 if err = decodeBody(resp, task.Task); err != nil {
368 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
369 }
370
371 // The request was successful
372 return *task, nil
373
374 }
375
376 func (v *VApp) Delete() (Task, error) {
377
378 s, _ := url.ParseRequestURI(v.VApp.HREF)
379
380 req := v.c.NewRequest(map[string]string{}, "DELETE", *s, nil)
381
382 resp, err := checkResp(v.c.Http.Do(req))
383 if err != nil {
384 return Task{}, fmt.Errorf("error deleting vApp: %s", err)
385 }
386
387 task := NewTask(v.c)
388
389 if err = decodeBody(resp, task.Task); err != nil {
390 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
391 }
392
393 // The request was successful
394 return *task, nil
395
396 }
397
398 func (v *VApp) RunCustomizationScript(computername, script string) (Task, error) {
399
400 err := v.Refresh()
401 if err != nil {
402 return Task{}, fmt.Errorf("error refreshing vapp before running customization: %v", err)
403 }
404
405 // Check if VApp Children is populated
406 if v.VApp.Children == nil {
407 return Task{}, fmt.Errorf("vApp doesn't contain any children, aborting customization")
408 }
409
410 vu := &types.GuestCustomizationSection{
411 Ovf: "http://schemas.dmtf.org/ovf/envelope/1",
412 Xsi: "http://www.w3.org/2001/XMLSchema-instance",
413 Xmlns: "http://www.vmware.com/vcloud/v1.5",
414
415 HREF: v.VApp.Children.VM[0].HREF,
416 Type: "application/vnd.vmware.vcloud.guestCustomizationSection+xml",
417 Info: "Specifies Guest OS Customization Settings",
418 Enabled: true,
419 ComputerName: computername,
420 CustomizationScript: script,
421 }
422
423 output, err := xml.MarshalIndent(vu, " ", " ")
424 if err != nil {
425 fmt.Printf("error: %v\n", err)
426 }
427
428 log.Printf("[DEBUG] VCD Client configuration: %s", output)
429
430 debug := os.Getenv("GOVCLOUDAIR_DEBUG")
431
432 if debug == "true" {
433 fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
434 }
435
436 b := bytes.NewBufferString(xml.Header + string(output))
437
438 s, _ := url.ParseRequestURI(v.VApp.Children.VM[0].HREF)
439 s.Path += "/guestCustomizationSection/"
440
441 req := v.c.NewRequest(map[string]string{}, "PUT", *s, b)
442
443 req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.guestCustomizationSection+xml")
444
445 resp, err := checkResp(v.c.Http.Do(req))
446 if err != nil {
447 return Task{}, fmt.Errorf("error customizing VM: %s", err)
448 }
449
450 task := NewTask(v.c)
451
452 if err = decodeBody(resp, task.Task); err != nil {
453 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
454 }
455
456 // The request was successful
457 return *task, nil
458
459 }
460
461 func (v *VApp) GetStatus() (string, error) {
462 err := v.Refresh()
463 if err != nil {
464 return "", fmt.Errorf("error refreshing vapp: %v", err)
465 }
466 return types.VAppStatuses[v.VApp.Status], nil
467 }
468
469 func (v *VApp) ChangeCPUcount(size int) (Task, error) {
470
471 err := v.Refresh()
472 if err != nil {
473 return Task{}, fmt.Errorf("error refreshing vapp before running customization: %v", err)
474 }
475
476 // Check if VApp Children is populated
477 if v.VApp.Children == nil {
478 return Task{}, fmt.Errorf("vApp doesn't contain any children, aborting customization")
479 }
480
481 newcpu := &types.OVFItem{
482 XmlnsRasd: "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
483 XmlnsVCloud: "http://www.vmware.com/vcloud/v1.5",
484 XmlnsXsi: "http://www.w3.org/2001/XMLSchema-instance",
485 VCloudHREF: v.VApp.Children.VM[0].HREF + "/virtualHardwareSection/cpu",
486 VCloudType: "application/vnd.vmware.vcloud.rasdItem+xml",
487 AllocationUnits: "hertz * 10^6",
488 Description: "Number of Virtual CPUs",
489 ElementName: strconv.Itoa(size) + " virtual CPU(s)",
490 InstanceID: 4,
491 Reservation: 0,
492 ResourceType: 3,
493 VirtualQuantity: size,
494 Weight: 0,
495 Link: &types.Link{
496 HREF: v.VApp.Children.VM[0].HREF + "/virtualHardwareSection/cpu",
497 Rel: "edit",
498 Type: "application/vnd.vmware.vcloud.rasdItem+xml",
499 },
500 }
501
502 output, err := xml.MarshalIndent(newcpu, " ", " ")
503 if err != nil {
504 fmt.Printf("error: %v\n", err)
505 }
506
507 debug := os.Getenv("GOVCLOUDAIR_DEBUG")
508
509 if debug == "true" {
510 fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
511 }
512
513 b := bytes.NewBufferString(xml.Header + string(output))
514
515 s, _ := url.ParseRequestURI(v.VApp.Children.VM[0].HREF)
516 s.Path += "/virtualHardwareSection/cpu"
517
518 req := v.c.NewRequest(map[string]string{}, "PUT", *s, b)
519
520 req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.rasdItem+xml")
521
522 resp, err := checkResp(v.c.Http.Do(req))
523 if err != nil {
524 return Task{}, fmt.Errorf("error customizing VM: %s", err)
525 }
526
527 task := NewTask(v.c)
528
529 if err = decodeBody(resp, task.Task); err != nil {
530 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
531 }
532
533 // The request was successful
534 return *task, nil
535
536 }
537
538 func (v *VApp) ChangeVMName(name string) (Task, error) {
539 err := v.Refresh()
540 if err != nil {
541 return Task{}, fmt.Errorf("error refreshing vapp before running customization: %v", err)
542 }
543
544 if v.VApp.Children == nil {
545 return Task{}, fmt.Errorf("vApp doesn't contain any children, aborting customization")
546 }
547
548 newname := &types.VM{
549 Name: name,
550 Xmlns: "http://www.vmware.com/vcloud/v1.5",
551 }
552
553 output, err := xml.MarshalIndent(newname, " ", " ")
554 if err != nil {
555 fmt.Printf("error: %v\n", err)
556 }
557
558 log.Printf("[DEBUG] VCD Client configuration: %s", output)
559
560 b := bytes.NewBufferString(xml.Header + string(output))
561
562 s, _ := url.ParseRequestURI(v.VApp.Children.VM[0].HREF)
563
564 req := v.c.NewRequest(map[string]string{}, "PUT", *s, b)
565
566 req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.vm+xml")
567
568 resp, err := checkResp(v.c.Http.Do(req))
569 if err != nil {
570 return Task{}, fmt.Errorf("error customizing VM: %s", err)
571 }
572
573 task := NewTask(v.c)
574
575 if err = decodeBody(resp, task.Task); err != nil {
576 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
577 }
578
579 // The request was successful
580 return *task, nil
581
582 }
583
584 func (v *VApp) DeleteMetadata(key string) (Task, error) {
585 err := v.Refresh()
586 if err != nil {
587 return Task{}, fmt.Errorf("error refreshing vapp before running customization: %v", err)
588 }
589
590 if v.VApp.Children == nil {
591 return Task{}, fmt.Errorf("vApp doesn't contain any children, aborting customization")
592 }
593
594 s, _ := url.ParseRequestURI(v.VApp.Children.VM[0].HREF)
595 s.Path += "/metadata/" + key
596
597 req := v.c.NewRequest(map[string]string{}, "DELETE", *s, nil)
598
599 resp, err := checkResp(v.c.Http.Do(req))
600 if err != nil {
601 return Task{}, fmt.Errorf("error deleting Metadata: %s", err)
602 }
603
604 task := NewTask(v.c)
605
606 if err = decodeBody(resp, task.Task); err != nil {
607 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
608 }
609
610 // The request was successful
611 return *task, nil
612 }
613
614 func (v *VApp) AddMetadata(key, value string) (Task, error) {
615 err := v.Refresh()
616 if err != nil {
617 return Task{}, fmt.Errorf("error refreshing vapp before running customization: %v", err)
618 }
619
620 if v.VApp.Children == nil {
621 return Task{}, fmt.Errorf("vApp doesn't contain any children, aborting customization")
622 }
623
624 newmetadata := &types.MetadataValue{
625 Xmlns: "http://www.vmware.com/vcloud/v1.5",
626 Xsi: "http://www.w3.org/2001/XMLSchema-instance",
627 TypedValue: &types.TypedValue{
628 XsiType: "MetadataStringValue",
629 Value: value,
630 },
631 }
632
633 output, err := xml.MarshalIndent(newmetadata, " ", " ")
634 if err != nil {
635 fmt.Printf("error: %v\n", err)
636 }
637
638 log.Printf("[DEBUG] NetworkXML: %s", output)
639
640 b := bytes.NewBufferString(xml.Header + string(output))
641
642 s, _ := url.ParseRequestURI(v.VApp.Children.VM[0].HREF)
643 s.Path += "/metadata/" + key
644
645 req := v.c.NewRequest(map[string]string{}, "PUT", *s, b)
646
647 req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.metadata.value+xml")
648
649 resp, err := checkResp(v.c.Http.Do(req))
650 if err != nil {
651 return Task{}, fmt.Errorf("error customizing VM Network: %s", err)
652 }
653
654 task := NewTask(v.c)
655
656 if err = decodeBody(resp, task.Task); err != nil {
657 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
658 }
659
660 // The request was successful
661 return *task, nil
662
663 }
664
665 func (v *VApp) SetOvf(parameters map[string]string) (Task, error) {
666 err := v.Refresh()
667 if err != nil {
668 return Task{}, fmt.Errorf("error refreshing vapp before running customization: %v", err)
669 }
670
671 if v.VApp.Children == nil {
672 return Task{}, fmt.Errorf("vApp doesn't contain any children, aborting customization")
673 }
674
675 if v.VApp.Children.VM[0].ProductSection == nil {
676 return Task{}, fmt.Errorf("vApp doesn't contain any children with ProductSection, aborting customization")
677 }
678
679 for key, value := range parameters {
680 for _, ovf_value := range v.VApp.Children.VM[0].ProductSection.Property {
681 if ovf_value.Key == key {
682 ovf_value.Value = &types.Value{Value: value}
683 break
684 }
685 }
686 }
687
688 newmetadata := &types.ProductSectionList{
689 Xmlns: "http://www.vmware.com/vcloud/v1.5",
690 Ovf: "http://schemas.dmtf.org/ovf/envelope/1",
691 ProductSection: v.VApp.Children.VM[0].ProductSection,
692 }
693
694 output, err := xml.MarshalIndent(newmetadata, " ", " ")
695 if err != nil {
696 fmt.Printf("error: %v\n", err)
697 }
698
699 log.Printf("[DEBUG] NetworkXML: %s", output)
700
701 b := bytes.NewBufferString(xml.Header + string(output))
702
703 s, _ := url.ParseRequestURI(v.VApp.Children.VM[0].HREF)
704 s.Path += "/productSections"
705
706 req := v.c.NewRequest(map[string]string{}, "PUT", *s, b)
707
708 req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.productSections+xml")
709
710 resp, err := checkResp(v.c.Http.Do(req))
711 if err != nil {
712 return Task{}, fmt.Errorf("error customizing VM Network: %s", err)
713 }
714
715 task := NewTask(v.c)
716
717 if err = decodeBody(resp, task.Task); err != nil {
718 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
719 }
720
721 // The request was successful
722 return *task, nil
723
724 }
725
726 func (v *VApp) ChangeNetworkConfig(network, ip string) (Task, error) {
727 err := v.Refresh()
728 if err != nil {
729 return Task{}, fmt.Errorf("error refreshing vapp before running customization: %v", err)
730 }
731
732 if v.VApp.Children == nil {
733 return Task{}, fmt.Errorf("vApp doesn't contain any children, aborting customization")
734 }
735
736 // Determine what type of address is requested for the vApp
737 ipAllocationMode := "NONE"
738 ipAddress := "Any"
739
740 // TODO: Review current behaviour of using DHCP when left blank
741 if ip == "" || ip == "dhcp" {
742 ipAllocationMode = "DHCP"
743 } else if ip == "allocated" {
744 ipAllocationMode = "POOL"
745 } else if ip == "none" {
746 ipAllocationMode = "NONE"
747 } else if ip != "" {
748 ipAllocationMode = "MANUAL"
749 // TODO: Check a valid IP has been given
750 ipAddress = ip
751 }
752
753 networkConnection := &types.NetworkConnection{
754 Network: network,
755 NeedsCustomization: true,
756 NetworkConnectionIndex: 0,
757 IPAddress: ipAddress,
758 IsConnected: true,
759 IPAddressAllocationMode: ipAllocationMode,
760 }
761
762 newnetwork := &types.NetworkConnectionSection{
763 Xmlns: "http://www.vmware.com/vcloud/v1.5",
764 Ovf: "http://schemas.dmtf.org/ovf/envelope/1",
765 Info: "Specifies the available VM network connections",
766 PrimaryNetworkConnectionIndex: 0,
767 NetworkConnection: networkConnection,
768 }
769
770 output, err := xml.MarshalIndent(newnetwork, " ", " ")
771 if err != nil {
772 fmt.Printf("error: %v\n", err)
773 }
774
775 log.Printf("[DEBUG] NetworkXML: %s", output)
776
777 b := bytes.NewBufferString(xml.Header + string(output))
778
779 s, _ := url.ParseRequestURI(v.VApp.Children.VM[0].HREF)
780 s.Path += "/networkConnectionSection/"
781
782 req := v.c.NewRequest(map[string]string{}, "PUT", *s, b)
783
784 req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.networkConnectionSection+xml")
785
786 resp, err := checkResp(v.c.Http.Do(req))
787 if err != nil {
788 return Task{}, fmt.Errorf("error customizing VM Network: %s", err)
789 }
790
791 task := NewTask(v.c)
792
793 if err = decodeBody(resp, task.Task); err != nil {
794 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
795 }
796
797 // The request was successful
798 return *task, nil
799 }
800
801 func (v *VApp) ChangeMemorySize(size int) (Task, error) {
802
803 err := v.Refresh()
804 if err != nil {
805 return Task{}, fmt.Errorf("error refreshing vapp before running customization: %v", err)
806 }
807
808 // Check if VApp Children is populated
809 if v.VApp.Children == nil {
810 return Task{}, fmt.Errorf("vApp doesn't contain any children, aborting customization")
811 }
812
813 newmem := &types.OVFItem{
814 XmlnsRasd: "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
815 XmlnsVCloud: "http://www.vmware.com/vcloud/v1.5",
816 XmlnsXsi: "http://www.w3.org/2001/XMLSchema-instance",
817 VCloudHREF: v.VApp.Children.VM[0].HREF + "/virtualHardwareSection/memory",
818 VCloudType: "application/vnd.vmware.vcloud.rasdItem+xml",
819 AllocationUnits: "byte * 2^20",
820 Description: "Memory Size",
821 ElementName: strconv.Itoa(size) + " MB of memory",
822 InstanceID: 5,
823 Reservation: 0,
824 ResourceType: 4,
825 VirtualQuantity: size,
826 Weight: 0,
827 Link: &types.Link{
828 HREF: v.VApp.Children.VM[0].HREF + "/virtualHardwareSection/memory",
829 Rel: "edit",
830 Type: "application/vnd.vmware.vcloud.rasdItem+xml",
831 },
832 }
833
834 output, err := xml.MarshalIndent(newmem, " ", " ")
835 if err != nil {
836 fmt.Printf("error: %v\n", err)
837 }
838
839 debug := os.Getenv("GOVCLOUDAIR_DEBUG")
840
841 if debug == "true" {
842 fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
843 }
844
845 b := bytes.NewBufferString(xml.Header + string(output))
846
847 s, _ := url.ParseRequestURI(v.VApp.Children.VM[0].HREF)
848 s.Path += "/virtualHardwareSection/memory"
849
850 req := v.c.NewRequest(map[string]string{}, "PUT", *s, b)
851
852 req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.rasdItem+xml")
853
854 resp, err := checkResp(v.c.Http.Do(req))
855 if err != nil {
856 return Task{}, fmt.Errorf("error customizing VM: %s", err)
857 }
858
859 task := NewTask(v.c)
860
861 if err = decodeBody(resp, task.Task); err != nil {
862 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
863 }
864
865 // The request was successful
866 return *task, nil
867
868 }