forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_binding.go
121 lines (104 loc) · 3.58 KB
/
service_binding.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
package ccv2
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// ServiceBinding represents a Cloud Controller Service Binding.
type ServiceBinding struct {
AppGUID string
GUID string
ServiceInstanceGUID string
}
// UnmarshalJSON helps unmarshal a Cloud Controller Service Binding response.
func (serviceBinding *ServiceBinding) UnmarshalJSON(data []byte) error {
var ccServiceBinding struct {
Metadata internal.Metadata
Entity struct {
AppGUID string `json:"app_guid"`
ServiceInstanceGUID string `json:"service_instance_guid"`
} `json:"entity"`
}
err := json.Unmarshal(data, &ccServiceBinding)
if err != nil {
return err
}
serviceBinding.AppGUID = ccServiceBinding.Entity.AppGUID
serviceBinding.GUID = ccServiceBinding.Metadata.GUID
serviceBinding.ServiceInstanceGUID = ccServiceBinding.Entity.ServiceInstanceGUID
return nil
}
// serviceBindingRequestBody represents the body of the service binding create
// request.
type serviceBindingRequestBody struct {
ServiceInstanceGUID string `json:"service_instance_guid"`
AppGUID string `json:"app_guid"`
Parameters map[string]interface{} `json:"parameters"`
}
// CreateServiceBinding creates a service binding
func (client *Client) CreateServiceBinding(appGUID string, serviceInstanceGUID string, parameters map[string]interface{}) (ServiceBinding, Warnings, error) {
requestBody := serviceBindingRequestBody{
ServiceInstanceGUID: serviceInstanceGUID,
AppGUID: appGUID,
Parameters: parameters,
}
bodyBytes, err := json.Marshal(requestBody)
if err != nil {
return ServiceBinding{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostServiceBindingRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return ServiceBinding{}, nil, err
}
var serviceBinding ServiceBinding
response := cloudcontroller.Response{
Result: &serviceBinding,
}
err = client.connection.Make(request, &response)
if err != nil {
return ServiceBinding{}, response.Warnings, err
}
return serviceBinding, response.Warnings, nil
}
// GetServiceBindings returns back a list of Service Bindings based off of the
// provided queries.
func (client *Client) GetServiceBindings(queries []Query) ([]ServiceBinding, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServiceBindingsRequest,
Query: FormatQueryParameters(queries),
})
if err != nil {
return nil, nil, err
}
var fullBindingsList []ServiceBinding
warnings, err := client.paginate(request, ServiceBinding{}, func(item interface{}) error {
if binding, ok := item.(ServiceBinding); ok {
fullBindingsList = append(fullBindingsList, binding)
} else {
return ccerror.UnknownObjectInListError{
Expected: ServiceBinding{},
Unexpected: item,
}
}
return nil
})
return fullBindingsList, warnings, err
}
// DeleteServiceBinding will destroy the requested Service Binding.
func (client *Client) DeleteServiceBinding(serviceBindingGUID string) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteServiceBindingRequest,
URIParams: map[string]string{"service_binding_guid": serviceBindingGUID},
})
if err != nil {
return nil, err
}
var response cloudcontroller.Response
err = client.connection.Make(request, &response)
return response.Warnings, err
}