-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_plan.go
138 lines (114 loc) · 3.74 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
package ccv2
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// ServicePlan represents a predefined set of configurations for a Cloud
// Controller service object.
type ServicePlan struct {
//GUID is the unique identifier of the service plan.
GUID string
// Name is the name of the service plan.
Name string
// ServiceGUID is the unique identifier of the service that the service
// plan belongs to.
ServiceGUID string
// Public is true if plan is accessible to all organizations.
Public bool
// Description of the plan
Description string
// Free is true if plan is free
Free bool
// Information about maintenance available
MaintenanceInfo MaintenanceInfo
}
// UnmarshalJSON helps unmarshal a Cloud Controller Service Plan response.
func (servicePlan *ServicePlan) UnmarshalJSON(data []byte) error {
var ccServicePlan struct {
Metadata internal.Metadata
Entity struct {
Name string `json:"name"`
ServiceGUID string `json:"service_guid"`
Public bool `json:"public"`
Description string `json:"description"`
Free bool `json:"free"`
MaintenanceInfo MaintenanceInfo `json:"maintenance_info"`
}
}
err := cloudcontroller.DecodeJSON(data, &ccServicePlan)
if err != nil {
return err
}
servicePlan.GUID = ccServicePlan.Metadata.GUID
servicePlan.Name = ccServicePlan.Entity.Name
servicePlan.ServiceGUID = ccServicePlan.Entity.ServiceGUID
servicePlan.Public = ccServicePlan.Entity.Public
servicePlan.Description = ccServicePlan.Entity.Description
servicePlan.Free = ccServicePlan.Entity.Free
servicePlan.MaintenanceInfo = ccServicePlan.Entity.MaintenanceInfo
return nil
}
// GetServicePlan returns the service plan with the given GUID.
func (client *Client) GetServicePlan(servicePlanGUID string) (ServicePlan, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServicePlanRequest,
URIParams: Params{"service_plan_guid": servicePlanGUID},
})
if err != nil {
return ServicePlan{}, nil, err
}
var servicePlan ServicePlan
response := cloudcontroller.Response{
DecodeJSONResponseInto: &servicePlan,
}
err = client.connection.Make(request, &response)
return servicePlan, response.Warnings, err
}
func (client *Client) GetServicePlans(filters ...Filter) ([]ServicePlan, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServicePlansRequest,
Query: ConvertFilterParameters(filters),
})
if err != nil {
return nil, nil, err
}
var fullServicePlansList []ServicePlan
warnings, err := client.paginate(request, ServicePlan{}, func(item interface{}) error {
if plan, ok := item.(ServicePlan); ok {
fullServicePlansList = append(fullServicePlansList, plan)
} else {
return ccerror.UnknownObjectInListError{
Expected: ServicePlan{},
Unexpected: item,
}
}
return nil
})
return fullServicePlansList, warnings, err
}
type updateServicePlanRequestBody struct {
Public bool `json:"public"`
}
func (client *Client) UpdateServicePlan(guid string, public bool) (Warnings, error) {
requestBody := updateServicePlanRequestBody{
Public: public,
}
body, err := json.Marshal(requestBody)
if err != nil {
return nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutServicePlanRequest,
Body: bytes.NewReader(body),
URIParams: Params{"service_plan_guid": guid},
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}