forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_plan.go
92 lines (74 loc) · 2.93 KB
/
service_plan.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
package v2action
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
)
type ServicePlan ccv2.ServicePlan
func (actor Actor) GetServicePlan(servicePlanGUID string) (ServicePlan, Warnings, error) {
servicePlan, warnings, err := actor.CloudControllerClient.GetServicePlan(servicePlanGUID)
return ServicePlan(servicePlan), Warnings(warnings), err
}
// GetServicePlansForService returns a list of plans associated with the service and the broker if provided
func (actor Actor) GetServicePlansForService(serviceName, brokerName string) ([]ServicePlan, Warnings, error) {
service, allWarnings, err := actor.GetServiceByNameAndBrokerName(serviceName, brokerName)
if err != nil {
return []ServicePlan{}, allWarnings, err
}
servicePlans, warnings, err := actor.CloudControllerClient.GetServicePlans(ccv2.Filter{
Type: constant.ServiceGUIDFilter,
Operator: constant.EqualOperator,
Values: []string{service.GUID},
})
allWarnings = append(allWarnings, warnings...)
if err != nil {
return []ServicePlan{}, allWarnings, err
}
var plansToReturn []ServicePlan
for _, plan := range servicePlans {
plansToReturn = append(plansToReturn, ServicePlan(plan))
}
return plansToReturn, allWarnings, nil
}
func (actor Actor) getServicePlanForServiceInSpace(servicePlanName, serviceName, spaceGUID, brokerName string) (ServicePlan, Warnings, error) {
services, allWarnings, err := actor.getServicesByNameForSpace(serviceName, spaceGUID)
if err != nil {
return ServicePlan{}, allWarnings, err
}
if len(services) == 0 {
return ServicePlan{}, Warnings(allWarnings), actionerror.ServiceNotFoundError{Name: serviceName}
}
if len(services) > 1 && brokerName == "" {
return ServicePlan{}, Warnings(allWarnings), actionerror.DuplicateServiceError{Name: serviceName}
}
service, err := findServiceByBrokerName(services, serviceName, brokerName)
if err != nil {
return ServicePlan{}, allWarnings, err
}
plans, warnings, err := actor.CloudControllerClient.GetServicePlans(ccv2.Filter{
Type: constant.ServiceGUIDFilter,
Operator: constant.EqualOperator,
Values: []string{service.GUID},
})
allWarnings = append(allWarnings, warnings...)
if err != nil {
return ServicePlan{}, allWarnings, err
}
for _, plan := range plans {
if servicePlanName == plan.Name {
return ServicePlan(plan), allWarnings, err
}
}
return ServicePlan{}, allWarnings, actionerror.ServicePlanNotFoundError{PlanName: servicePlanName, ServiceName: serviceName}
}
func findServiceByBrokerName(services []Service, serviceName, brokerName string) (Service, error) {
if brokerName == "" && len(services) == 1 {
return services[0], nil
}
for _, s := range services {
if s.ServiceBrokerName == brokerName {
return s, nil
}
}
return Service{}, actionerror.ServiceAndBrokerCombinationNotFoundError{ServiceName: serviceName, BrokerName: brokerName}
}