github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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 // Logging is the link to the Logging API 20 Logging APILink `json:"logging"` 21 22 NetworkPolicyV1 APILink `json:"network_policy_v1"` 23 24 // UAA is the link to the UAA API 25 UAA APILink `json:"uaa"` 26 } `json:"links"` 27 } 28 29 // Logging returns the HREF for Logging. 30 func (info APIInfo) Logging() string { 31 return info.Links.Logging.HREF 32 } 33 34 func (info APIInfo) NetworkPolicyV1() string { 35 return info.Links.NetworkPolicyV1.HREF 36 } 37 38 // UAA returns the HREF for the UAA. 39 func (info APIInfo) UAA() string { 40 return info.Links.UAA.HREF 41 } 42 43 // CloudControllerAPIVersion returns the version for the CloudController. 44 func (info APIInfo) CloudControllerAPIVersion() string { 45 return info.Links.CCV3.Meta.Version 46 } 47 48 func (info APIInfo) ccV3Link() string { 49 return info.Links.CCV3.HREF 50 } 51 52 // ResourceLinks represents the information returned back from /v3. 53 type ResourceLinks map[string]APILink 54 55 // UnmarshalJSON helps unmarshal a Cloud Controller /v3 response. 56 func (resources ResourceLinks) UnmarshalJSON(data []byte) error { 57 var ccResourceLinks struct { 58 Links map[string]APILink `json:"links"` 59 } 60 if err := json.Unmarshal(data, &ccResourceLinks); err != nil { 61 return err 62 } 63 64 for key, val := range ccResourceLinks.Links { 65 resources[key] = val 66 } 67 68 return nil 69 } 70 71 // Info returns endpoint and API information from /v3. 72 func (client *Client) Info() (APIInfo, ResourceLinks, Warnings, error) { 73 rootResponse, warnings, err := client.rootResponse() 74 if err != nil { 75 return APIInfo{}, ResourceLinks{}, warnings, err 76 } 77 78 request, err := client.newHTTPRequest(requestOptions{ 79 Method: http.MethodGet, 80 URL: rootResponse.ccV3Link(), 81 }) 82 if err != nil { 83 return APIInfo{}, ResourceLinks{}, warnings, err 84 } 85 86 info := ResourceLinks{} // Explicitly initializing 87 response := cloudcontroller.Response{ 88 Result: &info, 89 } 90 91 err = client.connection.Make(request, &response) 92 warnings = append(warnings, response.Warnings...) 93 94 if err != nil { 95 return APIInfo{}, ResourceLinks{}, warnings, err 96 } 97 98 return rootResponse, info, warnings, nil 99 } 100 101 // rootResponse returns the CC API root document. 102 func (client *Client) rootResponse() (APIInfo, Warnings, error) { 103 request, err := client.newHTTPRequest(requestOptions{ 104 Method: http.MethodGet, 105 URL: client.cloudControllerURL, 106 }) 107 if err != nil { 108 return APIInfo{}, nil, err 109 } 110 111 var rootResponse APIInfo 112 response := cloudcontroller.Response{ 113 Result: &rootResponse, 114 } 115 116 err = client.connection.Make(request, &response) 117 if err != nil { 118 if _, ok := err.(ccerror.NotFoundError); ok { 119 return APIInfo{}, nil, ccerror.APINotFoundError{URL: client.cloudControllerURL} 120 } 121 return APIInfo{}, response.Warnings, err 122 } 123 124 return rootResponse, response.Warnings, nil 125 }