forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_brokers.go
98 lines (85 loc) · 3.6 KB
/
service_brokers.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
package api
import (
"fmt"
"net/url"
"strings"
"github.com/cloudfoundry/cli/cf/api/resources"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
type ServiceBrokerRepository interface {
ListServiceBrokers(callback func(models.ServiceBroker) bool) error
FindByName(name string) (serviceBroker models.ServiceBroker, apiErr error)
FindByGuid(guid string) (serviceBroker models.ServiceBroker, apiErr error)
Create(name, url, username, password string) (apiErr error)
Update(serviceBroker models.ServiceBroker) (apiErr error)
Rename(guid, name string) (apiErr error)
Delete(guid string) (apiErr error)
}
type CloudControllerServiceBrokerRepository struct {
config core_config.Reader
gateway net.Gateway
}
func NewCloudControllerServiceBrokerRepository(config core_config.Reader, gateway net.Gateway) (repo CloudControllerServiceBrokerRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerServiceBrokerRepository) ListServiceBrokers(callback func(models.ServiceBroker) bool) error {
return repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
"/v2/service_brokers",
resources.ServiceBrokerResource{},
func(resource interface{}) bool {
callback(resource.(resources.ServiceBrokerResource).ToFields())
return true
})
}
func (repo CloudControllerServiceBrokerRepository) FindByName(name string) (serviceBroker models.ServiceBroker, apiErr error) {
foundBroker := false
apiErr = repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
fmt.Sprintf("/v2/service_brokers?q=%s", url.QueryEscape("name:"+name)),
resources.ServiceBrokerResource{},
func(resource interface{}) bool {
serviceBroker = resource.(resources.ServiceBrokerResource).ToFields()
foundBroker = true
return false
})
if !foundBroker && (apiErr == nil) {
apiErr = errors.NewModelNotFoundError("Service Broker", name)
}
return
}
func (repo CloudControllerServiceBrokerRepository) FindByGuid(guid string) (serviceBroker models.ServiceBroker, apiErr error) {
broker := new(resources.ServiceBrokerResource)
apiErr = repo.gateway.GetResource(repo.config.ApiEndpoint()+fmt.Sprintf("/v2/service_brokers/%s", guid), broker)
serviceBroker = broker.ToFields()
return
}
func (repo CloudControllerServiceBrokerRepository) Create(name, url, username, password string) (apiErr error) {
path := "/v2/service_brokers"
body := fmt.Sprintf(
`{"name":"%s","broker_url":"%s","auth_username":"%s","auth_password":"%s"}`, name, url, username, password,
)
return repo.gateway.CreateResource(repo.config.ApiEndpoint(), path, strings.NewReader(body))
}
func (repo CloudControllerServiceBrokerRepository) Update(serviceBroker models.ServiceBroker) (apiErr error) {
path := fmt.Sprintf("/v2/service_brokers/%s", serviceBroker.Guid)
body := fmt.Sprintf(
`{"broker_url":"%s","auth_username":"%s","auth_password":"%s"}`,
serviceBroker.Url, serviceBroker.Username, serviceBroker.Password,
)
return repo.gateway.UpdateResource(repo.config.ApiEndpoint(), path, strings.NewReader(body))
}
func (repo CloudControllerServiceBrokerRepository) Rename(guid, name string) (apiErr error) {
path := fmt.Sprintf("/v2/service_brokers/%s", guid)
body := fmt.Sprintf(`{"name":"%s"}`, name)
return repo.gateway.UpdateResource(repo.config.ApiEndpoint(), path, strings.NewReader(body))
}
func (repo CloudControllerServiceBrokerRepository) Delete(guid string) (apiErr error) {
path := fmt.Sprintf("/v2/service_brokers/%s", guid)
return repo.gateway.DeleteResource(repo.config.ApiEndpoint(), path)
}