forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_bindings.go
62 lines (50 loc) · 1.59 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
package api
import (
"bytes"
"encoding/json"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
type ServiceBindingRepository interface {
Create(instanceGuid, appGuid string, paramsMap map[string]interface{}) (apiErr error)
Delete(instance models.ServiceInstance, appGuid string) (found bool, apiErr error)
}
type CloudControllerServiceBindingRepository struct {
config core_config.Reader
gateway net.Gateway
}
func NewCloudControllerServiceBindingRepository(config core_config.Reader, gateway net.Gateway) (repo CloudControllerServiceBindingRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerServiceBindingRepository) Create(instanceGuid, appGuid string, paramsMap map[string]interface{}) (apiErr 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) (found bool, apiErr error) {
var path string
for _, binding := range instance.ServiceBindings {
if binding.AppGuid == appGuid {
path = binding.Url
break
}
}
if path == "" {
return
} else {
found = true
}
apiErr = repo.gateway.DeleteResource(repo.config.ApiEndpoint(), path)
return
}