forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_provided_service_instances.go
79 lines (62 loc) · 2.51 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
package api
import (
"bytes"
"encoding/json"
"fmt"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
//go:generate counterfeiter . UserProvidedServiceInstanceRepository
type UserProvidedServiceInstanceRepository interface {
Create(name, drainUrl string, routeServiceUrl string, params map[string]interface{}) (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{}) (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,
})
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,
}
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
}