-
Notifications
You must be signed in to change notification settings - Fork 929
/
user_provided_service_instances.go
81 lines (64 loc) · 2.6 KB
/
user_provided_service_instances.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/net"
)
//go:generate counterfeiter . UserProvidedServiceInstanceRepository
type UserProvidedServiceInstanceRepository interface {
Create(name, drainURL string, routeServiceURL string, params map[string]interface{}, tags []string) (apiErr error)
Update(serviceInstanceFields models.ServiceInstanceFields) (apiErr error)
GetSummaries() (models.UserProvidedServiceSummary, error)
}
type CCUserProvidedServiceInstanceRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCCUserProvidedServiceInstanceRepository(config coreconfig.Reader, gateway net.Gateway) (repo CCUserProvidedServiceInstanceRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CCUserProvidedServiceInstanceRepository) Create(name, drainURL string, routeServiceURL string, params map[string]interface{}, tags []string) (apiErr error) {
path := "/v2/user_provided_service_instances"
jsonBytes, err := json.Marshal(models.UserProvidedService{
Name: name,
Credentials: params,
SpaceGUID: repo.config.SpaceFields().GUID,
SysLogDrainURL: drainURL,
RouteServiceURL: routeServiceURL,
Tags: tags,
})
if err != nil {
apiErr = fmt.Errorf("%s: %s", "Error parsing response", err.Error())
return
}
return repo.gateway.CreateResource(repo.config.APIEndpoint(), path, bytes.NewReader(jsonBytes))
}
func (repo CCUserProvidedServiceInstanceRepository) Update(serviceInstanceFields models.ServiceInstanceFields) (apiErr error) {
path := fmt.Sprintf("/v2/user_provided_service_instances/%s", serviceInstanceFields.GUID)
reqBody := models.UserProvidedService{
Credentials: serviceInstanceFields.Params,
SysLogDrainURL: serviceInstanceFields.SysLogDrainURL,
RouteServiceURL: serviceInstanceFields.RouteServiceURL,
Tags: serviceInstanceFields.Tags,
}
jsonBytes, err := json.Marshal(reqBody)
if err != nil {
apiErr = fmt.Errorf("%s: %s", "Error parsing response", err.Error())
return
}
return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, bytes.NewReader(jsonBytes))
}
func (repo CCUserProvidedServiceInstanceRepository) GetSummaries() (models.UserProvidedServiceSummary, error) {
path := fmt.Sprintf("%s/v2/user_provided_service_instances", repo.config.APIEndpoint())
model := models.UserProvidedServiceSummary{}
apiErr := repo.gateway.GetResource(path, &model)
if apiErr != nil {
return models.UserProvidedServiceSummary{}, apiErr
}
return model, nil
}