Debian Codegraph @

api_vca.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 "fmt"
9 "net/http"
10 "net/url"
11 "os"
12 "time"
13
14 types "github.com/hmrc/vmware-govcd/types/v56"
15 )
16
17 // Client provides a client to vCloud Air, values can be populated automatically using the Authenticate method.
18 type VAClient struct {
19 VAToken string // vCloud Air authorization token
20 VAEndpoint url.URL // vCloud Air API endpoint
21 Region string // Region where the compute resource lives.
22 Client Client // Client for the underlying vCD instance
23 }
24
25 // VCHS API
26
27 type services struct {
28 Service []struct {
29 Region string `xml:"region,attr"`
30 ServiceID string `xml:"serviceId,attr"`
31 ServiceType string `xml:"serviceType,attr"`
32 Type string `xml:"type,attr"`
33 HREF string `xml:"href,attr"`
34 } `xml:"Service"`
35 }
36
37 type session struct {
38 Link []*types.Link `xml:"Link"`
39 }
40
41 type computeResources struct {
42 VdcRef []struct {
43 Status string `xml:"status,attr"`
44 Name string `xml:"name,attr"`
45 Type string `xml:"type,attr"`
46 HREF string `xml:"href,attr"`
47 Link []*types.Link `xml:"Link"`
48 } `xml:"VdcRef"`
49 }
50
51 type vCloudSession struct {
52 VdcLink []struct {
53 AuthorizationToken string `xml:"authorizationToken,attr"`
54 AuthorizationHeader string `xml:"authorizationHeader,attr"`
55 Name string `xml:"name,attr"`
56 HREF string `xml:"href,attr"`
57 } `xml:"VdcLink"`
58 }
59
60 //
61
62 func (c *VAClient) vaauthorize(user, pass string) (u url.URL, err error) {
63
64 if user == "" {
65 user = os.Getenv("VCLOUDAIR_USERNAME")
66 }
67
68 if pass == "" {
69 pass = os.Getenv("VCLOUDAIR_PASSWORD")
70 }
71
72 s := c.VAEndpoint
73 s.Path += "/vchs/sessions"
74
75 // No point in checking for errors here
76 req := c.Client.NewRequest(map[string]string{}, "POST", s, nil)
77
78 // Set Basic Authentication Header
79 req.SetBasicAuth(user, pass)
80
81 // Add the Accept header for vCA
82 req.Header.Add("Accept", "application/xml;version=5.6")
83
84 resp, err := checkResp(c.Client.Http.Do(req))
85 if err != nil {
86 return url.URL{}, err
87 }
88 defer resp.Body.Close()
89
90 // Store the authentication header
91 c.VAToken = resp.Header.Get("X-Vchs-Authorization")
92
93 session := new(session)
94
95 if err = decodeBody(resp, session); err != nil {
96 return url.URL{}, fmt.Errorf("error decoding session response: %s", err)
97 }
98
99 // Loop in the session struct to find right service and compute resource.
100 for _, s := range session.Link {
101 if s.Type == "application/xml;class=vnd.vmware.vchs.servicelist" && s.Rel == "down" {
102 u, err := url.ParseRequestURI(s.HREF)
103 return *u, err
104 }
105 }
106 return url.URL{}, fmt.Errorf("couldn't find a Service List in current session")
107 }
108
109 func (c *VAClient) vaacquireservice(s url.URL, cid string) (u url.URL, err error) {
110
111 if cid == "" {
112 cid = os.Getenv("VCLOUDAIR_COMPUTEID")
113 }
114
115 req := c.Client.NewRequest(map[string]string{}, "GET", s, nil)
116
117 // Add the Accept header for vCA
118 req.Header.Add("Accept", "application/xml;version=5.6")
119
120 // Set Authorization Header for vCA
121 req.Header.Add("x-vchs-authorization", c.VAToken)
122
123 resp, err := checkResp(c.Client.Http.Do(req))
124 if err != nil {
125 return url.URL{}, fmt.Errorf("error processing compute action: %s", err)
126 }
127
128 services := new(services)
129
130 if err = decodeBody(resp, services); err != nil {
131 return url.URL{}, fmt.Errorf("error decoding services response: %s", err)
132 }
133
134 // Loop in the Services struct to find right service and compute resource.
135 for _, s := range services.Service {
136 if s.ServiceID == cid {
137 c.Region = s.Region
138 u, err := url.ParseRequestURI(s.HREF)
139 return *u, err
140 }
141 }
142 return url.URL{}, fmt.Errorf("couldn't find a Compute Resource in current service list")
143 }
144
145 func (c *VAClient) vaacquirecompute(s url.URL, vid string) (u url.URL, err error) {
146
147 if vid == "" {
148 vid = os.Getenv("VCLOUDAIR_VDCID")
149 }
150
151 req := c.Client.NewRequest(map[string]string{}, "GET", s, nil)
152
153 // Add the Accept header for vCA
154 req.Header.Add("Accept", "application/xml;version=5.6")
155
156 // Set Authorization Header
157 req.Header.Add("x-vchs-authorization", c.VAToken)
158
159 // TODO: wrap into checkresp to parse error
160 resp, err := checkResp(c.Client.Http.Do(req))
161 if err != nil {
162 return url.URL{}, fmt.Errorf("error processing compute action: %s", err)
163 }
164
165 computeresources := new(computeResources)
166
167 if err = decodeBody(resp, computeresources); err != nil {
168 return url.URL{}, fmt.Errorf("error decoding computeresources response: %s", err)
169 }
170
171 // Iterate through the ComputeResources struct searching for the right
172 // backend server
173 for _, s := range computeresources.VdcRef {
174 if s.Name == vid {
175 for _, t := range s.Link {
176 if t.Name == vid {
177 u, err := url.ParseRequestURI(t.HREF)
178 return *u, err
179 }
180 }
181 }
182 }
183 return url.URL{}, fmt.Errorf("couldn't find a VDC Resource in current Compute list")
184 }
185
186 func (c *VAClient) vagetbackendauth(s url.URL, cid string) error {
187
188 if cid == "" {
189 cid = os.Getenv("VCLOUDAIR_COMPUTEID")
190 }
191
192 req := c.Client.NewRequest(map[string]string{}, "POST", s, nil)
193
194 // Add the Accept header for vCA
195 req.Header.Add("Accept", "application/xml;version=5.6")
196
197 // Set Authorization Header
198 req.Header.Add("x-vchs-authorization", c.VAToken)
199
200 // TODO: wrap into checkresp to parse error
201 resp, err := checkResp(c.Client.Http.Do(req))
202 if err != nil {
203 return fmt.Errorf("error processing backend url action: %s", err)
204 }
205 defer resp.Body.Close()
206
207 vcloudsession := new(vCloudSession)
208
209 if err = decodeBody(resp, vcloudsession); err != nil {
210 return fmt.Errorf("error decoding vcloudsession response: %s", err)
211 }
212
213 // Get the backend session information
214 for _, s := range vcloudsession.VdcLink {
215 if s.Name == cid {
216 // Fetch the authorization token
217 c.Client.VCDToken = s.AuthorizationToken
218
219 // Fetch the authorization header
220 c.Client.VCDAuthHeader = s.AuthorizationHeader
221
222 u, err := url.ParseRequestURI(s.HREF)
223 if err != nil {
224 return fmt.Errorf("error decoding href: %s", err)
225 }
226 c.Client.VCDVDCHREF = *u
227 return nil
228 }
229 }
230 return fmt.Errorf("error finding the right backend resource")
231 }
232
233 // NewVAClient returns a new empty client to authenticate against the vCloud Air
234 // service, the vCloud Air endpoint can be overridden by setting the
235 // VCLOUDAIR_ENDPOINT environment variable.
236 func NewVAClient() (*VAClient, error) {
237
238 var u *url.URL
239 var err error
240
241 if os.Getenv("VCLOUDAIR_ENDPOINT") != "" {
242 u, err = url.ParseRequestURI(os.Getenv("VCLOUDAIR_ENDPOINT"))
243 if err != nil {
244 return &VAClient{}, fmt.Errorf("cannot parse endpoint coming from VCLOUDAIR_ENDPOINT")
245 }
246 } else {
247 // Implicitly trust this URL parse.
248 u, _ = url.ParseRequestURI("https://vchs.vmware.com/api")
249 }
250
251 VAClient := VAClient{
252 VAEndpoint: *u,
253 Client: Client{
254 APIVersion: "5.6",
255 // Patching things up as we're hitting several TLS timeouts.
256 Http: http.Client{
257 Transport: &http.Transport{
258 Proxy: http.ProxyFromEnvironment,
259 TLSHandshakeTimeout: 120 * time.Second,
260 },
261 },
262 },
263 }
264 return &VAClient, nil
265 }
266
267 // Authenticate is an helper function that performs a complete login in vCloud
268 // Air and in the backend vCloud Director instance.
269 func (c *VAClient) Authenticate(username, password, computeid, vdcid string) (Vdc, error) {
270 // Authorize
271 vaservicehref, err := c.vaauthorize(username, password)
272 if err != nil {
273 return Vdc{}, fmt.Errorf("error Authorizing: %s", err)
274 }
275
276 // Get Service
277 vacomputehref, err := c.vaacquireservice(vaservicehref, computeid)
278 if err != nil {
279 return Vdc{}, fmt.Errorf("error Acquiring Service: %s", err)
280 }
281
282 // Get Compute
283 vavdchref, err := c.vaacquirecompute(vacomputehref, vdcid)
284 if err != nil {
285 return Vdc{}, fmt.Errorf("error Acquiring Compute: %s", err)
286 }
287
288 // Get Backend Authorization
289 if err = c.vagetbackendauth(vavdchref, computeid); err != nil {
290 return Vdc{}, fmt.Errorf("error Acquiring Backend Authorization: %s", err)
291 }
292
293 v, err := c.Client.retrieveVDC()
294 if err != nil {
295 return Vdc{}, fmt.Errorf("error Acquiring VDC: %s", err)
296 }
297
298 return v, nil
299
300 }
301
302 // Disconnect performs a disconnection from the vCloud Air API endpoint.
303 func (c *VAClient) Disconnect() error {
304 if c.Client.VCDToken == "" && c.Client.VCDAuthHeader == "" && c.VAToken == "" {
305 return fmt.Errorf("cannot disconnect, client is not authenticated")
306 }
307
308 s := c.VAEndpoint
309 s.Path += "/vchs/session"
310
311 req := c.Client.NewRequest(map[string]string{}, "DELETE", s, nil)
312
313 // Add the Accept header for vCA
314 req.Header.Add("Accept", "application/xml;version=5.6")
315
316 // Set Authorization Header
317 req.Header.Add("x-vchs-authorization", c.VAToken)
318
319 if _, err := checkResp(c.Client.Http.Do(req)); err != nil {
320 return fmt.Errorf("error processing session delete for vchs: %s", err)
321 }
322
323 return nil
324 }