forked from cloudfoundry/cli
/
service_binding.go
173 lines (150 loc) · 5.43 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
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
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 {
GUID string
AppGUID 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
}
// GetServiceInstanceServiceBindings returns back a list of Service Bindings for the provided service instance GUID.
func (client *Client) GetServiceInstanceServiceBindings(serviceInstanceGUID string) ([]ServiceBinding, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServiceInstanceServiceBindingsRequest,
URIParams: map[string]string{"service_instance_guid": serviceInstanceGUID},
})
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
}
// GetUserProvidedServiceInstanceServiceBindings returns back a list of Service Bindings for the provided user provided service instance GUID.
func (client *Client) GetUserProvidedServiceInstanceServiceBindings(userProvidedServiceInstanceGUID string) ([]ServiceBinding, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetUserProvidedServiceInstanceServiceBindingsRequest,
URIParams: map[string]string{"user_provided_service_instance_guid": userProvidedServiceInstanceGUID},
})
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
}