This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
client.go
170 lines (138 loc) · 5.24 KB
/
client.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2018 the Service Broker Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/pivotal-cf/brokerapi"
"github.com/spf13/viper"
)
const (
// ClientsBrokerApiVersion is the minimum supported version of the client.
// Note: This may need to be changed in the future as we use newer versions
// of the OSB API, but should be kept near the lower end of the systems we
// expect to be compatible with to ensure any reverse-compatibility measures
// put in place work.
ClientsBrokerApiVersion = "2.13"
)
// NewClientFromEnv creates a new client from the client configuration properties.
func NewClientFromEnv() (*Client, error) {
user := viper.GetString("api.user")
pass := viper.GetString("api.password")
port := viper.GetInt("api.port")
viper.SetDefault("api.hostname", "localhost")
host := viper.GetString("api.hostname")
return New(user, pass, host, port)
}
// New creates a new OSB Client connected to the given resource.
func New(username, password, hostname string, port int) (*Client, error) {
base := fmt.Sprintf("http://%s:%s@%s:%d/v2/", username, password, hostname, port)
baseUrl, err := url.Parse(base)
if err != nil {
return nil, err
}
return &Client{BaseUrl: baseUrl}, nil
}
type Client struct {
BaseUrl *url.URL
}
// Catalog fetches the service catalog
func (client *Client) Catalog() *BrokerResponse {
return client.makeRequest(http.MethodGet, "catalog", nil)
}
// Provision creates a new service with the given instanceId, of type serviceId,
// from the plan planId, with additional details provisioningDetails
func (client *Client) Provision(instanceId, serviceId, planId string, provisioningDetails json.RawMessage) *BrokerResponse {
url := fmt.Sprintf("service_instances/%s?accepts_incomplete=true", instanceId)
return client.makeRequest(http.MethodPut, url, brokerapi.ProvisionDetails{
ServiceID: serviceId,
PlanID: planId,
RawParameters: provisioningDetails,
})
}
// Deprovision destroys a service instance of type instanceId
func (client *Client) Deprovision(instanceId, serviceId, planId string) *BrokerResponse {
url := fmt.Sprintf("service_instances/%s?accepts_incomplete=true&service_id=%s&plan_id=%s", instanceId, serviceId, planId)
return client.makeRequest(http.MethodDelete, url, nil)
}
// Bind creates an account identified by bindingId and gives it access to instanceId
func (client *Client) Bind(instanceId, bindingId, serviceId, planId string, parameters json.RawMessage) *BrokerResponse {
url := fmt.Sprintf("service_instances/%s/service_bindings/%s", instanceId, bindingId)
return client.makeRequest(http.MethodPut, url, brokerapi.BindDetails{
ServiceID: serviceId,
PlanID: planId,
RawParameters: parameters,
})
}
// Unbind destroys an account identified by bindingId
func (client *Client) Unbind(instanceId, bindingId, serviceId, planId string) *BrokerResponse {
url := fmt.Sprintf("service_instances/%s/service_bindings/%s?service_id=%s&plan_id=%s", instanceId, bindingId, serviceId, planId)
return client.makeRequest(http.MethodDelete, url, nil)
}
// Update sends a patch request to change the plan
func (client *Client) Update(instanceId, serviceId, planId string, parameters json.RawMessage) *BrokerResponse {
url := fmt.Sprintf("service_instances/%s", instanceId)
return client.makeRequest(http.MethodPatch, url, brokerapi.UpdateDetails{
ServiceID: serviceId,
PlanID: planId,
RawParameters: parameters,
})
}
// LastOperation queries the status of a long-running job on the server
func (client *Client) LastOperation(instanceId string) *BrokerResponse {
url := fmt.Sprintf("service_instances/%s/last_operation", instanceId)
return client.makeRequest(http.MethodGet, url, nil)
}
func (client *Client) makeRequest(method, path string, body interface{}) *BrokerResponse {
br := BrokerResponse{}
req, err := client.newRequest(method, path, body)
br.UpdateRequest(req)
br.UpdateError(err)
if br.InError() {
return &br
}
resp, err := http.DefaultClient.Do(req)
br.UpdateResponse(resp)
br.UpdateError(err)
return &br
}
func (client *Client) newRequest(method, path string, body interface{}) (*http.Request, error) {
url, err := client.BaseUrl.Parse(path)
if err != nil {
return nil, err
}
var buffer io.ReadWriter
if body != nil {
buffer = new(bytes.Buffer)
enc := json.NewEncoder(buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(body)
if err != nil {
return nil, err
}
}
request, err := http.NewRequest(method, url.String(), buffer)
if err != nil {
return nil, err
}
request.Header.Set("X-Broker-Api-Version", ClientsBrokerApiVersion)
if body != nil {
request.Header.Set("Content-Type", "application/json")
}
return request, nil
}