forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_provided_service_instances.go
71 lines (57 loc) · 2.25 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
package api
import (
"bytes"
"encoding/json"
"fmt"
"github.com/cloudfoundry/cli/cf/configuration"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
type UserProvidedServiceInstanceRepository interface {
Create(name, drainUrl string, params map[string]interface{}) (apiErr error)
Update(serviceInstanceFields models.ServiceInstanceFields) (apiErr error)
}
type CCUserProvidedServiceInstanceRepository struct {
config configuration.Reader
gateway net.Gateway
}
func NewCCUserProvidedServiceInstanceRepository(config configuration.Reader, gateway net.Gateway) (repo CCUserProvidedServiceInstanceRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CCUserProvidedServiceInstanceRepository) Create(name, drainUrl string, params map[string]interface{}) (apiErr error) {
path := fmt.Sprintf("%s/v2/user_provided_service_instances", repo.config.ApiEndpoint())
type RequestBody struct {
Name string `json:"name"`
Credentials map[string]interface{} `json:"credentials"`
SpaceGuid string `json:"space_guid"`
SysLogDrainUrl string `json:"syslog_drain_url"`
}
jsonBytes, err := json.Marshal(RequestBody{
Name: name,
Credentials: params,
SpaceGuid: repo.config.SpaceFields().Guid,
SysLogDrainUrl: drainUrl,
})
if err != nil {
apiErr = errors.NewWithError("Error parsing response", err)
return
}
return repo.gateway.CreateResource(path, bytes.NewReader(jsonBytes))
}
func (repo CCUserProvidedServiceInstanceRepository) Update(serviceInstanceFields models.ServiceInstanceFields) (apiErr error) {
path := fmt.Sprintf("%s/v2/user_provided_service_instances/%s", repo.config.ApiEndpoint(), serviceInstanceFields.Guid)
type RequestBody struct {
Credentials map[string]interface{} `json:"credentials,omitempty"`
SysLogDrainUrl string `json:"syslog_drain_url,omitempty"`
}
reqBody := RequestBody{serviceInstanceFields.Params, serviceInstanceFields.SysLogDrainUrl}
jsonBytes, err := json.Marshal(reqBody)
if err != nil {
apiErr = errors.NewWithError("Error parsing response", err)
return
}
return repo.gateway.UpdateResource(path, bytes.NewReader(jsonBytes))
}