forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_plan_visibilities.go
163 lines (148 loc) · 5.93 KB
/
service_plan_visibilities.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
package cfclient
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type ServicePlanVisibilitiesResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []ServicePlanVisibilityResource `json:"resources"`
}
type ServicePlanVisibilityResource struct {
Meta Meta `json:"metadata"`
Entity ServicePlanVisibility `json:"entity"`
}
type ServicePlanVisibility struct {
Guid string `json:"guid"`
ServicePlanGuid string `json:"service_plan_guid"`
OrganizationGuid string `json:"organization_guid"`
ServicePlanUrl string `json:"service_plan_url"`
OrganizationUrl string `json:"organization_url"`
c *Client
}
func (c *Client) ListServicePlanVisibilitiesByQuery(query url.Values) ([]ServicePlanVisibility, error) {
var servicePlanVisibilities []ServicePlanVisibility
requestUrl := "/v2/service_plan_visibilities?" + query.Encode()
for {
var servicePlanVisibilitiesResp ServicePlanVisibilitiesResponse
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting service plan visibilities")
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading service plan visibilities request:")
}
err = json.Unmarshal(resBody, &servicePlanVisibilitiesResp)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshaling service plan visibilities")
}
for _, servicePlanVisibility := range servicePlanVisibilitiesResp.Resources {
servicePlanVisibility.Entity.Guid = servicePlanVisibility.Meta.Guid
servicePlanVisibility.Entity.c = c
servicePlanVisibilities = append(servicePlanVisibilities, servicePlanVisibility.Entity)
}
requestUrl = servicePlanVisibilitiesResp.NextUrl
if requestUrl == "" {
break
}
}
return servicePlanVisibilities, nil
}
func (c *Client) ListServicePlanVisibilities() ([]ServicePlanVisibility, error) {
return c.ListServicePlanVisibilitiesByQuery(nil)
}
func (c *Client) GetServicePlanVisibilityByGuid(guid string) (ServicePlanVisibility, error) {
r := c.NewRequest("GET", "/v2/service_plan_visibilities/"+guid)
resp, err := c.DoRequest(r)
if err != nil {
return ServicePlanVisibility{}, err
}
return respBodyToServicePlanVisibility(resp.Body, c)
}
//a uniqueID is the id of the service in the catalog and not in cf internal db
func (c *Client) CreateServicePlanVisibilityByUniqueId(uniqueId string, organizationGuid string) (ServicePlanVisibility, error) {
q := url.Values{}
q.Set("q", fmt.Sprintf("unique_id:%s", uniqueId))
plans, err := c.ListServicePlansByQuery(q)
if err != nil {
return ServicePlanVisibility{}, errors.Wrap(err, fmt.Sprintf("Couldn't find a service plan with unique_id: %s", uniqueId))
}
return c.CreateServicePlanVisibility(plans[0].Guid, organizationGuid)
}
func (c *Client) CreateServicePlanVisibility(servicePlanGuid string, organizationGuid string) (ServicePlanVisibility, error) {
req := c.NewRequest("POST", "/v2/service_plan_visibilities")
req.obj = map[string]interface{}{
"service_plan_guid": servicePlanGuid,
"organization_guid": organizationGuid,
}
resp, err := c.DoRequest(req)
if err != nil {
return ServicePlanVisibility{}, err
}
if resp.StatusCode != http.StatusCreated {
return ServicePlanVisibility{}, errors.Wrapf(err, "Error creating service plan visibility, response code: %d", resp.StatusCode)
}
return respBodyToServicePlanVisibility(resp.Body, c)
}
func (c *Client) DeleteServicePlanVisibilityByPlanAndOrg(servicePlanGuid string, organizationGuid string, async bool) error {
q := url.Values{}
q.Set("q", fmt.Sprintf("organization_guid:%s;service_plan_guid:%s", organizationGuid, servicePlanGuid))
plans, err := c.ListServicePlanVisibilitiesByQuery(q)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Couldn't find a service plan visibility for service plan %s and org %s", servicePlanGuid, organizationGuid))
}
if len(plans) != 1 {
return fmt.Errorf("Query for a service plan visibility did not return exactly one result when searching for a service plan visibility for service plan %s and org %s",
servicePlanGuid, organizationGuid)
}
return c.DeleteServicePlanVisibility(plans[0].Guid, async)
}
func (c *Client) DeleteServicePlanVisibility(guid string, async bool) error {
req := c.NewRequest("DELETE", fmt.Sprintf("/v2/service_plan_visibilities/%s?async=%v", guid, async))
resp, err := c.DoRequest(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error deleting service plan visibility, response code: %d", resp.StatusCode)
}
return nil
}
func (c *Client) UpdateServicePlanVisibility(guid string, servicePlanGuid string, organizationGuid string) (ServicePlanVisibility, error) {
req := c.NewRequest("PUT", "/v2/service_plan_visibilities/"+guid)
req.obj = map[string]interface{}{
"service_plan_guid": servicePlanGuid,
"organization_guid": organizationGuid,
}
resp, err := c.DoRequest(req)
if err != nil {
return ServicePlanVisibility{}, err
}
if resp.StatusCode != http.StatusCreated {
return ServicePlanVisibility{}, errors.Wrapf(err, "Error updating service plan visibility, response code: %d", resp.StatusCode)
}
return respBodyToServicePlanVisibility(resp.Body, c)
}
func respBodyToServicePlanVisibility(body io.ReadCloser, c *Client) (ServicePlanVisibility, error) {
bodyRaw, err := ioutil.ReadAll(body)
if err != nil {
return ServicePlanVisibility{}, err
}
servicePlanVisibilityRes := ServicePlanVisibilityResource{}
err = json.Unmarshal(bodyRaw, &servicePlanVisibilityRes)
if err != nil {
return ServicePlanVisibility{}, err
}
servicePlanVisibility := servicePlanVisibilityRes.Entity
servicePlanVisibility.Guid = servicePlanVisibilityRes.Meta.Guid
servicePlanVisibility.c = c
return servicePlanVisibility, nil
}