-
Notifications
You must be signed in to change notification settings - Fork 929
/
service.go
156 lines (126 loc) · 4.82 KB
/
service.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
package v2action
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
)
// Service is representation of a service offering.
type Service ccv2.Service
// Filter is representation of Cloud Controller request query parameters.
type Filter ccv2.Filter
// ServicesWithPlans is an association between a Service and the plans it offers.
type ServicesWithPlans map[Service][]ServicePlan
// GetService fetches a service by GUID.
func (actor Actor) GetService(serviceGUID string) (Service, Warnings, error) {
service, warnings, err := actor.CloudControllerClient.GetService(serviceGUID)
return Service(service), Warnings(warnings), err
}
// GetServiceByNameAndBrokerName returns services based on the name and broker provided.
// If a service broker name is provided, only services for that broker will be returned.
// If there are no services, a ServiceNotFoundError will be returned.
//
// NOTE: this will fail for non-admin users if the broker name is specified, as only
// admin users are able to view the list of service brokers.
func (actor Actor) GetServiceByNameAndBrokerName(serviceName, serviceBrokerName string) (Service, Warnings, error) {
filters := []ccv2.Filter{ccv2.Filter{
Type: constant.LabelFilter,
Operator: constant.EqualOperator,
Values: []string{serviceName},
}}
if serviceBrokerName != "" {
serviceBroker, warnings, err := actor.GetServiceBrokerByName(serviceBrokerName)
if err != nil {
return Service{}, warnings, err
}
brokerFilter := ccv2.Filter{
Type: constant.ServiceBrokerGUIDFilter,
Operator: constant.EqualOperator,
Values: []string{serviceBroker.GUID},
}
filters = append(filters, brokerFilter)
}
services, warnings, err := actor.CloudControllerClient.GetServices(filters...)
if err != nil {
return Service{}, Warnings(warnings), err
}
if len(services) == 0 {
return Service{}, Warnings(warnings), actionerror.ServiceNotFoundError{Name: serviceName}
}
if len(services) > 1 {
return Service{}, Warnings(warnings), actionerror.DuplicateServiceError{Name: serviceName}
}
return Service(services[0]), Warnings(warnings), nil
}
func (actor Actor) getServicesByNameForSpace(serviceName, spaceGUID string) ([]Service, Warnings, error) {
var filters []ccv2.Filter
filters = append(filters, ccv2.Filter{
Type: constant.LabelFilter,
Operator: constant.EqualOperator,
Values: []string{serviceName},
})
services, warnings, err := actor.CloudControllerClient.GetSpaceServices(spaceGUID, filters...)
var result []Service
if err != nil {
return result, Warnings(warnings), err
}
for _, service := range services {
result = append(result, Service(service))
}
return result, Warnings(warnings), nil
}
// GetServicesWithPlans returns a map of Services to ServicePlans for a particular broker.
// A particular service with associated plans from a broker can be fetched by additionally providing
// a service label filter.
func (actor Actor) GetServicesWithPlans(filters ...Filter) (ServicesWithPlans, Warnings, error) {
ccv2Filters := []ccv2.Filter{}
for _, f := range filters {
ccv2Filters = append(ccv2Filters, ccv2.Filter(f))
}
var allWarnings Warnings
services, warnings, err := actor.CloudControllerClient.GetServices(ccv2Filters...)
allWarnings = append(allWarnings, Warnings(warnings)...)
if err != nil {
return nil, allWarnings, err
}
servicesWithPlans := ServicesWithPlans{}
for _, service := range services {
servicePlans, warnings, err := actor.CloudControllerClient.GetServicePlans(ccv2.Filter{
Type: constant.ServiceGUIDFilter,
Operator: constant.EqualOperator,
Values: []string{service.GUID},
})
allWarnings = append(allWarnings, Warnings(warnings)...)
if err != nil {
return nil, allWarnings, err
}
plansToReturn := []ServicePlan{}
for _, plan := range servicePlans {
plansToReturn = append(plansToReturn, ServicePlan(plan))
}
servicesWithPlans[Service(service)] = plansToReturn
}
return servicesWithPlans, allWarnings, nil
}
// ServiceExistsWithName returns true if there is an Organization with the
// provided name, otherwise false.
func (actor Actor) ServiceExistsWithName(serviceName string) (bool, Warnings, error) {
services, warnings, err := actor.CloudControllerClient.GetServices(ccv2.Filter{
Type: constant.LabelFilter,
Operator: constant.EqualOperator,
Values: []string{serviceName},
})
if err != nil {
return false, Warnings(warnings), err
}
if len(services) == 0 {
return false, Warnings(warnings), nil
}
return true, Warnings(warnings), nil
}
func (actor Actor) PurgeServiceOffering(service Service) (Warnings, error) {
warnings, err := actor.CloudControllerClient.DeleteService(service.GUID, true)
if err != nil {
return Warnings(warnings), err
}
return Warnings(warnings), nil
}