forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.go
314 lines (256 loc) · 12.1 KB
/
services.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package api
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/cloudfoundry/cli/cf/api/resources"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
//go:generate counterfeiter . ServiceRepository
type ServiceRepository interface {
PurgeServiceOffering(offering models.ServiceOffering) error
GetServiceOfferingByGUID(serviceGUID string) (offering models.ServiceOffering, apiErr error)
FindServiceOfferingsByLabel(name string) (offering models.ServiceOfferings, apiErr error)
FindServiceOfferingByLabelAndProvider(name, provider string) (offering models.ServiceOffering, apiErr error)
FindServiceOfferingsForSpaceByLabel(spaceGUID, name string) (offering models.ServiceOfferings, apiErr error)
GetAllServiceOfferings() (offerings models.ServiceOfferings, apiErr error)
GetServiceOfferingsForSpace(spaceGUID string) (offerings models.ServiceOfferings, apiErr error)
FindInstanceByName(name string) (instance models.ServiceInstance, apiErr error)
PurgeServiceInstance(instance models.ServiceInstance) error
CreateServiceInstance(name, planGUID string, params map[string]interface{}, tags []string) (apiErr error)
UpdateServiceInstance(instanceGUID, planGUID string, params map[string]interface{}, tags []string) (apiErr error)
RenameService(instance models.ServiceInstance, newName string) (apiErr error)
DeleteService(instance models.ServiceInstance) (apiErr error)
FindServicePlanByDescription(planDescription resources.ServicePlanDescription) (planGUID string, apiErr error)
ListServicesFromBroker(brokerGUID string) (services []models.ServiceOffering, err error)
ListServicesFromManyBrokers(brokerGUIDs []string) (services []models.ServiceOffering, err error)
GetServiceInstanceCountForServicePlan(v1PlanGUID string) (count int, apiErr error)
MigrateServicePlanFromV1ToV2(v1PlanGUID, v2PlanGUID string) (changedCount int, apiErr error)
}
type CloudControllerServiceRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCloudControllerServiceRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerServiceRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerServiceRepository) GetServiceOfferingByGUID(serviceGUID string) (models.ServiceOffering, error) {
offering := new(resources.ServiceOfferingResource)
apiErr := repo.gateway.GetResource(repo.config.APIEndpoint()+fmt.Sprintf("/v2/services/%s", serviceGUID), offering)
serviceOffering := offering.ToFields()
return models.ServiceOffering{ServiceOfferingFields: serviceOffering}, apiErr
}
func (repo CloudControllerServiceRepository) GetServiceOfferingsForSpace(spaceGUID string) (models.ServiceOfferings, error) {
return repo.getServiceOfferings(fmt.Sprintf("/v2/spaces/%s/services", spaceGUID))
}
func (repo CloudControllerServiceRepository) FindServiceOfferingsForSpaceByLabel(spaceGUID, name string) (offerings models.ServiceOfferings, err error) {
offerings, err = repo.getServiceOfferings(fmt.Sprintf("/v2/spaces/%s/services?q=%s", spaceGUID, url.QueryEscape("label:"+name)))
if httpErr, ok := err.(errors.HTTPError); ok && httpErr.ErrorCode() == errors.BadQueryParameter {
offerings, err = repo.findServiceOfferingsByPaginating(spaceGUID, name)
}
if err == nil && len(offerings) == 0 {
err = errors.NewModelNotFoundError("Service offering", name)
}
return
}
func (repo CloudControllerServiceRepository) findServiceOfferingsByPaginating(spaceGUID, label string) (offerings models.ServiceOfferings, apiErr error) {
offerings, apiErr = repo.GetServiceOfferingsForSpace(spaceGUID)
if apiErr != nil {
return
}
matchingOffering := models.ServiceOfferings{}
for _, offering := range offerings {
if offering.Label == label {
matchingOffering = append(matchingOffering, offering)
}
}
return matchingOffering, nil
}
func (repo CloudControllerServiceRepository) GetAllServiceOfferings() (models.ServiceOfferings, error) {
return repo.getServiceOfferings("/v2/services")
}
func (repo CloudControllerServiceRepository) getServiceOfferings(path string) ([]models.ServiceOffering, error) {
var offerings []models.ServiceOffering
apiErr := repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
path,
resources.ServiceOfferingResource{},
func(resource interface{}) bool {
if so, ok := resource.(resources.ServiceOfferingResource); ok {
offerings = append(offerings, so.ToModel())
}
return true
})
return offerings, apiErr
}
func (repo CloudControllerServiceRepository) FindInstanceByName(name string) (instance models.ServiceInstance, apiErr error) {
path := fmt.Sprintf("%s/v2/spaces/%s/service_instances?return_user_provided_service_instances=true&q=%s&inline-relations-depth=1", repo.config.APIEndpoint(), repo.config.SpaceFields().GUID, url.QueryEscape("name:"+name))
responseJSON := new(resources.PaginatedServiceInstanceResources)
apiErr = repo.gateway.GetResource(path, responseJSON)
if apiErr != nil {
return
}
if len(responseJSON.Resources) == 0 {
apiErr = errors.NewModelNotFoundError("Service instance", name)
return
}
instanceResource := responseJSON.Resources[0]
instance = instanceResource.ToModel()
if instanceResource.Entity.ServicePlan.Metadata.GUID != "" {
resource := &resources.ServiceOfferingResource{}
path = fmt.Sprintf("%s/v2/services/%s", repo.config.APIEndpoint(), instanceResource.Entity.ServicePlan.Entity.ServiceOfferingGUID)
apiErr = repo.gateway.GetResource(path, resource)
instance.ServiceOffering = resource.ToFields()
}
return
}
func (repo CloudControllerServiceRepository) CreateServiceInstance(name, planGUID string, params map[string]interface{}, tags []string) (err error) {
path := "/v2/service_instances?accepts_incomplete=true"
request := models.ServiceInstanceCreateRequest{
Name: name,
PlanGUID: planGUID,
SpaceGUID: repo.config.SpaceFields().GUID,
Params: params,
Tags: tags,
}
jsonBytes, err := json.Marshal(request)
if err != nil {
return err
}
err = repo.gateway.CreateResource(repo.config.APIEndpoint(), path, bytes.NewReader(jsonBytes))
if httpErr, ok := err.(errors.HTTPError); ok && httpErr.ErrorCode() == errors.ServiceInstanceNameTaken {
serviceInstance, findInstanceErr := repo.FindInstanceByName(name)
if findInstanceErr == nil && serviceInstance.ServicePlan.GUID == planGUID {
return errors.NewModelAlreadyExistsError("Service", name)
}
}
return
}
func (repo CloudControllerServiceRepository) UpdateServiceInstance(instanceGUID, planGUID string, params map[string]interface{}, tags []string) (err error) {
path := fmt.Sprintf("/v2/service_instances/%s?accepts_incomplete=true", instanceGUID)
request := models.ServiceInstanceUpdateRequest{
PlanGUID: planGUID,
Params: params,
Tags: tags,
}
jsonBytes, err := json.Marshal(request)
if err != nil {
return err
}
err = repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, bytes.NewReader(jsonBytes))
return
}
func (repo CloudControllerServiceRepository) RenameService(instance models.ServiceInstance, newName string) (apiErr error) {
body := fmt.Sprintf(`{"name":"%s"}`, newName)
path := fmt.Sprintf("/v2/service_instances/%s?accepts_incomplete=true", instance.GUID)
if instance.IsUserProvided() {
path = fmt.Sprintf("/v2/user_provided_service_instances/%s", instance.GUID)
}
return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, strings.NewReader(body))
}
func (repo CloudControllerServiceRepository) DeleteService(instance models.ServiceInstance) (apiErr error) {
if len(instance.ServiceBindings) > 0 || len(instance.ServiceKeys) > 0 {
return errors.NewServiceAssociationError()
}
path := fmt.Sprintf("/v2/service_instances/%s?%s", instance.GUID, "accepts_incomplete=true")
return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
}
func (repo CloudControllerServiceRepository) PurgeServiceOffering(offering models.ServiceOffering) error {
url := fmt.Sprintf("/v2/services/%s?purge=true", offering.GUID)
return repo.gateway.DeleteResource(repo.config.APIEndpoint(), url)
}
func (repo CloudControllerServiceRepository) PurgeServiceInstance(instance models.ServiceInstance) error {
url := fmt.Sprintf("/v2/service_instances/%s?purge=true", instance.GUID)
return repo.gateway.DeleteResource(repo.config.APIEndpoint(), url)
}
func (repo CloudControllerServiceRepository) FindServiceOfferingsByLabel(label string) (models.ServiceOfferings, error) {
path := fmt.Sprintf("/v2/services?q=%s", url.QueryEscape("label:"+label))
offerings, apiErr := repo.getServiceOfferings(path)
if apiErr != nil {
return models.ServiceOfferings{}, apiErr
} else if len(offerings) == 0 {
apiErr = errors.NewModelNotFoundError("Service offering", label)
return models.ServiceOfferings{}, apiErr
}
return offerings, apiErr
}
func (repo CloudControllerServiceRepository) FindServiceOfferingByLabelAndProvider(label, provider string) (models.ServiceOffering, error) {
path := fmt.Sprintf("/v2/services?q=%s", url.QueryEscape("label:"+label+";provider:"+provider))
offerings, apiErr := repo.getServiceOfferings(path)
if apiErr != nil {
return models.ServiceOffering{}, apiErr
} else if len(offerings) == 0 {
apiErr = errors.NewModelNotFoundError("Service offering", label+" "+provider)
return models.ServiceOffering{}, apiErr
}
return offerings[0], apiErr
}
func (repo CloudControllerServiceRepository) FindServicePlanByDescription(planDescription resources.ServicePlanDescription) (string, error) {
path := fmt.Sprintf("/v2/services?inline-relations-depth=1&q=%s",
url.QueryEscape("label:"+planDescription.ServiceLabel+";provider:"+planDescription.ServiceProvider))
offerings, err := repo.getServiceOfferings(path)
if err != nil {
return "", err
}
for _, serviceOfferingResource := range offerings {
for _, servicePlanResource := range serviceOfferingResource.Plans {
if servicePlanResource.Name == planDescription.ServicePlanName {
return servicePlanResource.GUID, nil
}
}
}
return "", errors.NewModelNotFoundError("Plan", planDescription.String())
}
func (repo CloudControllerServiceRepository) ListServicesFromManyBrokers(brokerGUIDs []string) ([]models.ServiceOffering, error) {
brokerGUIDsString := strings.Join(brokerGUIDs, ",")
services := []models.ServiceOffering{}
err := repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
fmt.Sprintf("/v2/services?q=%s", url.QueryEscape("service_broker_guid IN "+brokerGUIDsString)),
resources.ServiceOfferingResource{},
func(resource interface{}) bool {
if service, ok := resource.(resources.ServiceOfferingResource); ok {
services = append(services, service.ToModel())
}
return true
})
return services, err
}
func (repo CloudControllerServiceRepository) ListServicesFromBroker(brokerGUID string) (offerings []models.ServiceOffering, err error) {
err = repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
fmt.Sprintf("/v2/services?q=%s", url.QueryEscape("service_broker_guid:"+brokerGUID)),
resources.ServiceOfferingResource{},
func(resource interface{}) bool {
if offering, ok := resource.(resources.ServiceOfferingResource); ok {
offerings = append(offerings, offering.ToModel())
}
return true
})
return
}
func (repo CloudControllerServiceRepository) MigrateServicePlanFromV1ToV2(v1PlanGUID, v2PlanGUID string) (changedCount int, apiErr error) {
path := fmt.Sprintf("/v2/service_plans/%s/service_instances", v1PlanGUID)
body := strings.NewReader(fmt.Sprintf(`{"service_plan_guid":"%s"}`, v2PlanGUID))
response := new(resources.ServiceMigrateV1ToV2Response)
apiErr = repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, body, response)
if apiErr != nil {
return
}
changedCount = response.ChangedCount
return
}
func (repo CloudControllerServiceRepository) GetServiceInstanceCountForServicePlan(v1PlanGUID string) (count int, apiErr error) {
path := fmt.Sprintf("%s/v2/service_plans/%s/service_instances?results-per-page=1", repo.config.APIEndpoint(), v1PlanGUID)
response := new(resources.PaginatedServiceInstanceResources)
apiErr = repo.gateway.GetResource(path, response)
count = response.TotalResults
return
}