-
Notifications
You must be signed in to change notification settings - Fork 929
/
service_bindings.go
79 lines (66 loc) · 2.33 KB
/
service_bindings.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"code.cloudfoundry.org/cli/cf/api/resources"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/net"
)
//go:generate counterfeiter . ServiceBindingRepository
type ServiceBindingRepository interface {
Create(instanceGUID string, appGUID string, paramsMap map[string]interface{}) error
Delete(instance models.ServiceInstance, appGUID string) (bool, error)
ListAllForService(instanceGUID string) ([]models.ServiceBindingFields, error)
}
type CloudControllerServiceBindingRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCloudControllerServiceBindingRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerServiceBindingRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerServiceBindingRepository) Create(instanceGUID, appGUID string, paramsMap map[string]interface{}) error {
path := "/v2/service_bindings"
request := models.ServiceBindingRequest{
AppGUID: appGUID,
ServiceInstanceGUID: instanceGUID,
Params: paramsMap,
}
jsonBytes, err := json.Marshal(request)
if err != nil {
return err
}
return repo.gateway.CreateResource(repo.config.APIEndpoint(), path, bytes.NewReader(jsonBytes))
}
func (repo CloudControllerServiceBindingRepository) Delete(instance models.ServiceInstance, appGUID string) (bool, error) {
var path string
for _, binding := range instance.ServiceBindings {
if binding.AppGUID == appGUID {
path = binding.URL
break
}
}
if path == "" {
return false, nil
}
return true, repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
}
func (repo CloudControllerServiceBindingRepository) ListAllForService(instanceGUID string) ([]models.ServiceBindingFields, error) {
serviceBindings := []models.ServiceBindingFields{}
err := repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
fmt.Sprintf("/v2/service_instances/%s/service_bindings", instanceGUID),
resources.ServiceBindingResource{},
func(resource interface{}) bool {
if serviceBindingResource, ok := resource.(resources.ServiceBindingResource); ok {
serviceBindings = append(serviceBindings, serviceBindingResource.ToFields())
}
return true
},
)
return serviceBindings, err
}