Debian Codegraph @

orgvdcnetwork.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 types "github.com/hmrc/vmware-govcd/types/v56"
12 "log"
13 "net/http"
14 "net/url"
15 "regexp"
16 "strings"
17 "time"
18 )
19
20 // OrgVDCNetwork an org vdc network client
21 type OrgVDCNetwork struct {
22 OrgVDCNetwork *types.OrgVDCNetwork
23 c *Client
24 }
25
26 // NewOrgVDCNetwork creates an org vdc network client
27 func NewOrgVDCNetwork(c *Client) *OrgVDCNetwork {
28 return &OrgVDCNetwork{
29 OrgVDCNetwork: new(types.OrgVDCNetwork),
30 c: c,
31 }
32 }
33
34 func (o *OrgVDCNetwork) Refresh() error {
35 if o.OrgVDCNetwork.HREF == "" {
36 return fmt.Errorf("cannot refresh, Object is empty")
37 }
38
39 u, _ := url.ParseRequestURI(o.OrgVDCNetwork.HREF)
40
41 req := o.c.NewRequest(map[string]string{}, "GET", *u, nil)
42
43 resp, err := checkResp(o.c.Http.Do(req))
44 if err != nil {
45 return fmt.Errorf("error retrieving task: %s", err)
46 }
47
48 // Empty struct before a new unmarshal, otherwise we end up with duplicate
49 // elements in slices.
50 o.OrgVDCNetwork = &types.OrgVDCNetwork{}
51
52 if err = decodeBody(resp, o.OrgVDCNetwork); err != nil {
53 return fmt.Errorf("error decoding task response: %s", err)
54 }
55
56 // The request was successful
57 return nil
58 }
59
60 func (o *OrgVDCNetwork) Delete() (Task, error) {
61 err := o.Refresh()
62 if err != nil {
63 return Task{}, fmt.Errorf("Error refreshing network: %s", err)
64 }
65 pathArr := strings.Split(o.OrgVDCNetwork.HREF, "/")
66 s, _ := url.ParseRequestURI(o.OrgVDCNetwork.HREF)
67 s.Path = "/api/admin/network/" + pathArr[len(pathArr)-1]
68
69 var resp *http.Response
70 for {
71 req := o.c.NewRequest(map[string]string{}, "DELETE", *s, nil)
72 resp, err = checkResp(o.c.Http.Do(req))
73 if err != nil {
74 if v, _ := regexp.MatchString("is busy, cannot proceed with the operation.$", err.Error()); v {
75 time.Sleep(3 * time.Second)
76 continue
77 }
78 return Task{}, fmt.Errorf("error deleting Network: %s", err)
79 }
80 break
81 }
82
83 task := NewTask(o.c)
84
85 if err = decodeBody(resp, task.Task); err != nil {
86 return Task{}, fmt.Errorf("error decoding Task response: %s", err)
87 }
88
89 // The request was successful
90 return *task, nil
91 }
92
93 func (v *Vdc) CreateOrgVDCNetwork(networkConfig *types.OrgVDCNetwork) error {
94 for _, av := range v.Vdc.Link {
95 if av.Rel == "add" && av.Type == "application/vnd.vmware.vcloud.orgVdcNetwork+xml" {
96 u, err := url.ParseRequestURI(av.HREF)
97 //return fmt.Errorf("Test output: %#v")
98
99 if err != nil {
100 return fmt.Errorf("error decoding vdc response: %s", err)
101 }
102
103 output, err := xml.MarshalIndent(networkConfig, " ", " ")
104 if err != nil {
105 return fmt.Errorf("error marshaling OrgVDCNetwork compose: %s", err)
106 }
107
108 //return fmt.Errorf("Test output: %s\n%#v", b, v.c)
109
110 var resp *http.Response
111 for {
112 b := bytes.NewBufferString(xml.Header + string(output))
113 log.Printf("[DEBUG] VCD Client configuration: %s", b)
114 req := v.c.NewRequest(map[string]string{}, "POST", *u, b)
115 req.Header.Add("Content-Type", av.Type)
116 resp, err = checkResp(v.c.Http.Do(req))
117 if err != nil {
118 if v, _ := regexp.MatchString("is busy, cannot proceed with the operation.$", err.Error()); v {
119 time.Sleep(3 * time.Second)
120 continue
121 }
122 return fmt.Errorf("error instantiating a new OrgVDCNetwork: %s", err)
123 }
124 break
125 }
126 newstuff := NewOrgVDCNetwork(v.c)
127 if err = decodeBody(resp, newstuff.OrgVDCNetwork); err != nil {
128 return fmt.Errorf("error decoding orgvdcnetwork response: %s", err)
129 }
130 task := NewTask(v.c)
131 for _, t := range newstuff.OrgVDCNetwork.Tasks.Task {
132 task.Task = t
133 err = task.WaitTaskCompletion()
134 if err != nil {
135 return fmt.Errorf("Error performing task: %#v", err)
136 }
137 }
138 }
139 }
140 return nil
141 }