-
Notifications
You must be signed in to change notification settings - Fork 928
/
service_broker.go
154 lines (134 loc) · 4.74 KB
/
service_broker.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package ccv3
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
// ServiceBroker represents Service Broker data
type ServiceBroker struct {
// GUID is a unique service broker identifier.
GUID string
// Name is the name of the service broker.
Name string
// URL is the url of the service broker.
URL string
}
// serviceBrokerPresentation represents a Cloud Controller V3 Service Broker.
type serviceBrokerPresentation struct {
// GUID is a unique service broker identifier.
GUID string `json:"guid,omitempty"`
// Name is the name of the service broker.
Name string `json:"name"`
// URL is the url of the service broker.
URL string `json:"url"`
// Credentials contains the credentials for authenticating with the service broker.
Credentials serviceBrokerCredentials `json:"credentials"`
// This is the relationship for the space GUID
Relationships *serviceBrokerRelationships `json:"relationships,omitempty"`
}
// serviceBrokerCredentials represents a data structure for the Credentials
// of V3 Service Broker.
type serviceBrokerCredentials struct {
// Type is the type of credentials for the service broker, e.g. "basic"
Type constant.ServiceBrokerCredentialsType `json:"type"`
// Data is the credentials data of the service broker of a particular type.
Data serviceBrokerCredentialsData `json:"data"`
}
// serviceBrokerCredentialsData represents a data structure for the Credentials Data
// of V3 Service Broker Credentials.
type serviceBrokerCredentialsData struct {
// Username is the Basic Auth username for the service broker.
Username string `json:"username"`
// Password is the Basic Auth password for the service broker.
Password string `json:"password"`
}
// serviceBrokerRelationships represents a data structure for the relationships data
// of V3 Service Broker Relationships.
type serviceBrokerRelationships struct {
// Space represents the space that a space-scoped broker is in
Space serviceBrokerRelationshipsSpace `json:"space"`
}
// serviceBrokerRelationshipsSpace represents a data structure for the relationships space data
// of V3 Service Broker Relationships.
type serviceBrokerRelationshipsSpace struct {
// Data holds the space GUID object
Data serviceBrokerRelationshipsSpaceData `json:"data"`
}
// serviceBrokerRelationshipsSpaceData represents a data structure for the relationships space GUID data
// of V3 Service Broker Relationships.
type serviceBrokerRelationshipsSpaceData struct {
// GUID is the space guid associated with a space-scoped broker
GUID string `json:"guid"`
}
// CreateServiceBroker registers a new service broker.
func (client *Client) CreateServiceBroker(name, username, password, brokerURL, spaceGUID string) (Warnings, error) {
bodyBytes, err := json.Marshal(newServiceBroker(name, username, password, brokerURL, spaceGUID))
if err != nil {
return nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostServiceBrokerRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}
// GetServiceBrokers lists service brokers.
func (client *Client) GetServiceBrokers() ([]ServiceBroker, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetServiceBrokersRequest,
})
if err != nil {
return nil, nil, err
}
var fullList []ServiceBroker
warnings, err := client.paginate(request, serviceBrokerPresentation{}, func(item interface{}) error {
if serviceBroker, ok := item.(serviceBrokerPresentation); ok {
fullList = append(fullList, extractServiceBrokerData(serviceBroker))
} else {
return ccerror.UnknownObjectInListError{
Expected: serviceBrokerPresentation{},
Unexpected: item,
}
}
return nil
})
return fullList, warnings, err
}
func newServiceBroker(name, username, password, brokerURL, spaceGUID string) serviceBrokerPresentation {
sbp := serviceBrokerPresentation{
Name: name,
URL: brokerURL,
Credentials: serviceBrokerCredentials{
Type: constant.BasicCredentials,
Data: serviceBrokerCredentialsData{
Username: username,
Password: password,
},
},
}
if spaceGUID != "" {
sbp.Relationships = &serviceBrokerRelationships{
Space: serviceBrokerRelationshipsSpace{
Data: serviceBrokerRelationshipsSpaceData{
GUID: spaceGUID,
},
},
}
}
return sbp
}
func extractServiceBrokerData(sbp serviceBrokerPresentation) ServiceBroker {
return ServiceBroker{
Name: sbp.Name,
URL: sbp.URL,
GUID: sbp.GUID,
}
}