github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/cloudcontroller/ccv3/info.go (about) 1 package ccv3 2 3 import ( 4 "encoding/json" 5 "net/http" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 ) 10 11 // APIInfo represents a GET response from the '/' endpoint of the cloud 12 // controller API. 13 type APIInfo struct { 14 // Links is a list of top level Cloud Controller APIs. 15 Links struct { 16 // CCV3 is the link to the Cloud Controller V3 API 17 CCV3 APILink `json:"cloud_controller_v3"` 18 19 // UAA is the link to the UAA API 20 UAA APILink `json:"uaa"` 21 } `json:"links"` 22 } 23 24 // UAA return the HREF for the UAA. 25 func (info APIInfo) UAA() string { 26 return info.Links.UAA.HREF 27 } 28 29 // CloudControllerAPIVersion return the version for the CloudController. 30 func (info APIInfo) CloudControllerAPIVersion() string { 31 return info.Links.CCV3.Meta.Version 32 } 33 34 func (info APIInfo) ccV3Link() string { 35 return info.Links.CCV3.HREF 36 } 37 38 // ResourceLinks represents the information returned back from /v3. 39 type ResourceLinks map[string]APILink 40 41 // UnmarshalJSON helps unmarshal a Cloud Controller /v3 response. 42 func (resources ResourceLinks) UnmarshalJSON(data []byte) error { 43 var ccResourceLinks struct { 44 Links map[string]APILink `json:"links"` 45 } 46 if err := json.Unmarshal(data, &ccResourceLinks); err != nil { 47 return err 48 } 49 50 for key, val := range ccResourceLinks.Links { 51 resources[key] = val 52 } 53 54 return nil 55 } 56 57 // Info returns endpoint and API information from /v3. 58 func (client *Client) Info() (APIInfo, ResourceLinks, Warnings, error) { 59 rootResponse, warnings, err := client.rootResponse() 60 if err != nil { 61 return APIInfo{}, ResourceLinks{}, warnings, err 62 } 63 64 request, err := client.newHTTPRequest(requestOptions{ 65 Method: http.MethodGet, 66 URL: rootResponse.ccV3Link(), 67 }) 68 if err != nil { 69 return APIInfo{}, ResourceLinks{}, warnings, err 70 } 71 72 info := ResourceLinks{} // Explicitly initializing 73 response := cloudcontroller.Response{ 74 Result: &info, 75 } 76 77 err = client.connection.Make(request, &response) 78 warnings = append(warnings, response.Warnings...) 79 80 if err != nil { 81 return APIInfo{}, ResourceLinks{}, warnings, err 82 } 83 84 return rootResponse, info, warnings, nil 85 } 86 87 // rootResponse returns the CC API root document. 88 func (client *Client) rootResponse() (APIInfo, Warnings, error) { 89 request, err := client.newHTTPRequest(requestOptions{ 90 Method: http.MethodGet, 91 URL: client.cloudControllerURL, 92 }) 93 if err != nil { 94 return APIInfo{}, nil, err 95 } 96 97 var rootResponse APIInfo 98 response := cloudcontroller.Response{ 99 Result: &rootResponse, 100 } 101 102 err = client.connection.Make(request, &response) 103 if err != nil { 104 if _, ok := err.(ccerror.NotFoundError); ok { 105 return APIInfo{}, nil, ccerror.APINotFoundError{URL: client.cloudControllerURL} 106 } 107 return APIInfo{}, response.Warnings, err 108 } 109 110 return rootResponse, response.Warnings, nil 111 }