-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_keys.go
104 lines (85 loc) · 3.34 KB
/
service_keys.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
99
100
101
102
103
104
package api
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"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 ServiceKeyRepository interface {
CreateServiceKey(serviceKeyGuid string, keyName string, params map[string]interface{}) error
ListServiceKeys(serviceKeyGuid string) ([]models.ServiceKey, error)
GetServiceKey(serviceKeyGuid string, keyName string) (models.ServiceKey, error)
DeleteServiceKey(serviceKeyGuid string) error
}
type CloudControllerServiceKeyRepository struct {
config core_config.Reader
gateway net.Gateway
}
func NewCloudControllerServiceKeyRepository(config core_config.Reader, gateway net.Gateway) (repo CloudControllerServiceKeyRepository) {
return CloudControllerServiceKeyRepository{
config: config,
gateway: gateway,
}
}
func (c CloudControllerServiceKeyRepository) CreateServiceKey(instanceGuid string, keyName string, params map[string]interface{}) error {
path := "/v2/service_keys"
request := models.ServiceKeyRequest{
Name: keyName,
ServiceInstanceGuid: instanceGuid,
Params: params,
}
jsonBytes, err := json.Marshal(request)
if err != nil {
return err
}
err = c.gateway.CreateResource(c.config.ApiEndpoint(), path, bytes.NewReader(jsonBytes))
if httpErr, ok := err.(errors.HttpError); ok && httpErr.ErrorCode() == errors.SERVICE_KEY_NAME_TAKEN {
return errors.NewModelAlreadyExistsError("Service key", keyName)
} else if httpErr, ok := err.(errors.HttpError); ok && httpErr.ErrorCode() == errors.UNBINDABLE_SERVICE {
return errors.NewUnbindableServiceError()
} else if httpErr, ok := err.(errors.HttpError); ok && httpErr.ErrorCode() != "" {
return errors.New(httpErr.Error())
}
return nil
}
func (c CloudControllerServiceKeyRepository) ListServiceKeys(instanceGuid string) ([]models.ServiceKey, error) {
path := fmt.Sprintf("/v2/service_instances/%s/service_keys", instanceGuid)
return c.listServiceKeys(path)
}
func (c CloudControllerServiceKeyRepository) GetServiceKey(instanceGuid string, keyName string) (models.ServiceKey, error) {
path := fmt.Sprintf("/v2/service_instances/%s/service_keys?q=%s", instanceGuid, url.QueryEscape("name:"+keyName))
serviceKeys, err := c.listServiceKeys(path)
if err != nil || len(serviceKeys) == 0 {
return models.ServiceKey{}, err
}
return serviceKeys[0], nil
}
func (c CloudControllerServiceKeyRepository) listServiceKeys(path string) ([]models.ServiceKey, error) {
serviceKeys := []models.ServiceKey{}
err := c.gateway.ListPaginatedResources(
c.config.ApiEndpoint(),
path,
resources.ServiceKeyResource{},
func(resource interface{}) bool {
serviceKey := resource.(resources.ServiceKeyResource).ToModel()
serviceKeys = append(serviceKeys, serviceKey)
return true
})
if err != nil {
if httpErr, ok := err.(errors.HttpError); ok && httpErr.ErrorCode() == errors.NOT_AUTHORIZED {
return []models.ServiceKey{}, errors.NewNotAuthorizedError()
} else {
return []models.ServiceKey{}, err
}
}
return serviceKeys, nil
}
func (c CloudControllerServiceKeyRepository) DeleteServiceKey(serviceKeyGuid string) error {
path := fmt.Sprintf("/v2/service_keys/%s", serviceKeyGuid)
return c.gateway.DeleteResource(c.config.ApiEndpoint(), path)
}