forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_keys.go
171 lines (150 loc) · 4.9 KB
/
service_keys.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
package cfclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type ServiceKeysResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
Resources []ServiceKeyResource `json:"resources"`
NextUrl string `json:"next_url"`
}
type ServiceKeyResource struct {
Meta Meta `json:"metadata"`
Entity ServiceKey `json:"entity"`
}
type CreateServiceKeyRequest struct {
Name string `json:"name"`
ServiceInstanceGuid string `json:"service_instance_guid"`
Parameters interface{} `json:"parameters,omitempty"`
}
type ServiceKey struct {
Name string `json:"name"`
Guid string `json:"guid"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ServiceInstanceGuid string `json:"service_instance_guid"`
Credentials interface{} `json:"credentials"`
ServiceInstanceUrl string `json:"service_instance_url"`
c *Client
}
func (c *Client) ListServiceKeysByQuery(query url.Values) ([]ServiceKey, error) {
var serviceKeys []ServiceKey
requestUrl := "/v2/service_keys?" + query.Encode()
for {
var serviceKeysResp ServiceKeysResponse
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting service keys")
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading service keys request:")
}
err = json.Unmarshal(resBody, &serviceKeysResp)
if err != nil {
return nil, errors.Wrapf(err, "Error unmarshaling service keys: %q", string(resBody))
}
for _, serviceKey := range serviceKeysResp.Resources {
serviceKey.Entity.Guid = serviceKey.Meta.Guid
serviceKey.Entity.CreatedAt = serviceKey.Meta.CreatedAt
serviceKey.Entity.UpdatedAt = serviceKey.Meta.UpdatedAt
serviceKey.Entity.c = c
serviceKeys = append(serviceKeys, serviceKey.Entity)
}
requestUrl = serviceKeysResp.NextUrl
if requestUrl == "" {
break
}
}
return serviceKeys, nil
}
func (c *Client) ListServiceKeys() ([]ServiceKey, error) {
return c.ListServiceKeysByQuery(nil)
}
func (c *Client) GetServiceKeyByName(name string) (ServiceKey, error) {
var serviceKey ServiceKey
q := url.Values{}
q.Set("q", "name:"+name)
serviceKeys, err := c.ListServiceKeysByQuery(q)
if err != nil {
return serviceKey, err
}
if len(serviceKeys) == 0 {
return serviceKey, fmt.Errorf("Unable to find service key %s", name)
}
return serviceKeys[0], nil
}
// GetServiceKeyByInstanceGuid is deprecated in favor of GetServiceKeysByInstanceGuid
func (c *Client) GetServiceKeyByInstanceGuid(guid string) (ServiceKey, error) {
var serviceKey ServiceKey
q := url.Values{}
q.Set("q", "service_instance_guid:"+guid)
serviceKeys, err := c.ListServiceKeysByQuery(q)
if err != nil {
return serviceKey, err
}
if len(serviceKeys) == 0 {
return serviceKey, fmt.Errorf("Unable to find service key for guid %s", guid)
}
return serviceKeys[0], nil
}
// GetServiceKeysByInstanceGuid returns the service keys for a service instance.
// If none are found, it returns an error.
func (c *Client) GetServiceKeysByInstanceGuid(guid string) ([]ServiceKey, error) {
q := url.Values{}
q.Set("q", "service_instance_guid:"+guid)
serviceKeys, err := c.ListServiceKeysByQuery(q)
if err != nil {
return serviceKeys, err
}
if len(serviceKeys) == 0 {
return serviceKeys, fmt.Errorf("Unable to find service key for guid %s", guid)
}
return serviceKeys, nil
}
// CreateServiceKey creates a service key from the request. If a service key
// exists already, it returns an error containing `CF-ServiceKeyNameTaken`
func (c *Client) CreateServiceKey(csr CreateServiceKeyRequest) (ServiceKey, error) {
var serviceKeyResource ServiceKeyResource
buf := bytes.NewBuffer(nil)
err := json.NewEncoder(buf).Encode(csr)
if err != nil {
return ServiceKey{}, err
}
req := c.NewRequestWithBody("POST", "/v2/service_keys", buf)
resp, err := c.DoRequest(req)
if err != nil {
return ServiceKey{}, err
}
if resp.StatusCode != http.StatusCreated {
return ServiceKey{}, 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 ServiceKey{}, err
}
err = json.Unmarshal(body, &serviceKeyResource)
if err != nil {
return ServiceKey{}, err
}
return serviceKeyResource.Entity, nil
}
// DeleteServiceKey removes a service key instance
func (c *Client) DeleteServiceKey(guid string) error {
resp, err := c.DoRequest(c.NewRequest("DELETE", fmt.Sprintf("/v2/service_keys/%s", guid)))
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error deleting service instance key %s, response code %d", guid, resp.StatusCode)
}
return nil
}