forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_instance_summary.go
292 lines (246 loc) · 11.1 KB
/
service_instance_summary.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
package v2action
import (
"fmt"
"sort"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
"code.cloudfoundry.org/cli/util/sorting"
log "github.com/sirupsen/logrus"
)
type ServiceInstanceShareType string
type LastOperation ccv2.LastOperation
const (
ServiceInstanceIsSharedFrom ServiceInstanceShareType = "SharedFrom"
ServiceInstanceIsSharedTo ServiceInstanceShareType = "SharedTo"
ServiceInstanceIsNotShared ServiceInstanceShareType = "NotShared"
)
type ServiceInstanceSummary struct {
ServiceInstance
ServicePlan ServicePlan
Service Service
ServiceInstanceSharingFeatureFlag bool
ServiceInstanceShareType ServiceInstanceShareType
ServiceInstanceSharedFrom ServiceInstanceSharedFrom
ServiceInstanceSharedTos []ServiceInstanceSharedTo
BoundApplications []BoundApplication
}
func (s ServiceInstanceSummary) IsShareable() bool {
return s.ServiceInstanceSharingFeatureFlag && s.Service.Extra.Shareable
}
func (s ServiceInstanceSummary) IsNotShared() bool {
return s.ServiceInstanceShareType == ServiceInstanceIsNotShared
}
func (s ServiceInstanceSummary) IsSharedFrom() bool {
return s.ServiceInstanceShareType == ServiceInstanceIsSharedFrom
}
func (s ServiceInstanceSummary) IsSharedTo() bool {
return s.ServiceInstanceShareType == ServiceInstanceIsSharedTo
}
type BoundApplication struct {
AppName string
LastOperation LastOperation
ServiceBindingName string
}
func (actor Actor) GetServiceInstanceSummaryByNameAndSpace(name string, spaceGUID string) (ServiceInstanceSummary, Warnings, error) {
serviceInstance, instanceWarnings, err := actor.GetServiceInstanceByNameAndSpace(name, spaceGUID)
allWarnings := Warnings(instanceWarnings)
if err != nil {
return ServiceInstanceSummary{}, allWarnings, err
}
serviceInstanceSummary, warnings, err := actor.getSummaryInfoCompositeForInstance(spaceGUID, serviceInstance, true)
return serviceInstanceSummary, append(allWarnings, warnings...), err
}
func (actor Actor) GetServiceInstancesSummaryBySpace(spaceGUID string) ([]ServiceInstanceSummary, Warnings, error) {
spaceSummary, warnings, err := actor.CloudControllerClient.GetSpaceSummary(spaceGUID)
allWarnings := Warnings(warnings)
if err != nil {
return []ServiceInstanceSummary{}, allWarnings, err
}
services, warnings, err := actor.CloudControllerClient.GetServices()
allWarnings = append(allWarnings, warnings...)
if err != nil {
return []ServiceInstanceSummary{}, allWarnings, err
}
serviceGUIDToBrokerName := map[string]string{}
for _, service := range services {
serviceGUIDToBrokerName[service.GUID] = service.ServiceBrokerName
}
var serviceInstanceSummaries []ServiceInstanceSummary
for _, serviceInstance := range spaceSummary.ServiceInstances {
instanceSummary := ServiceInstanceSummary{}
instanceSummary.Name = serviceInstance.Name
instanceSummary.ServicePlan.Name = serviceInstance.ServicePlan.Name
instanceSummary.Service.Label = serviceInstance.ServicePlan.Service.Label
instanceSummary.LastOperation = serviceInstance.LastOperation
instanceSummary.Service.ServiceBrokerName = serviceGUIDToBrokerName[serviceInstance.ServicePlan.Service.GUID]
instanceSummary.ServiceInstance.Type = discoverServiceInstanceType(serviceInstance)
instanceSummary.BoundApplications = findServiceInstanceBoundApplications(serviceInstance.Name, spaceSummary.Applications)
serviceInstanceSummaries = append(serviceInstanceSummaries, instanceSummary)
}
return serviceInstanceSummaries, allWarnings, nil
}
// getAndSetSharedInformation gets a service instance's shared from or shared to information,
func (actor Actor) getAndSetSharedInformation(summary *ServiceInstanceSummary, spaceGUID string) (Warnings, error) {
var (
warnings Warnings
err error
)
// Part of determining if a service instance is shareable, we need to find
// out if the service_instance_sharing feature flag is enabled
featureFlags, featureFlagsWarnings, featureFlagsErr := actor.CloudControllerClient.GetConfigFeatureFlags()
allWarnings := Warnings(featureFlagsWarnings)
if featureFlagsErr != nil {
return allWarnings, featureFlagsErr
}
for _, flag := range featureFlags {
if flag.Name == string(constant.FeatureFlagServiceInstanceSharing) {
summary.ServiceInstanceSharingFeatureFlag = flag.Enabled
}
}
// Service instance is shared from if:
// 1. the source space of the service instance is empty (API returns json null)
// 2. the targeted space is not the same as the source space of the service instance AND
// we call the shared_from url and it returns a non-empty resource
if summary.ServiceInstance.SpaceGUID == "" || summary.ServiceInstance.SpaceGUID != spaceGUID {
summary.ServiceInstanceSharedFrom, warnings, err = actor.GetServiceInstanceSharedFromByServiceInstance(summary.ServiceInstance.GUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
// if the API version does not support service instance sharing, ignore the 404
if _, ok := err.(ccerror.ResourceNotFoundError); !ok {
return allWarnings, err
}
}
if summary.ServiceInstanceSharedFrom.SpaceGUID != "" {
summary.ServiceInstanceShareType = ServiceInstanceIsSharedFrom
} else {
summary.ServiceInstanceShareType = ServiceInstanceIsNotShared
}
return allWarnings, nil
}
// Service instance is shared to if:
// the targeted space is the same as the source space of the service instance AND
// we call the shared_to url and get a non-empty list
summary.ServiceInstanceSharedTos, warnings, err = actor.GetServiceInstanceSharedTosByServiceInstance(summary.ServiceInstance.GUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
// if the API version does not support service instance sharing, ignore the 404
if _, ok := err.(ccerror.ResourceNotFoundError); !ok {
return allWarnings, err
}
}
if len(summary.ServiceInstanceSharedTos) > 0 {
summary.ServiceInstanceShareType = ServiceInstanceIsSharedTo
} else {
summary.ServiceInstanceShareType = ServiceInstanceIsNotShared
}
return allWarnings, nil
}
func (actor Actor) getSummaryInfoCompositeForInstance(spaceGUID string, serviceInstance ServiceInstance, retrieveSharedInfo bool) (ServiceInstanceSummary, Warnings, error) {
log.WithField("GUID", serviceInstance.GUID).Info("looking up service instance info")
serviceInstanceSummary := ServiceInstanceSummary{ServiceInstance: serviceInstance}
var (
serviceBindings []ServiceBinding
allWarnings Warnings
)
if serviceInstance.IsManaged() {
log.Debug("service is managed")
if retrieveSharedInfo {
sharedWarnings, err := actor.getAndSetSharedInformation(&serviceInstanceSummary, spaceGUID)
allWarnings = Warnings(sharedWarnings)
if err != nil {
log.WithField("GUID", serviceInstance.GUID).Errorln("looking up share info:", err)
return serviceInstanceSummary, allWarnings, err
}
}
servicePlan, planWarnings, err := actor.GetServicePlan(serviceInstance.ServicePlanGUID)
allWarnings = append(allWarnings, planWarnings...)
if err != nil {
log.WithField("service_plan_guid", serviceInstance.ServicePlanGUID).Errorln("looking up service plan:", err)
if _, ok := err.(ccerror.ForbiddenError); !ok {
return serviceInstanceSummary, allWarnings, err
}
log.Warning("Forbidden Error - ignoring and continue")
allWarnings = append(allWarnings, fmt.Sprintf("This org is not authorized to view necessary data about this service plan. Contact your administrator regarding service GUID %s.", serviceInstance.ServicePlanGUID))
}
serviceInstanceSummary.ServicePlan = servicePlan
service, serviceWarnings, err := actor.GetService(serviceInstance.ServiceGUID)
allWarnings = append(allWarnings, serviceWarnings...)
if err != nil {
log.WithField("service_guid", serviceInstance.ServiceGUID).Errorln("looking up service:", err)
if _, ok := err.(ccerror.ForbiddenError); !ok {
return serviceInstanceSummary, allWarnings, err
}
log.Warning("Forbidden Error - ignoring and continue")
allWarnings = append(allWarnings, fmt.Sprintf("This org is not authorized to view necessary data about this service. Contact your administrator regarding service GUID %s.", serviceInstance.ServiceGUID))
}
serviceInstanceSummary.Service = service
var bindingsWarnings Warnings
serviceBindings, bindingsWarnings, err = actor.GetServiceBindingsByServiceInstance(serviceInstance.GUID)
allWarnings = append(allWarnings, bindingsWarnings...)
if err != nil {
log.WithField("GUID", serviceInstance.GUID).Errorln("looking up service binding:", err)
return serviceInstanceSummary, allWarnings, err
}
} else {
log.Debug("service is user provided")
var bindingsWarnings Warnings
var err error
serviceBindings, bindingsWarnings, err = actor.GetServiceBindingsByUserProvidedServiceInstance(serviceInstance.GUID)
allWarnings = append(allWarnings, bindingsWarnings...)
if err != nil {
log.WithField("service_instance_guid", serviceInstance.GUID).Errorln("looking up service bindings:", err)
return serviceInstanceSummary, allWarnings, err
}
}
for _, serviceBinding := range serviceBindings {
log.WithFields(log.Fields{
"app_guid": serviceBinding.AppGUID,
"service_binding_guid": serviceBinding.GUID,
}).Debug("application lookup")
app, appWarnings, err := actor.GetApplication(serviceBinding.AppGUID)
allWarnings = append(allWarnings, appWarnings...)
if err != nil {
log.WithFields(log.Fields{
"app_guid": serviceBinding.AppGUID,
"service_binding_guid": serviceBinding.GUID,
}).Errorln("looking up application:", err)
return serviceInstanceSummary, allWarnings, err
}
serviceInstanceSummary.BoundApplications = append(
serviceInstanceSummary.BoundApplications,
BoundApplication{
AppName: app.Name,
ServiceBindingName: serviceBinding.Name,
LastOperation: LastOperation(serviceBinding.LastOperation),
})
}
sort.Slice(
serviceInstanceSummary.BoundApplications,
func(i, j int) bool {
return sorting.LessIgnoreCase(serviceInstanceSummary.BoundApplications[i].AppName, serviceInstanceSummary.BoundApplications[j].AppName)
})
return serviceInstanceSummary, allWarnings, nil
}
func findServiceInstanceBoundApplications(instanceName string, spaceApps []ccv2.SpaceSummaryApplication) []BoundApplication {
var applicationNames []BoundApplication
for _, app := range spaceApps {
for _, name := range app.ServiceNames {
if name == instanceName {
applicationNames = append(applicationNames, BoundApplication{AppName: app.Name})
}
}
}
return applicationNames
}
func discoverServiceInstanceType(instance ccv2.SpaceSummaryServiceInstance) constant.ServiceInstanceType {
if isUserProvided(instance) {
return constant.UserProvidedService
}
return constant.ManagedService
}
func isUserProvided(instance ccv2.SpaceSummaryServiceInstance) bool {
// For now we rely on empty service_plan guid to decide if the instance is user provided or managed
// This could be improved in future if we add the Type of the service instnace to the summary endpoint result body
return instance.ServicePlan.GUID == ""
}