forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_service_binding_repository.go
79 lines (66 loc) · 1.9 KB
/
route_service_binding_repository.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"
"io"
"strings"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"github.com/cloudfoundry/cli/cf/net"
)
//go:generate counterfeiter . RouteServiceBindingRepository
type RouteServiceBindingRepository interface {
Bind(instanceGUID, routeGUID string, userProvided bool, parameters string) error
Unbind(instanceGUID, routeGUID string, userProvided bool) error
}
type CloudControllerRouteServiceBindingRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCloudControllerRouteServiceBindingRepository(config coreconfig.Reader, gateway net.Gateway) CloudControllerRouteServiceBindingRepository {
return CloudControllerRouteServiceBindingRepository{
config: config,
gateway: gateway,
}
}
func (repo CloudControllerRouteServiceBindingRepository) Bind(
instanceGUID string,
routeGUID string,
userProvided bool,
opaqueParams string,
) error {
var rs io.ReadSeeker
if opaqueParams != "" {
opaqueJSON := json.RawMessage(opaqueParams)
s := struct {
Parameters *json.RawMessage `json:"parameters"`
}{
&opaqueJSON,
}
jsonBytes, err := json.Marshal(s)
if err != nil {
return err
}
rs = bytes.NewReader(jsonBytes)
} else {
rs = strings.NewReader("")
}
return repo.gateway.UpdateResourceSync(
repo.config.APIEndpoint(),
getPath(instanceGUID, routeGUID, userProvided),
rs,
)
}
func (repo CloudControllerRouteServiceBindingRepository) Unbind(instanceGUID, routeGUID string, userProvided bool) error {
path := getPath(instanceGUID, routeGUID, userProvided)
return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
}
func getPath(instanceGUID, routeGUID string, userProvided bool) string {
var resource string
if userProvided {
resource = "user_provided_service_instances"
} else {
resource = "service_instances"
}
return fmt.Sprintf("/v2/%s/%s/routes/%s", resource, instanceGUID, routeGUID)
}