-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
123 lines (106 loc) · 2.95 KB
/
application.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
package ccv2
import (
"encoding/json"
"net/http"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// Application represents a Cloud Controller Application.
type Application struct {
GUID string
Name string
}
// UnmarshalJSON helps unmarshal a Cloud Controller Application response.
func (application *Application) UnmarshalJSON(data []byte) error {
var ccApp struct {
Metadata internal.Metadata `json:"metadata"`
Entity struct {
Name string `json:"name"`
} `json:"entity"`
}
if err := json.Unmarshal(data, &ccApp); err != nil {
return err
}
application.GUID = ccApp.Metadata.GUID
application.Name = ccApp.Entity.Name
return nil
}
// GetApplications returns back a list of Applications based off of the
// provided queries.
func (client *Client) GetApplications(queries []Query) ([]Application, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.AppsRequest,
Query: FormatQueryParameters(queries),
})
if err != nil {
return nil, nil, err
}
fullAppsList := []Application{}
fullWarningsList := Warnings{}
for {
var apps []Application
wrapper := PaginatedWrapper{
Resources: &apps,
}
response := cloudcontroller.Response{
Result: &wrapper,
}
err := client.connection.Make(request, &response)
fullWarningsList = append(fullWarningsList, response.Warnings...)
if err != nil {
return nil, fullWarningsList, err
}
fullAppsList = append(fullAppsList, apps...)
if wrapper.NextURL == "" {
break
}
request, err = client.newHTTPRequest(requestOptions{
URI: wrapper.NextURL,
Method: http.MethodGet,
})
if err != nil {
return nil, fullWarningsList, err
}
}
return fullAppsList, fullWarningsList, nil
}
// GetRouteApplications returns a list of Applications associated with a route
// GUID, filtered by provided queries.
func (client *Client) GetRouteApplications(routeGUID string, queryParams []Query) ([]Application, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.AppsFromRouteRequest,
URIParams: map[string]string{"route_guid": routeGUID},
Query: FormatQueryParameters(queryParams),
})
if err != nil {
return nil, nil, err
}
fullAppsList := []Application{}
fullWarningsList := Warnings{}
for {
var apps []Application
wrapper := PaginatedWrapper{
Resources: &apps,
}
response := cloudcontroller.Response{
Result: &wrapper,
}
err := client.connection.Make(request, &response)
fullWarningsList = append(fullWarningsList, response.Warnings...)
if err != nil {
return nil, fullWarningsList, err
}
fullAppsList = append(fullAppsList, apps...)
if wrapper.NextURL == "" {
break
}
request, err = client.newHTTPRequest(requestOptions{
URI: wrapper.NextURL,
Method: http.MethodGet,
})
if err != nil {
return nil, fullWarningsList, err
}
}
return fullAppsList, fullWarningsList, nil
}