forked from cloudfoundry/cli
/
info.go
128 lines (102 loc) · 3.79 KB
/
info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package ccv2
import (
"net/http"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// APIInformation represents the information returned back from /v2/info
type APIInformation struct {
// APIVersion is the Cloud Controller API version number.
APIVersion string `json:"api_version"`
// AuthorizationEndpoint is the authorization endpoint for the targeted Cloud
// Controller.
AuthorizationEndpoint string `json:"authorization_endpoint"`
DopplerEndpoint string `json:"doppler_logging_endpoint"`
LogCacheEndpoint string `json:"log_cache_endpoint"`
MinCLIVersion string `json:"min_cli_version"`
// MinimumRecommendedCLIVersion is the minimum CLI version number recommended
// for the targeted Cloud Controller.
MinimumRecommendedCLIVersion string `json:"min_recommended_cli_version"`
// Name is the name given to the targeted Cloud Controller.
Name string `json:"name"`
// RoutingEndpoint is the Routing endpoint for the targeted Cloud Controller.
RoutingEndpoint string `json:"routing_endpoint"`
}
// API returns the Cloud Controller API URL for the targeted Cloud Controller.
func (client *Client) API() string {
return client.cloudControllerURL
}
// APIVersion returns Cloud Controller API Version for the targeted Cloud
// Controller.
func (client *Client) APIVersion() string {
return client.cloudControllerAPIVersion
}
// AuthorizationEndpoint returns the authorization endpoint for the targeted
// Cloud Controller.
func (client *Client) AuthorizationEndpoint() string {
return client.authorizationEndpoint
}
// DopplerEndpoint returns the Doppler endpoint for the targetd Cloud
// Controller.
func (client *Client) DopplerEndpoint() string {
return client.dopplerEndpoint
}
func (client *Client) LogCacheEndpoint() string {
return client.logCacheEndpoint
}
// Info returns back endpoint and API information from /v2/info.
func (client *Client) Info() (APIInformation, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetInfoRequest,
})
if err != nil {
return APIInformation{}, nil, err
}
var info APIInformation
response := cloudcontroller.Response{
DecodeJSONResponseInto: &info,
}
err = client.connection.Make(request, &response)
if unknownSourceErr, ok := err.(ccerror.UnknownHTTPSourceError); ok && unknownSourceErr.StatusCode == http.StatusNotFound {
return APIInformation{}, nil, ccerror.APINotFoundError{URL: client.cloudControllerURL}
}
return info, response.Warnings, err
}
// MinCLIVersion returns the minimum CLI version required for the targeted
// Cloud Controller
func (client *Client) MinCLIVersion() string {
return client.minCLIVersion
}
// RoutingEndpoint returns the Routing endpoint for the targeted Cloud
// Controller.
func (client *Client) RoutingEndpoint() string {
return client.routingEndpoint
}
// Info represents a GET response from the '/' endpoint of the cloud
// controller API.
type Info struct {
// Links is a list of top level Cloud Controller APIs.
Links InfoLinks `json:"links"`
}
type InfoLinks struct {
LogCache APILink `json:"log_cache"`
}
// rootResponse returns the CC API root document.
func (client *Client) RootResponse() (Info, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetRootRequest,
})
if err != nil {
return Info{}, nil, err
}
var rootResult Info
response := cloudcontroller.Response{
DecodeJSONResponseInto: &rootResult,
}
err = client.connection.Make(request, &response)
if unknownSourceErr, ok := err.(ccerror.UnknownHTTPSourceError); ok && unknownSourceErr.StatusCode == http.StatusNotFound {
return Info{}, nil, ccerror.APINotFoundError{URL: client.cloudControllerURL}
}
return rootResult, response.Warnings, err
}