forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_binding.go
102 lines (83 loc) · 3.35 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
package v2action
import (
"fmt"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
)
// ServiceBinding represents the link between a service instance and an
// application.
type ServiceBinding ccv2.ServiceBinding
// ServiceBindingNotFoundError is returned when a service binding cannot be
// found.
type ServiceBindingNotFoundError struct {
AppGUID string
ServiceInstanceGUID string
}
func (e ServiceBindingNotFoundError) Error() string {
return fmt.Sprintf("Service binding for application GUID '%s', and service instance GUID '%s' not found.", e.AppGUID, e.ServiceInstanceGUID)
}
// BindServiceBySpace binds the service instance to an application for a given space.
func (actor Actor) BindServiceBySpace(appName string, serviceInstanceName string, spaceGUID string, parameters map[string]interface{}) (Warnings, error) {
var allWarnings Warnings
app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace(serviceInstanceName, spaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
_, ccv2Warnings, err := actor.CloudControllerClient.CreateServiceBinding(app.GUID, serviceInstance.GUID, parameters)
allWarnings = append(allWarnings, ccv2Warnings...)
return allWarnings, err
}
// GetServiceBindingByApplicationAndServiceInstance returns a service binding
// given an application GUID and and service instance GUID.
func (actor Actor) GetServiceBindingByApplicationAndServiceInstance(appGUID string, serviceInstanceGUID string) (ServiceBinding, Warnings, error) {
serviceBindings, warnings, err := actor.CloudControllerClient.GetServiceBindings([]ccv2.Query{
ccv2.Query{
Filter: ccv2.AppGUIDFilter,
Operator: ccv2.EqualOperator,
Value: appGUID,
},
ccv2.Query{
Filter: ccv2.ServiceInstanceGUIDFilter,
Operator: ccv2.EqualOperator,
Value: serviceInstanceGUID,
},
})
if err != nil {
return ServiceBinding{}, Warnings(warnings), err
}
if len(serviceBindings) == 0 {
return ServiceBinding{}, Warnings(warnings), ServiceBindingNotFoundError{
AppGUID: appGUID,
ServiceInstanceGUID: serviceInstanceGUID,
}
}
return ServiceBinding(serviceBindings[0]), Warnings(warnings), err
}
// UnbindServiceBySpace deletes the service binding between an application and
// service instance for a given space.
func (actor Actor) UnbindServiceBySpace(appName string, serviceInstanceName string, spaceGUID string) (Warnings, error) {
var allWarnings Warnings
app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace(serviceInstanceName, spaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
serviceBinding, warnings, err := actor.GetServiceBindingByApplicationAndServiceInstance(app.GUID, serviceInstance.GUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
ccWarnings, err := actor.CloudControllerClient.DeleteServiceBinding(serviceBinding.GUID)
allWarnings = append(allWarnings, ccWarnings...)
return allWarnings, err
}