-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_key.go
83 lines (70 loc) · 2.46 KB
/
service_key.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
package ccv2
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// ServiceKey represents a Cloud Controller Service Key.
type ServiceKey struct {
// GUID is the unique Service Key identifier.
GUID string
// Name is the name of the service key.
Name string
// ServiceInstanceGUID is the associated service instance GUID.
ServiceInstanceGUID string
// Credentials are the credentials returned by the service broker for the service key.
Credentials map[string]interface{}
}
func (serviceKey *ServiceKey) UnmarshalJSON(data []byte) error {
var ccServiceKey struct {
Metadata internal.Metadata
Entity struct {
ServiceInstanceGUID string `json:"service_instance_guid"`
Name string `json:"name"`
Credentials map[string]interface{} `json:"credentials"`
}
}
err := cloudcontroller.DecodeJSON(data, &ccServiceKey)
if err != nil {
return err
}
serviceKey.GUID = ccServiceKey.Metadata.GUID
serviceKey.Name = ccServiceKey.Entity.Name
serviceKey.ServiceInstanceGUID = ccServiceKey.Entity.ServiceInstanceGUID
serviceKey.Credentials = ccServiceKey.Entity.Credentials
return nil
}
// serviceKeyRequestBody represents the body of the service key create
// request.
type serviceKeyRequestBody struct {
ServiceInstanceGUID string `json:"service_instance_guid"`
Name string `json:"name"`
Parameters map[string]interface{} `json:"parameters,omitempty"`
}
// CreateServiceKey creates a new service key using the provided name and
// parameters for the requested service instance.
func (client *Client) CreateServiceKey(serviceInstanceGUID string, keyName string, parameters map[string]interface{}) (ServiceKey, Warnings, error) {
requestBody := serviceKeyRequestBody{
ServiceInstanceGUID: serviceInstanceGUID,
Name: keyName,
Parameters: parameters,
}
bodyBytes, err := json.Marshal(requestBody)
if err != nil {
return ServiceKey{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostServiceKeyRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return ServiceKey{}, nil, err
}
var serviceKey ServiceKey
response := cloudcontroller.Response{
DecodeJSONResponseInto: &serviceKey,
}
err = client.connection.Make(request, &response)
return serviceKey, response.Warnings, err
}