forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_binding.go
216 lines (189 loc) · 7.04 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package ccv2
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"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 is the associated application GUID.
AppGUID string
// GUID is the unique Service Binding identifier.
GUID string
// LastOperation
LastOperation LastOperation
// Name is the name of the service binding
Name string
// ServiceInstanceGUID is the associated service GUID.
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"`
Name string `json:"name"`
LastOperation LastOperation `json:"last_operation"`
} `json:"entity"`
}
err := cloudcontroller.DecodeJSON(data, &ccServiceBinding)
if err != nil {
return err
}
serviceBinding.AppGUID = ccServiceBinding.Entity.AppGUID
serviceBinding.GUID = ccServiceBinding.Metadata.GUID
serviceBinding.ServiceInstanceGUID = ccServiceBinding.Entity.ServiceInstanceGUID
serviceBinding.Name = ccServiceBinding.Entity.Name
serviceBinding.LastOperation = ccServiceBinding.Entity.LastOperation
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"`
Name string `json:"name,omitempty"`
Parameters map[string]interface{} `json:"parameters"`
}
func (client *Client) CreateServiceBinding(appGUID string, serviceInstanceGUID string, bindingName string, acceptsIncomplete bool, parameters map[string]interface{}) (ServiceBinding, Warnings, error) {
requestBody := serviceBindingRequestBody{
ServiceInstanceGUID: serviceInstanceGUID,
AppGUID: appGUID,
Name: bindingName,
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),
Query: url.Values{"accepts_incomplete": {fmt.Sprint(acceptsIncomplete)}},
})
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
}
// DeleteServiceBinding deletes the specified Service Binding. An updated
// service binding is returned only if acceptsIncomplete is true.
func (client *Client) DeleteServiceBinding(serviceBindingGUID string, acceptsIncomplete bool) (ServiceBinding, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteServiceBindingRequest,
URIParams: map[string]string{"service_binding_guid": serviceBindingGUID},
Query: url.Values{"accepts_incomplete": {fmt.Sprint(acceptsIncomplete)}},
})
if err != nil {
return ServiceBinding{}, nil, err
}
var response cloudcontroller.Response
var serviceBinding ServiceBinding
if acceptsIncomplete {
response = cloudcontroller.Response{
Result: &serviceBinding,
}
}
err = client.connection.Make(request, &response)
return serviceBinding, response.Warnings, err
}
// GetServiceBinding returns back a service binding with the provided GUID.
func (client *Client) GetServiceBinding(guid string) (ServiceBinding, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServiceBindingRequest,
URIParams: Params{"service_binding_guid": guid},
})
if err != nil {
return ServiceBinding{}, nil, err
}
var serviceBinding ServiceBinding
response := cloudcontroller.Response{
Result: &serviceBinding,
}
err = client.connection.Make(request, &response)
return serviceBinding, response.Warnings, err
}
// GetServiceBindings returns back a list of Service Bindings based off of the
// provided filters.
func (client *Client) GetServiceBindings(filters ...Filter) ([]ServiceBinding, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServiceBindingsRequest,
Query: ConvertFilterParameters(filters),
})
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
}
// 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
}