Debian Codegraph @

api_vcd.go

raw symbols SCIP
1 package govcd
2
3 import (
4 "crypto/tls"
5 "fmt"
6 "net/http"
7 "net/url"
8 "os"
9 "sync"
10 "time"
11 )
12
13 type VCDClient struct {
14 OrgHREF url.URL // vCloud Director OrgRef
15 Org Org // Org
16 OrgVdc Vdc // Org vDC
17 Client Client // Client for the underlying VCD instance
18 sessionHREF url.URL // HREF for the session API
19 QueryHREF url.URL // HREF for the query API
20 Mutex sync.Mutex
21 }
22
23 type supportedVersions struct {
24 VersionInfo struct {
25 Version string `xml:"Version"`
26 LoginUrl string `xml:"LoginUrl"`
27 } `xml:"VersionInfo"`
28 }
29
30 func (c *VCDClient) vcdloginurl() error {
31
32 s := c.Client.VCDVDCHREF
33 s.Path += "/versions"
34
35 // No point in checking for errors here
36 req := c.Client.NewRequest(map[string]string{}, "GET", s, nil)
37
38 resp, err := checkResp(c.Client.Http.Do(req))
39 if err != nil {
40 return err
41 }
42 defer resp.Body.Close()
43
44 supportedVersions := new(supportedVersions)
45
46 err = decodeBody(resp, supportedVersions)
47
48 if err != nil {
49 return fmt.Errorf("error decoding versions response: %s", err)
50 }
51
52 u, err := url.Parse(supportedVersions.VersionInfo.LoginUrl)
53 if err != nil {
54 return fmt.Errorf("couldn't find a LoginUrl in versions")
55 }
56 c.sessionHREF = *u
57 return nil
58 }
59
60 func (c *VCDClient) vcdauthorize(user, pass, org string) error {
61
62 if user == "" {
63 user = os.Getenv("VCLOUD_USERNAME")
64 }
65
66 if pass == "" {
67 pass = os.Getenv("VCLOUD_PASSWORD")
68 }
69
70 if org == "" {
71 org = os.Getenv("VCLOUD_ORG")
72 }
73
74 // No point in checking for errors here
75 req := c.Client.NewRequest(map[string]string{}, "POST", c.sessionHREF, nil)
76
77 // Set Basic Authentication Header
78 req.SetBasicAuth(user+"@"+org, pass)
79
80 // Add the Accept header for vCA
81 req.Header.Add("Accept", "application/*+xml;version=5.5")
82
83 resp, err := checkResp(c.Client.Http.Do(req))
84 if err != nil {
85 return err
86 }
87 defer resp.Body.Close()
88
89 // Store the authentication header
90 c.Client.VCDToken = resp.Header.Get("x-vcloud-authorization")
91 c.Client.VCDAuthHeader = "x-vcloud-authorization"
92
93 session := new(session)
94 err = decodeBody(resp, session)
95
96 if err != nil {
97 fmt.Errorf("error decoding session response: %s", err)
98 }
99
100 org_found := false
101 // Loop in the session struct to find the organization and query api.
102 for _, s := range session.Link {
103 if s.Type == "application/vnd.vmware.vcloud.org+xml" && s.Rel == "down" {
104 u, err := url.Parse(s.HREF)
105 if err != nil {
106 return fmt.Errorf("couldn't find a Organization in current session, %v", err)
107 }
108 c.OrgHREF = *u
109 org_found = true
110 }
111 if s.Type == "application/vnd.vmware.vcloud.query.queryList+xml" && s.Rel == "down" {
112 u, err := url.Parse(s.HREF)
113 if err != nil {
114 return fmt.Errorf("couldn't find a Query API in current session, %v", err)
115 }
116 c.QueryHREF = *u
117 }
118 }
119 if !org_found {
120 return fmt.Errorf("couldn't find a Organization in current session")
121 }
122
123 // Loop in the session struct to find the session url.
124 session_found := false
125 for _, s := range session.Link {
126 if s.Rel == "remove" {
127 u, err := url.Parse(s.HREF)
128 if err != nil {
129 return fmt.Errorf("couldn't find a logout HREF in current session, %v", err)
130 }
131 c.sessionHREF = *u
132 session_found = true
133 }
134 }
135 if !session_found {
136 return fmt.Errorf("couldn't find a logout HREF in current session")
137 }
138 return nil
139 }
140
141 func (c *VCDClient) RetrieveOrg(vcdname string) (Org, error) {
142
143 req := c.Client.NewRequest(map[string]string{}, "GET", c.OrgHREF, nil)
144 req.Header.Add("Accept", "vnd.vmware.vcloud.org+xml;version=5.5")
145
146 // TODO: wrap into checkresp to parse error
147 resp, err := checkResp(c.Client.Http.Do(req))
148 if err != nil {
149 return Org{}, fmt.Errorf("error retreiving org: %s", err)
150 }
151
152 org := NewOrg(&c.Client)
153
154 if err = decodeBody(resp, org.Org); err != nil {
155 return Org{}, fmt.Errorf("error decoding org response: %s", err)
156 }
157
158 // Get the VDC ref from the Org
159 for _, s := range org.Org.Link {
160 if s.Type == "application/vnd.vmware.vcloud.vdc+xml" && s.Rel == "down" {
161 if vcdname != "" && s.Name != vcdname {
162 continue
163 }
164 u, err := url.Parse(s.HREF)
165 if err != nil {
166 return Org{}, err
167 }
168 c.Client.VCDVDCHREF = *u
169 }
170 }
171
172 if &c.Client.VCDVDCHREF == nil {
173 return Org{}, fmt.Errorf("error finding the organization VDC HREF")
174 }
175
176 return *org, nil
177 }
178
179 func NewVCDClient(vcdEndpoint url.URL, insecure bool) *VCDClient {
180
181 return &VCDClient{
182 Client: Client{
183 APIVersion: "5.5",
184 VCDVDCHREF: vcdEndpoint,
185 Http: http.Client{
186 Transport: &http.Transport{
187 TLSClientConfig: &tls.Config{
188 InsecureSkipVerify: insecure,
189 },
190 Proxy: http.ProxyFromEnvironment,
191 TLSHandshakeTimeout: 120 * time.Second,
192 },
193 },
194 },
195 }
196 }
197
198 // Authenticate is an helper function that performs a login in vCloud Director.
199 func (c *VCDClient) Authenticate(username, password, org, vdcname string) (Org, Vdc, error) {
200
201 // LoginUrl
202 err := c.vcdloginurl()
203 if err != nil {
204 return Org{}, Vdc{}, fmt.Errorf("error finding LoginUrl: %s", err)
205 }
206 // Authorize
207 err = c.vcdauthorize(username, password, org)
208 if err != nil {
209 return Org{}, Vdc{}, fmt.Errorf("error authorizing: %s", err)
210 }
211
212 // Get Org
213 o, err := c.RetrieveOrg(vdcname)
214 if err != nil {
215 return Org{}, Vdc{}, fmt.Errorf("error acquiring Org: %s", err)
216 }
217
218 vdc, err := c.Client.retrieveVDC()
219
220 if err != nil {
221 return Org{}, Vdc{}, fmt.Errorf("error retrieving the organization VDC")
222 }
223
224 return o, vdc, nil
225 }
226
227 // Disconnect performs a disconnection from the vCloud Director API endpoint.
228 func (c *VCDClient) Disconnect() error {
229 if c.Client.VCDToken == "" && c.Client.VCDAuthHeader == "" {
230 return fmt.Errorf("cannot disconnect, client is not authenticated")
231 }
232
233 req := c.Client.NewRequest(map[string]string{}, "DELETE", c.sessionHREF, nil)
234
235 // Add the Accept header for vCA
236 req.Header.Add("Accept", "application/xml;version=5.5")
237
238 // Set Authorization Header
239 req.Header.Add(c.Client.VCDAuthHeader, c.Client.VCDToken)
240
241 if _, err := checkResp(c.Client.Http.Do(req)); err != nil {
242 return fmt.Errorf("error processing session delete for vCloud Director: %s", err)
243 }
244 return nil
245 }