github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/api/cloudcontroller/ccv3/info.go (about) 1 package ccv3 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 8 ) 9 10 // Info represents a GET response from the '/' endpoint of the cloud 11 // controller API. 12 type Info struct { 13 // Links is a list of top level Cloud Controller APIs. 14 Links struct { 15 // AppSSH is the link for application ssh info. 16 AppSSH APILink `json:"app_ssh"` 17 18 // CCV3 is the link to the Cloud Controller V3 API. 19 CCV3 APILink `json:"cloud_controller_v3"` 20 21 // Logging is the link to the Logging API. 22 Logging APILink `json:"logging"` 23 24 // NetworkPolicyV1 is the link to the Container to Container Networking 25 // API. 26 NetworkPolicyV1 APILink `json:"network_policy_v1"` 27 28 // UAA is the link to the UAA API. 29 UAA APILink `json:"uaa"` 30 } `json:"links"` 31 } 32 33 // AppSSHEndpoint returns the HREF for SSHing into an app container. 34 func (info Info) AppSSHEndpoint() string { 35 return info.Links.AppSSH.HREF 36 } 37 38 // AppSSHHostKeyFingerprint returns the SSH key fingerprint of the SSH proxy 39 // that brokers connections to application instances. 40 func (info Info) AppSSHHostKeyFingerprint() string { 41 return info.Links.AppSSH.Meta.HostKeyFingerprint 42 } 43 44 // CloudControllerAPIVersion returns the version of the CloudController. 45 func (info Info) CloudControllerAPIVersion() string { 46 return info.Links.CCV3.Meta.Version 47 } 48 49 // Logging returns the HREF of the Loggregator Traffic Controller. 50 func (info Info) Logging() string { 51 return info.Links.Logging.HREF 52 } 53 54 // NetworkPolicyV1 returns the HREF of the Container Networking v1 Policy API 55 func (info Info) NetworkPolicyV1() string { 56 return info.Links.NetworkPolicyV1.HREF 57 } 58 59 // OAuthClient returns the oauth client ID of the SSH proxy that brokers 60 // connections to application instances. 61 func (info Info) OAuthClient() string { 62 return info.Links.AppSSH.Meta.OAuthClient 63 } 64 65 // UAA returns the HREF of the UAA server. 66 func (info Info) UAA() string { 67 return info.Links.UAA.HREF 68 } 69 70 // ccv3Link returns the HREF of the CloudController v3 API. 71 func (info Info) ccV3Link() string { 72 return info.Links.CCV3.HREF 73 } 74 75 // ResourceLinks represents the information returned back from /v3. 76 type ResourceLinks map[string]APILink 77 78 // UnmarshalJSON helps unmarshal a Cloud Controller /v3 response. 79 func (resources ResourceLinks) UnmarshalJSON(data []byte) error { 80 var ccResourceLinks struct { 81 Links map[string]APILink `json:"links"` 82 } 83 err := cloudcontroller.DecodeJSON(data, &ccResourceLinks) 84 if err != nil { 85 return err 86 } 87 88 for key, val := range ccResourceLinks.Links { 89 resources[key] = val 90 } 91 92 return nil 93 } 94 95 // GetInfo returns endpoint and API information from /v3. 96 func (client *Client) GetInfo() (Info, ResourceLinks, Warnings, error) { 97 rootResponse, warnings, err := client.rootResponse() 98 if err != nil { 99 return Info{}, ResourceLinks{}, warnings, err 100 } 101 102 request, err := client.newHTTPRequest(requestOptions{ 103 Method: http.MethodGet, 104 URL: rootResponse.ccV3Link(), 105 }) 106 if err != nil { 107 return Info{}, ResourceLinks{}, warnings, err 108 } 109 110 info := ResourceLinks{} // Explicitly initializing 111 response := cloudcontroller.Response{ 112 Result: &info, 113 } 114 115 err = client.connection.Make(request, &response) 116 warnings = append(warnings, response.Warnings...) 117 118 if err != nil { 119 return Info{}, ResourceLinks{}, warnings, err 120 } 121 122 return rootResponse, info, warnings, nil 123 } 124 125 // rootResponse returns the CC API root document. 126 func (client *Client) rootResponse() (Info, Warnings, error) { 127 request, err := client.newHTTPRequest(requestOptions{ 128 Method: http.MethodGet, 129 URL: client.cloudControllerURL, 130 }) 131 if err != nil { 132 return Info{}, nil, err 133 } 134 135 var rootResponse Info 136 response := cloudcontroller.Response{ 137 Result: &rootResponse, 138 } 139 140 err = client.connection.Make(request, &response) 141 if unknownSourceErr, ok := err.(ccerror.UnknownHTTPSourceError); ok && unknownSourceErr.StatusCode == http.StatusNotFound { 142 return Info{}, nil, ccerror.APINotFoundError{URL: client.cloudControllerURL} 143 } 144 145 return rootResponse, response.Warnings, err 146 }