forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_plan.go
174 lines (137 loc) · 5.1 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
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
package ccv3
import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
"code.cloudfoundry.org/cli/resources"
"code.cloudfoundry.org/cli/util/lookuptable"
)
func (client *Client) GetServicePlanByGUID(guid string) (resources.ServicePlan, Warnings, error) {
if guid == "" {
return resources.ServicePlan{}, nil, ccerror.ServicePlanNotFound{}
}
var result resources.ServicePlan
_, warnings, err := client.MakeRequest(RequestParams{
RequestName: internal.GetServicePlanRequest,
URIParams: internal.Params{"service_plan_guid": guid},
ResponseBody: &result,
})
return result, warnings, err
}
// GetServicePlans lists service plan with optional filters.
func (client *Client) GetServicePlans(query ...Query) ([]resources.ServicePlan, Warnings, error) {
plans, _, warnings, err := client.getServicePlans(query...)
return plans, warnings, err
}
func (client *Client) getServicePlans(query ...Query) ([]resources.ServicePlan, IncludedResources, Warnings, error) {
var plans []resources.ServicePlan
included, warnings, err := client.MakeListRequest(RequestParams{
RequestName: internal.GetServicePlansRequest,
Query: query,
ResponseBody: resources.ServicePlan{},
AppendToList: func(item interface{}) error {
plans = append(plans, item.(resources.ServicePlan))
return nil
},
})
return plans, included, warnings, err
}
type ServicePlanWithSpaceAndOrganization struct {
// GUID is a unique service plan identifier.
GUID string
// Name is the name of the service plan.
Name string
// VisibilityType can be "public", "admin", "organization" or "space"
VisibilityType resources.ServicePlanVisibilityType
// ServicePlanGUID is the GUID of the service offering
ServiceOfferingGUID string
SpaceGUID string
SpaceName string
OrganizationName string
}
type planSpaceDetails struct{ spaceName, orgName string }
func (client *Client) GetServicePlansWithSpaceAndOrganization(query ...Query) ([]ServicePlanWithSpaceAndOrganization, Warnings, error) {
query = append(query, Query{
Key: Include,
Values: []string{"space.organization"},
})
plans, included, warnings, err := client.getServicePlans(query...)
spaceDetailsFromGUID := computeSpaceDetailsTable(included)
var enrichedPlans []ServicePlanWithSpaceAndOrganization
for _, plan := range plans {
sd := spaceDetailsFromGUID[plan.SpaceGUID]
enrichedPlans = append(enrichedPlans, ServicePlanWithSpaceAndOrganization{
GUID: plan.GUID,
Name: plan.Name,
VisibilityType: plan.VisibilityType,
ServiceOfferingGUID: plan.ServiceOfferingGUID,
SpaceGUID: plan.SpaceGUID,
SpaceName: sd.spaceName,
OrganizationName: sd.orgName,
})
}
return enrichedPlans, warnings, err
}
type ServiceOfferingWithPlans struct {
// GUID is a unique service offering identifier.
GUID string
// Name is the name of the service offering.
Name string
// Description of the service offering
Description string
// ServiceBrokerName is the name of the service broker
ServiceBrokerName string
// List of service plans that this service offering provides
Plans []resources.ServicePlan
}
func (client *Client) GetServicePlansWithOfferings(query ...Query) ([]ServiceOfferingWithPlans, Warnings, error) {
query = append(query,
Query{
Key: Include,
Values: []string{"service_offering"},
},
Query{
Key: FieldsServiceOfferingServiceBroker,
Values: []string{"name,guid"},
},
)
plans, included, warnings, err := client.getServicePlans(query...)
if err != nil {
return nil, warnings, err
}
var offeringsWithPlans []ServiceOfferingWithPlans
offeringGUIDLookup := make(map[string]int)
indexOfOffering := func(serviceOfferingGUID string) int {
if i, ok := offeringGUIDLookup[serviceOfferingGUID]; ok {
return i
}
i := len(offeringsWithPlans)
offeringGUIDLookup[serviceOfferingGUID] = i
offeringsWithPlans = append(offeringsWithPlans, ServiceOfferingWithPlans{GUID: serviceOfferingGUID})
return i
}
brokerNameLookup := lookuptable.NameFromGUID(included.ServiceBrokers)
for _, p := range plans {
i := indexOfOffering(p.ServiceOfferingGUID)
offeringsWithPlans[i].Plans = append(offeringsWithPlans[i].Plans, p)
}
for _, o := range included.ServiceOfferings {
i := indexOfOffering(o.GUID)
offeringsWithPlans[i].Name = o.Name
offeringsWithPlans[i].Description = o.Description
offeringsWithPlans[i].ServiceBrokerName = brokerNameLookup[o.ServiceBrokerGUID]
}
return offeringsWithPlans, warnings, nil
}
func computeSpaceDetailsTable(included IncludedResources) map[string]planSpaceDetails {
orgNameFromGUID := lookuptable.NameFromGUID(included.Organizations)
spaceDetailsFromGUID := make(map[string]planSpaceDetails)
for _, space := range included.Spaces {
details := planSpaceDetails{spaceName: space.Name}
if orgRelationship, ok := space.Relationships[constant.RelationshipTypeOrganization]; ok {
details.orgName = orgNameFromGUID[orgRelationship.GUID]
}
spaceDetailsFromGUID[space.GUID] = details
}
return spaceDetailsFromGUID
}