/
service_instance.go
261 lines (219 loc) · 8.58 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package ccv2
import (
"bytes"
"encoding/json"
"net/url"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// ServiceInstance represents a Cloud Controller Service Instance.
type ServiceInstance struct {
// GUID is the unique service instance identifier.
GUID string
// Name is the name given to the service instance.
Name string
// SpaceGUID is the unique identifier of the space that this service instance
// belongs to.
SpaceGUID string
// ServiceGUID is the unique identifier of the service that this service
// instance belongs to.
ServiceGUID string
// ServicePlanGUID is the unique identifier of the service plan that this
// service instance belongs to.
ServicePlanGUID string
// Type is the type of service instance.
Type constant.ServiceInstanceType
// Tags is a list of all tags for the service instance.
Tags []string
// DashboardURL is the service-broker provided URL to access administrative
// features of the service instance.
DashboardURL string
// RouteServiceURL is the URL of the user-provided service to which requests
// for bound routes will be forwarded.
RouteServiceURL string
// LastOperation is the status of the last operation requested on the service
// instance.
LastOperation LastOperation
// MaintenanceInfo is the maintenance version of this service instance
MaintenanceInfo MaintenanceInfo
}
// Managed returns true if the Service Instance is a managed service.
func (serviceInstance ServiceInstance) Managed() bool {
return serviceInstance.Type == constant.ManagedService
}
// 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 `json:"name"`
SpaceGUID string `json:"space_guid"`
ServiceGUID string `json:"service_guid"`
ServicePlanGUID string `json:"service_plan_guid"`
Type string `json:"type"`
Tags []string `json:"tags"`
DashboardURL string `json:"dashboard_url"`
RouteServiceURL string `json:"route_service_url"`
LastOperation LastOperation `json:"last_operation"`
MaintenanceInfo MaintenanceInfo `json:"maintenance_info"`
}
}
err := cloudcontroller.DecodeJSON(data, &ccServiceInstance)
if err != nil {
return err
}
serviceInstance.GUID = ccServiceInstance.Metadata.GUID
serviceInstance.Name = ccServiceInstance.Entity.Name
serviceInstance.SpaceGUID = ccServiceInstance.Entity.SpaceGUID
serviceInstance.ServiceGUID = ccServiceInstance.Entity.ServiceGUID
serviceInstance.ServicePlanGUID = ccServiceInstance.Entity.ServicePlanGUID
serviceInstance.Type = constant.ServiceInstanceType(ccServiceInstance.Entity.Type)
serviceInstance.Tags = ccServiceInstance.Entity.Tags
serviceInstance.DashboardURL = ccServiceInstance.Entity.DashboardURL
serviceInstance.RouteServiceURL = ccServiceInstance.Entity.RouteServiceURL
serviceInstance.LastOperation = ccServiceInstance.Entity.LastOperation
serviceInstance.MaintenanceInfo = ccServiceInstance.Entity.MaintenanceInfo
return nil
}
// UserProvided returns true if the Service Instance is a user provided
// service.
func (serviceInstance ServiceInstance) UserProvided() bool {
return serviceInstance.Type == constant.UserProvidedService
}
type createServiceInstanceRequestBody struct {
Name string `json:"name"`
ServicePlanGUID string `json:"service_plan_guid"`
SpaceGUID string `json:"space_guid"`
Parameters map[string]interface{} `json:"parameters,omitempty"`
Tags []string `json:"tags,omitempty"`
}
// CreateServiceInstance posts a service instance resource with the provided
// attributes to the api and returns the result.
func (client *Client) CreateServiceInstance(spaceGUID, servicePlanGUID, serviceInstance string, parameters map[string]interface{}, tags []string) (ServiceInstance, Warnings, error) {
requestBody := createServiceInstanceRequestBody{
Name: serviceInstance,
ServicePlanGUID: servicePlanGUID,
SpaceGUID: spaceGUID,
Parameters: parameters,
Tags: tags,
}
bodyBytes, err := json.Marshal(requestBody)
if err != nil {
return ServiceInstance{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostServiceInstancesRequest,
Body: bytes.NewReader(bodyBytes),
Query: url.Values{"accepts_incomplete": {"true"}},
})
if err != nil {
return ServiceInstance{}, nil, err
}
var instance ServiceInstance
response := cloudcontroller.Response{
DecodeJSONResponseInto: &instance,
}
err = client.connection.Make(request, &response)
return instance, response.Warnings, err
}
// GetServiceInstance returns the service instance with the given GUID. This
// service can be either a managed or user provided.
func (client *Client) GetServiceInstance(serviceInstanceGUID string) (ServiceInstance, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServiceInstanceRequest,
URIParams: Params{"service_instance_guid": serviceInstanceGUID},
})
if err != nil {
return ServiceInstance{}, nil, err
}
var serviceInstance ServiceInstance
response := cloudcontroller.Response{
DecodeJSONResponseInto: &serviceInstance,
}
err = client.connection.Make(request, &response)
return serviceInstance, response.Warnings, err
}
// GetServiceInstances returns back a list of *managed* Service Instances based
// off of the provided filters.
func (client *Client) GetServiceInstances(filters ...Filter) ([]ServiceInstance, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServiceInstancesRequest,
Query: ConvertFilterParameters(filters),
})
if err != nil {
return nil, nil, err
}
var fullInstancesList []ServiceInstance
warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
if instance, ok := item.(ServiceInstance); ok {
fullInstancesList = append(fullInstancesList, instance)
} else {
return ccerror.UnknownObjectInListError{
Expected: ServiceInstance{},
Unexpected: item,
}
}
return nil
})
return fullInstancesList, warnings, err
}
// GetSpaceServiceInstances returns back a list of Service Instances based off
// of the space and filters provided. User provided services will be included
// if includeUserProvidedServices is set to true.
func (client *Client) GetSpaceServiceInstances(spaceGUID string, includeUserProvidedServices bool, filters ...Filter) ([]ServiceInstance, Warnings, error) {
query := ConvertFilterParameters(filters)
if includeUserProvidedServices {
query.Add("return_user_provided_service_instances", "true")
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSpaceServiceInstancesRequest,
URIParams: map[string]string{"guid": spaceGUID},
Query: query,
})
if err != nil {
return nil, nil, err
}
var fullInstancesList []ServiceInstance
warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
if instance, ok := item.(ServiceInstance); ok {
fullInstancesList = append(fullInstancesList, instance)
} else {
return ccerror.UnknownObjectInListError{
Expected: ServiceInstance{},
Unexpected: item,
}
}
return nil
})
return fullInstancesList, warnings, err
}
type MaintenanceInfo struct {
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
}
type updateServiceInstanceRequestBody struct {
MaintenanceInfo MaintenanceInfo `json:"maintenance_info"`
}
func (client *Client) UpdateServiceInstanceMaintenanceInfo(serviceInstanceGUID string, maintenanceInfo MaintenanceInfo) (Warnings, error) {
requestBody := updateServiceInstanceRequestBody{
MaintenanceInfo: maintenanceInfo,
}
bodyBytes, err := json.Marshal(requestBody)
if err != nil {
return nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutServiceInstanceRequest,
URIParams: Params{"service_instance_guid": serviceInstanceGUID},
Body: bytes.NewReader(bodyBytes),
Query: url.Values{"accepts_incomplete": {"true"}},
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}