-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_plan_visibility.go
117 lines (99 loc) · 3.55 KB
/
service_plan_visibility.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
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"
)
// ServicePlanVisibility represents a Cloud Controller Service Plan Visibility.
type ServicePlanVisibility struct {
// GUID is the unique Service Plan Visibility identifier.
GUID string
// ServicePlanGUID of the associated Service Plan.
ServicePlanGUID string
// OrganizationGUID of the associated Organization.
OrganizationGUID string
}
// UnmarshalJSON helps unmarshal a Cloud Controller Service Plan Visibilities
// response.
func (servicePlanVisibility *ServicePlanVisibility) UnmarshalJSON(data []byte) error {
var ccServicePlanVisibility struct {
Metadata internal.Metadata
Entity struct {
ServicePlanGUID string `json:"service_plan_guid"`
OrganizationGUID string `json:"organization_guid"`
} `json:"entity"`
}
err := cloudcontroller.DecodeJSON(data, &ccServicePlanVisibility)
if err != nil {
return err
}
servicePlanVisibility.GUID = ccServicePlanVisibility.Metadata.GUID
servicePlanVisibility.ServicePlanGUID = ccServicePlanVisibility.Entity.ServicePlanGUID
servicePlanVisibility.OrganizationGUID = ccServicePlanVisibility.Entity.OrganizationGUID
return nil
}
type createServicePlanRequestBody struct {
ServicePlanGUID string `json:"service_plan_guid"`
OrganizationGUID string `json:"organization_guid"`
}
func (client *Client) CreateServicePlanVisibility(planGUID string, orgGUID string) (ServicePlanVisibility, Warnings, error) {
requestBody := createServicePlanRequestBody{
ServicePlanGUID: planGUID,
OrganizationGUID: orgGUID,
}
bodyBytes, err := json.Marshal(requestBody)
if err != nil {
return ServicePlanVisibility{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostServicePlanVisibilityRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return ServicePlanVisibility{}, nil, err
}
var servicePlanVisibility ServicePlanVisibility
response := cloudcontroller.Response{
DecodeJSONResponseInto: &servicePlanVisibility,
}
err = client.connection.Make(request, &response)
return servicePlanVisibility, response.Warnings, err
}
func (client *Client) DeleteServicePlanVisibility(servicePlanVisibilityGUID string) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteServicePlanVisibilityRequest,
URIParams: Params{"service_plan_visibility_guid": servicePlanVisibilityGUID},
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}
// GetServicePlanVisibilities returns back a list of Service Plan Visibilities
// given the provided filters.
func (client *Client) GetServicePlanVisibilities(filters ...Filter) ([]ServicePlanVisibility, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServicePlanVisibilitiesRequest,
Query: ConvertFilterParameters(filters),
})
if err != nil {
return nil, nil, err
}
var fullVisibilityList []ServicePlanVisibility
warnings, err := client.paginate(request, ServicePlanVisibility{}, func(item interface{}) error {
if vis, ok := item.(ServicePlanVisibility); ok {
fullVisibilityList = append(fullVisibilityList, vis)
} else {
return ccerror.UnknownObjectInListError{
Expected: ServicePlanVisibility{},
Unexpected: item,
}
}
return nil
})
return fullVisibilityList, warnings, err
}