forked from cloudfoundry/go-cfclient
/
service_keys.go
143 lines (127 loc) · 4.08 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
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"`
}
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"`
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
var serviceKeysResp ServiceKeysResponse
r := c.NewRequest("GET", "/v2/service_keys?"+query.Encode())
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.Wrap(err, "Error unmarshaling service keys")
}
for _, serviceKey := range serviceKeysResp.Resources {
serviceKey.Entity.Guid = serviceKey.Meta.Guid
serviceKey.Entity.c = c
serviceKeys = append(serviceKeys, serviceKey.Entity)
}
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
}