forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_brokers.go
207 lines (186 loc) · 5.79 KB
/
service_brokers.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package cfclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type ServiceBrokerResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []ServiceBrokerResource `json:"resources"`
}
type ServiceBrokerResource struct {
Meta Meta `json:"metadata"`
Entity ServiceBroker `json:"entity"`
}
type UpdateServiceBrokerRequest struct {
Name string `json:"name"`
BrokerURL string `json:"broker_url"`
Username string `json:"auth_username"`
Password string `json:"auth_password"`
}
type CreateServiceBrokerRequest struct {
Name string `json:"name"`
BrokerURL string `json:"broker_url"`
Username string `json:"auth_username"`
Password string `json:"auth_password"`
SpaceGUID string `json:"space_guid,omitempty"`
}
type ServiceBroker struct {
Guid string `json:"guid"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
BrokerURL string `json:"broker_url"`
Username string `json:"auth_username"`
Password string `json:"auth_password"`
SpaceGUID string `json:"space_guid,omitempty"`
c *Client
}
func (c *Client) DeleteServiceBroker(guid string) error {
requestUrl := fmt.Sprintf("/v2/service_brokers/%s", guid)
r := c.NewRequest("DELETE", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error deleteing service broker %s, response code: %d", guid, resp.StatusCode)
}
return nil
}
func (c *Client) UpdateServiceBroker(guid string, usb UpdateServiceBrokerRequest) (ServiceBroker, error) {
var serviceBrokerResource ServiceBrokerResource
buf := bytes.NewBuffer(nil)
err := json.NewEncoder(buf).Encode(usb)
if err != nil {
return ServiceBroker{}, err
}
req := c.NewRequestWithBody("PUT", fmt.Sprintf("/v2/service_brokers/%s", guid), buf)
resp, err := c.DoRequest(req)
if err != nil {
return ServiceBroker{}, err
}
if resp.StatusCode != http.StatusOK {
return ServiceBroker{}, fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return ServiceBroker{}, err
}
err = json.Unmarshal(body, &serviceBrokerResource)
if err != nil {
return ServiceBroker{}, err
}
serviceBrokerResource.Entity.Guid = serviceBrokerResource.Meta.Guid
return serviceBrokerResource.Entity, nil
}
func (c *Client) CreateServiceBroker(csb CreateServiceBrokerRequest) (ServiceBroker, error) {
var serviceBrokerResource ServiceBrokerResource
buf := bytes.NewBuffer(nil)
err := json.NewEncoder(buf).Encode(csb)
if err != nil {
return ServiceBroker{}, err
}
req := c.NewRequestWithBody("POST", "/v2/service_brokers", buf)
resp, err := c.DoRequest(req)
if err != nil {
return ServiceBroker{}, err
}
if resp.StatusCode != http.StatusCreated {
return ServiceBroker{}, fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return ServiceBroker{}, err
}
err = json.Unmarshal(body, &serviceBrokerResource)
if err != nil {
return ServiceBroker{}, err
}
serviceBrokerResource.Entity.Guid = serviceBrokerResource.Meta.Guid
return serviceBrokerResource.Entity, nil
}
func (c *Client) ListServiceBrokersByQuery(query url.Values) ([]ServiceBroker, error) {
var sbs []ServiceBroker
requestUrl := "/v2/service_brokers?" + query.Encode()
for {
serviceBrokerResp, err := c.getServiceBrokerResponse(requestUrl)
if err != nil {
return []ServiceBroker{}, err
}
for _, sb := range serviceBrokerResp.Resources {
sb.Entity.Guid = sb.Meta.Guid
sb.Entity.CreatedAt = sb.Meta.CreatedAt
sb.Entity.UpdatedAt = sb.Meta.UpdatedAt
sbs = append(sbs, sb.Entity)
}
requestUrl = serviceBrokerResp.NextUrl
if requestUrl == "" {
break
}
}
return sbs, nil
}
func (c *Client) ListServiceBrokers() ([]ServiceBroker, error) {
return c.ListServiceBrokersByQuery(nil)
}
func (c *Client) GetServiceBrokerByGuid(guid string) (ServiceBroker, error) {
var serviceBrokerRes ServiceBrokerResource
r := c.NewRequest("GET", "/v2/service_brokers/"+guid)
resp, err := c.DoRequest(r)
if err != nil {
return ServiceBroker{}, err
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return ServiceBroker{}, err
}
err = json.Unmarshal(body, &serviceBrokerRes)
if err != nil {
return ServiceBroker{}, err
}
serviceBrokerRes.Entity.Guid = serviceBrokerRes.Meta.Guid
serviceBrokerRes.Entity.CreatedAt = serviceBrokerRes.Meta.CreatedAt
serviceBrokerRes.Entity.UpdatedAt = serviceBrokerRes.Meta.UpdatedAt
return serviceBrokerRes.Entity, nil
}
func (c *Client) GetServiceBrokerByName(name string) (ServiceBroker, error) {
var sb ServiceBroker
q := url.Values{}
q.Set("q", "name:"+name)
sbs, err := c.ListServiceBrokersByQuery(q)
if err != nil {
return sb, err
}
if len(sbs) == 0 {
return sb, fmt.Errorf("Unable to find service broker %s", name)
}
return sbs[0], nil
}
func (c *Client) getServiceBrokerResponse(requestUrl string) (ServiceBrokerResponse, error) {
var serviceBrokerResp ServiceBrokerResponse
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return ServiceBrokerResponse{}, errors.Wrap(err, "Error requesting Service Brokers")
}
resBody, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return ServiceBrokerResponse{}, errors.Wrap(err, "Error reading Service Broker request")
}
err = json.Unmarshal(resBody, &serviceBrokerResp)
if err != nil {
return ServiceBrokerResponse{}, errors.Wrap(err, "Error unmarshalling Service Broker")
}
return serviceBrokerResp, nil
}