-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_instance.go
159 lines (132 loc) · 4.29 KB
/
service_instance.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
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
package ccv2
import (
"encoding/json"
"net/http"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// ServiceInstanceType is the type of the Service Instance.
type ServiceInstanceType string
const (
// UserProvidedService is a Service Instance that is created by a user.
UserProvidedService ServiceInstanceType = "user_provided_service_instance"
// ManagedService is a Service Instance that is managed by a service broker.
ManagedService ServiceInstanceType = "managed_service_instance"
)
// ServiceInstance represents a Cloud Controller Service Instance.
type ServiceInstance struct {
GUID string
Name string
Type ServiceInstanceType
}
// UnmarshalJSON helps unmarshal a Cloud Controller Service Instance response.
func (serviceInstance *ServiceInstance) UnmarshalJSON(data []byte) error {
var ccServiceInstance struct {
Metadata internal.Metadata
Entity struct {
Name string
Type string
}
}
err := json.Unmarshal(data, &ccServiceInstance)
if err != nil {
return err
}
serviceInstance.GUID = ccServiceInstance.Metadata.GUID
serviceInstance.Name = ccServiceInstance.Entity.Name
serviceInstance.Type = ServiceInstanceType(ccServiceInstance.Entity.Type)
return nil
}
// UserProvidedService returns true if the Service Instance is a user provided
// service.
func (serviceInstance ServiceInstance) UserProvided() bool {
return serviceInstance.Type == UserProvidedService
}
// Managed returns true if the Service Instance is a managed service.
func (serviceInstance ServiceInstance) Managed() bool {
return serviceInstance.Type == ManagedService
}
// GetServiceInstances returns back a list of *managed* Service Instances based
// off of the provided queries.
func (client *Client) GetServiceInstances(queries []Query) ([]ServiceInstance, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.ServiceInstancesRequest,
Query: FormatQueryParameters(queries),
})
if err != nil {
return nil, nil, err
}
allServiceInstancesList := []ServiceInstance{}
allWarningsList := Warnings{}
for {
var serviceInstances []ServiceInstance
wrapper := PaginatedWrapper{
Resources: &serviceInstances,
}
response := cloudcontroller.Response{
Result: &wrapper,
}
err := client.connection.Make(request, &response)
allWarningsList = append(allWarningsList, response.Warnings...)
if err != nil {
return nil, allWarningsList, err
}
allServiceInstancesList = append(allServiceInstancesList, serviceInstances...)
if wrapper.NextURL == "" {
break
}
request, err = client.newHTTPRequest(requestOptions{
URI: wrapper.NextURL,
Method: http.MethodGet,
})
if err != nil {
return nil, allWarningsList, err
}
}
return allServiceInstancesList, allWarningsList, nil
}
// GetSpaceServiceInstances returns back a list of Service Instances based off
// of the space and queries provided. User provided services will be included
// if includeUserProvidedServices is set to true.
func (client *Client) GetSpaceServiceInstances(spaceGUID string, includeUserProvidedServices bool, queries []Query) ([]ServiceInstance, Warnings, error) {
query := FormatQueryParameters(queries)
if includeUserProvidedServices {
query.Add("return_user_provided_service_instances", "true")
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.SpaceServiceInstancesRequest,
URIParams: map[string]string{"guid": spaceGUID},
Query: query,
})
if err != nil {
return nil, nil, err
}
allServiceInstancesList := []ServiceInstance{}
allWarningsList := Warnings{}
for {
var serviceInstances []ServiceInstance
wrapper := PaginatedWrapper{
Resources: &serviceInstances,
}
response := cloudcontroller.Response{
Result: &wrapper,
}
err := client.connection.Make(request, &response)
allWarningsList = append(allWarningsList, response.Warnings...)
if err != nil {
return nil, allWarningsList, err
}
allServiceInstancesList = append(allServiceInstancesList, serviceInstances...)
if wrapper.NextURL == "" {
break
}
request, err = client.newHTTPRequest(requestOptions{
URI: wrapper.NextURL,
Method: http.MethodGet,
})
if err != nil {
return nil, allWarningsList, err
}
}
return allServiceInstancesList, allWarningsList, nil
}