forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_instances.go
186 lines (160 loc) · 6.12 KB
/
service_instances.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
package cfclient
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type ServiceInstancesResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []ServiceInstanceResource `json:"resources"`
}
type ServiceInstanceRequest struct {
Name string `json:"name"`
SpaceGuid string `json:"space_guid"`
ServicePlanGuid string `json:"service_plan_guid"`
Parameters map[string]interface{} `json:"parameters,omitempty"`
Tags []string `json:"tags,omitempty"`
}
type ServiceInstanceResource struct {
Meta Meta `json:"metadata"`
Entity ServiceInstance `json:"entity"`
}
type ServiceInstance struct {
Name string `json:"name"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Credentials map[string]interface{} `json:"credentials"`
ServicePlanGuid string `json:"service_plan_guid"`
SpaceGuid string `json:"space_guid"`
DashboardUrl string `json:"dashboard_url"`
Type string `json:"type"`
LastOperation LastOperation `json:"last_operation"`
Tags []string `json:"tags"`
ServiceGuid string `json:"service_guid"`
SpaceUrl string `json:"space_url"`
ServicePlanUrl string `json:"service_plan_url"`
ServiceBindingsUrl string `json:"service_bindings_url"`
ServiceKeysUrl string `json:"service_keys_url"`
RoutesUrl string `json:"routes_url"`
ServiceUrl string `json:"service_url"`
Guid string `json:"guid"`
c *Client
}
type LastOperation struct {
Type string `json:"type"`
State string `json:"state"`
Description string `json:"description"`
UpdatedAt string `json:"updated_at"`
CreatedAt string `json:"created_at"`
}
func (c *Client) ListServiceInstancesByQuery(query url.Values) ([]ServiceInstance, error) {
var instances []ServiceInstance
requestUrl := "/v2/service_instances?" + query.Encode()
for {
var sir ServiceInstancesResponse
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting service instances")
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading service instances request:")
}
err = json.Unmarshal(resBody, &sir)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshaling service instances")
}
for _, instance := range sir.Resources {
instances = append(instances, c.mergeServiceInstance(instance))
}
requestUrl = sir.NextUrl
if requestUrl == "" {
break
}
}
return instances, nil
}
func (c *Client) ListServiceInstances() ([]ServiceInstance, error) {
return c.ListServiceInstancesByQuery(nil)
}
func (c *Client) GetServiceInstanceByGuid(guid string) (ServiceInstance, error) {
var sir ServiceInstanceResource
req := c.NewRequest("GET", "/v2/service_instances/"+guid)
res, err := c.DoRequest(req)
if err != nil {
return ServiceInstance{}, errors.Wrap(err, "Error requesting service instance")
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return ServiceInstance{}, errors.Wrap(err, "Error reading service instance response")
}
err = json.Unmarshal(data, &sir)
if err != nil {
return ServiceInstance{}, errors.Wrap(err, "Error JSON parsing service instance response")
}
return c.mergeServiceInstance(sir), nil
}
func (c *Client) ServiceInstanceByGuid(guid string) (ServiceInstance, error) {
return c.GetServiceInstanceByGuid(guid)
}
func (c *Client) mergeServiceInstance(instance ServiceInstanceResource) ServiceInstance {
instance.Entity.Guid = instance.Meta.Guid
instance.Entity.CreatedAt = instance.Meta.CreatedAt
instance.Entity.UpdatedAt = instance.Meta.UpdatedAt
instance.Entity.c = c
return instance.Entity
}
func (c *Client) CreateServiceInstance(req ServiceInstanceRequest) (ServiceInstance, error) {
var sir ServiceInstanceResource
buf := bytes.NewBuffer(nil)
err := json.NewEncoder(buf).Encode(req)
if err != nil {
return ServiceInstance{}, err
}
r := c.NewRequestWithBody("POST", "/v2/service_instances?accepts_incomplete=true", buf)
res, err := c.DoRequest(r)
if err != nil {
return ServiceInstance{}, err
}
if res.StatusCode != http.StatusAccepted && res.StatusCode != http.StatusCreated {
return ServiceInstance{}, errors.Wrapf(err, "Error creating service, response code: %d", res.StatusCode)
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return ServiceInstance{}, errors.Wrap(err, "Error reading service instance response")
}
err = json.Unmarshal(data, &sir)
if err != nil {
return ServiceInstance{}, errors.Wrap(err, "Error JSON parsing service instance response")
}
return c.mergeServiceInstance(sir), nil
}
func (c *Client) UpdateServiceInstance(serviceInstanceGuid string, updatedConfiguration io.Reader, async bool) error {
u := fmt.Sprintf("/v2/service_instances/%s?accepts_incomplete=%t", serviceInstanceGuid, async)
resp, err := c.DoRequest(c.NewRequestWithBody("PUT", u, updatedConfiguration))
if err != nil {
return err
}
if resp.StatusCode != http.StatusAccepted {
return errors.Wrapf(err, "Error updating service instance %s, response code %d", serviceInstanceGuid, resp.StatusCode)
}
return nil
}
func (c *Client) DeleteServiceInstance(guid string, recursive, async bool) error {
resp, err := c.DoRequest(c.NewRequest("DELETE", fmt.Sprintf("/v2/service_instances/%s?recursive=%t&accepts_incomplete=%t&async=%t", guid, recursive, async, async)))
if err != nil {
return err
}
if resp.StatusCode != http.StatusAccepted {
return errors.Wrapf(err, "Error deleting service instance %s, response code %d", guid, resp.StatusCode)
}
return nil
}