forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_bindings.go
54 lines (45 loc) · 1.41 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
package api
import (
"fmt"
"github.com/cloudfoundry/cli/cf/configuration"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
"strings"
)
type ServiceBindingRepository interface {
Create(instanceGuid, appGuid string) (apiErr error)
Delete(instance models.ServiceInstance, appGuid string) (found bool, apiErr error)
}
type CloudControllerServiceBindingRepository struct {
config configuration.Reader
gateway net.Gateway
}
func NewCloudControllerServiceBindingRepository(config configuration.Reader, gateway net.Gateway) (repo CloudControllerServiceBindingRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerServiceBindingRepository) Create(instanceGuid, appGuid string) (apiErr error) {
path := fmt.Sprintf("%s/v2/service_bindings", repo.config.ApiEndpoint())
body := fmt.Sprintf(
`{"app_guid":"%s","service_instance_guid":"%s","async":true}`,
appGuid, instanceGuid,
)
return repo.gateway.CreateResource(path, strings.NewReader(body))
}
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 = repo.config.ApiEndpoint() + binding.Url
break
}
}
if path == "" {
return
} else {
found = true
}
apiErr = repo.gateway.DeleteResource(path)
return
}