-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathrest_cloud_foundry_client_extended.go
More file actions
237 lines (206 loc) · 7.88 KB
/
rest_cloud_foundry_client_extended.go
File metadata and controls
237 lines (206 loc) · 7.88 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package cfrestclient
import (
"crypto/md5"
"crypto/tls"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"code.cloudfoundry.org/cli/v8/plugin"
"code.cloudfoundry.org/jsonry"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/baseclient"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/log"
)
const cfBaseUrl = "v3/"
type CloudFoundryRestClient struct {
cliConn plugin.CliConnection
isSslDisabled bool
}
func NewCloudFoundryRestClient(cliConn plugin.CliConnection) CloudFoundryOperationsExtended {
isSslDisabled, err := cliConn.IsSSLDisabled()
if err != nil {
log.Tracef("Error while determining skip-ssl-validation: %v", err)
isSslDisabled = false
}
return &CloudFoundryRestClient{cliConn, isSslDisabled}
}
func (c CloudFoundryRestClient) GetApplications(mtaId, mtaNamespace, spaceGuid string) ([]models.CloudFoundryApplication, error) {
token, err := c.cliConn.AccessToken()
if err != nil {
return nil, fmt.Errorf("failed to retrieve access token: %s", err)
}
apiEndpoint, _ := c.cliConn.ApiEndpoint()
mtaIdHash := md5.Sum([]byte(mtaId))
mtaIdHashStr := hex.EncodeToString(mtaIdHash[:])
getAppsUrl := fmt.Sprintf("%s/%sapps?space_guids=%s&label_selector=mta_id=%s", apiEndpoint, cfBaseUrl, spaceGuid, mtaIdHashStr)
if mtaNamespace != "" {
namespaceHash := md5.Sum([]byte(mtaNamespace))
namespaceHashStr := hex.EncodeToString(namespaceHash[:])
getAppsUrl = fmt.Sprintf("%s,mta_namespace=%s", getAppsUrl, namespaceHashStr)
} else {
getAppsUrl = fmt.Sprintf("%s,!mta_namespace", getAppsUrl)
}
return getPaginatedResources[models.CloudFoundryApplication](getAppsUrl, token, c.isSslDisabled)
}
func (c CloudFoundryRestClient) GetAppProcessStatistics(appGuid string) ([]models.ApplicationProcessStatistics, error) {
token, err := c.cliConn.AccessToken()
if err != nil {
return nil, fmt.Errorf("failed to retrieve access token: %s", err)
}
apiEndpoint, _ := c.cliConn.ApiEndpoint()
getAppProcessStatsUrl := fmt.Sprintf("%s/%sapps/%s/processes/web/stats", apiEndpoint, cfBaseUrl, appGuid)
body, err := executeRequest(getAppProcessStatsUrl, token, c.isSslDisabled)
if err != nil {
return nil, err
}
processStats, err := parseBody[models.AppProcessStatisticsResponse](body)
if err != nil {
return nil, err
}
return processStats.Resources, nil
}
func (c CloudFoundryRestClient) GetApplicationRoutes(appGuid string) ([]models.ApplicationRoute, error) {
token, err := c.cliConn.AccessToken()
if err != nil {
return nil, fmt.Errorf("failed to retrieve access token: %s", err)
}
apiEndpoint, _ := c.cliConn.ApiEndpoint()
getAppRoutesUrl := fmt.Sprintf("%s/%sapps/%s/routes", apiEndpoint, cfBaseUrl, appGuid)
return getPaginatedResources[models.ApplicationRoute](getAppRoutesUrl, token, c.isSslDisabled)
}
func (c CloudFoundryRestClient) GetServiceInstances(mtaId, mtaNamespace, spaceGuid string) ([]models.CloudFoundryServiceInstance, error) {
token, err := c.cliConn.AccessToken()
if err != nil {
return nil, fmt.Errorf("failed to retrieve access token: %s", err)
}
apiEndpoint, _ := c.cliConn.ApiEndpoint()
mtaIdHash := md5.Sum([]byte(mtaId))
mtaIdHashStr := hex.EncodeToString(mtaIdHash[:])
getServicesUrl := fmt.Sprintf("%s/%sservice_instances?fields[service_plan]=guid,name,relationships.service_offering&fields[service_plan.service_offering]=guid,name&space_guids=%s&label_selector=mta_id=%s",
apiEndpoint, cfBaseUrl, spaceGuid, mtaIdHashStr)
if mtaNamespace != "" {
namespaceHash := md5.Sum([]byte(mtaNamespace))
namespaceHashStr := hex.EncodeToString(namespaceHash[:])
getServicesUrl = fmt.Sprintf("%s,mta_namespace=%s", getServicesUrl, namespaceHashStr)
} else {
getServicesUrl = fmt.Sprintf("%s,!mta_namespace", getServicesUrl)
}
return getPaginatedResourcesWithIncluded(getServicesUrl, token, c.isSslDisabled, buildServiceInstance)
}
func (c CloudFoundryRestClient) GetServiceBindings(serviceName string) ([]models.ServiceBinding, error) {
token, err := c.cliConn.AccessToken()
if err != nil {
return nil, fmt.Errorf("failed to retrieve access token: %s", err)
}
apiEndpoint, _ := c.cliConn.ApiEndpoint()
getServiceBindingsUrl := fmt.Sprintf("%s/%sservice_credential_bindings?type=app&include=app&service_instance_names=%s", apiEndpoint, cfBaseUrl, serviceName)
return getPaginatedResourcesWithIncluded(getServiceBindingsUrl, token, c.isSslDisabled, buildServiceBinding)
}
func getPaginatedResources[T any](url, token string, isSslDisabled bool) ([]T, error) {
var result []T
for url != "" {
body, err := executeRequest(url, token, isSslDisabled)
if err != nil {
return nil, err
}
response, err := parseBody[models.PaginatedResponse[T]](body)
if err != nil {
return nil, err
}
for _, entity := range response.Resources {
result = append(result, entity)
}
url = response.Pagination.NextPage
}
return result, nil
}
func getPaginatedResourcesWithIncluded[T any, Auxiliary any](url, token string, isSslDisabled bool, auxiliaryContentHandler func(T, Auxiliary) T) ([]T, error) {
var result []T
for url != "" {
body, err := executeRequest(url, token, isSslDisabled)
if err != nil {
return nil, err
}
response, err := parseBody[models.PaginatedResponseWithIncluded[T, Auxiliary]](body)
if err != nil {
return nil, err
}
for _, entity := range response.Resources {
result = append(result, auxiliaryContentHandler(entity, response.Included))
}
url = response.Pagination.NextPage
}
return result, nil
}
func executeRequest(url, token string, isSslDisabled bool) ([]byte, error) {
req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header.Add("Authorization", token)
// Create transport with TLS configuration
httpTransport := http.DefaultTransport.(*http.Transport).Clone()
httpTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: isSslDisabled}
// Wrap with User-Agent transport
userAgentTransport := baseclient.NewUserAgentTransport(httpTransport)
client := &http.Client{Transport: userAgentTransport}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode/100 != 2 {
return nil, models.HttpResponseError{Underlying: fmt.Errorf("%s: %s", resp.Status, string(bytes))}
}
return bytes, nil
}
func parseBody[T any](body []byte) (T, error) {
var result T
err := jsonry.Unmarshal(body, &result)
if err == nil {
return result, nil
}
//jsonry doesn't work with raw objects like map, so try the base json decoder
err = json.Unmarshal(body, &result)
if err == nil {
return result, nil
}
return result, fmt.Errorf("could not parse response: %s", err)
}
func buildServiceInstance(service models.CloudFoundryServiceInstance, auxiliaryContent models.ServiceInstanceAuxiliaryContent) models.CloudFoundryServiceInstance {
servicePlan := findServicePlan(service.PlanGuid, auxiliaryContent.ServicePlans)
service.Plan = servicePlan
service.Offering = findServiceOffering(servicePlan, auxiliaryContent.ServiceOfferings)
return service
}
func findServicePlan(planGuid string, plans []models.ServicePlan) models.ServicePlan {
for _, plan := range plans {
if plan.Guid == planGuid {
return plan
}
}
return models.ServicePlan{}
}
func findServiceOffering(plan models.ServicePlan, offerings []models.ServiceOffering) models.ServiceOffering {
for _, offering := range offerings {
if offering.Guid == plan.OfferingGuid {
return offering
}
}
return models.ServiceOffering{}
}
func buildServiceBinding(binding models.ServiceBinding, auxiliaryContent models.ServiceBindingAuxiliaryContent) models.ServiceBinding {
binding.AppName = findApp(binding.AppGuid, auxiliaryContent.Apps).Name
return binding
}
func findApp(appGuid string, apps []models.CloudFoundryApplication) models.CloudFoundryApplication {
for _, app := range apps {
if app.Guid == appGuid {
return app
}
}
return models.CloudFoundryApplication{}
}