forked from compose/gocomposeapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment.go
297 lines (235 loc) · 8.93 KB
/
deployment.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2016 Compose, an IBM Company
//
// 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 composeapi
import (
"encoding/json"
"fmt"
"time"
)
// Deployment structure
type Deployment struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
CreatedAt time.Time `json:"created_at"`
ProvisionRecipeID string `json:"provision_recipe_id,omitempty"`
CACertificateBase64 string `json:"ca_certificate_base64,omitempty"`
Connection ConnectionStrings `json:"connection_strings,omitempty"`
Notes string `json:"notes,omitempty"`
CustomerBillingCode string `json:"customer_billing_code,omitempty"`
Version string `json:"version,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
Links Links `json:"_links"`
}
// Links structure, part of the Deployment struct
type Links struct {
ComposeWebUILink Link `json:"compose_web_ui"`
ScalingsLink Link `json:"scalings"`
BackupsLink Link `json:"backups"`
AlertsLink Link `json:"alerts"`
PortalUsersLink Link `json:"portal_users"`
ClusterLink Link `json:"cluster"`
}
// ConnectionStrings structure, part of the Deployment struct
type ConnectionStrings struct {
Health []string `json:"health,omitempty"`
SSH []string `json:"ssh,omitempty"`
Admin []string `json:"admin,omitempty"`
SSHAdmin []string `json:"ssh_admin,omitempty"`
CLI []string `json:"cli,omitempty"`
Direct []string `json:"direct,omitempty"`
Maps []map[string]string `json:"maps,omitempty"`
Misc interface{} `json:"misc,omitempty"`
}
// deploymentsResource is used to represent and remove the JSON+HAL Embedded wrapper
type deploymentsResponse struct {
Embedded struct {
Deployments []Deployment `json:"deployments"`
} `json:"_embedded"`
}
//CreateDeploymentParams Parameters to be completed before creating a deployment
type CreateDeploymentParams struct {
Deployment DeploymentParams `json:"deployment"`
}
type patchDeployment struct {
DeploymentID string `json:"-"`
Deployment patchDeploymentParams `json:"deployment"`
}
type patchDeploymentParams struct {
Notes string `json:"notes,omitempty"`
CustomerBillingCode string `json:"customer_billing_code,omitempty"`
}
// PatchDeploymentParams is used to pass parameters to PatchDeployment
type PatchDeploymentParams struct {
DeploymentID string `json:"omit"`
Notes string `json:"notes,omitempty"`
CustomerBillingCode string `json:"customer_billing_code,omitempty"`
}
// DeploymentParams core parameters for a new deployment
type DeploymentParams struct {
Name string `json:"name"`
AccountID string `json:"account_id"`
ClusterID string `json:"cluster_id,omitempty"`
Datacenter string `json:"datacenter,omitempty"`
ProvisioningTags []string `json:"provisioning_tags,omitempty"`
DatabaseType string `json:"type"`
Version string `json:"version,omitempty"`
Units int `json:"units,omitempty"`
SSL bool `json:"ssl,omitempty"`
CacheMode bool `json:"cache_mode,omitempty"`
WiredTiger bool `json:"wired_tiger,omitempty"`
Notes string `json:"notes,omitempty"`
CustomerBillingCode string `json:"customer_billing_code,omitempty"`
}
//VersionTransition a struct wrapper for version transition information
type VersionTransition struct {
Application string `json:"application"`
Method string `json:"method"`
FromVersion string `json:"from_version"`
ToVersion string `json:"to_version"`
}
type versionsResponse struct {
Embedded struct {
VersionTransitions []VersionTransition `json:"transitions"`
} `json:"_embedded"`
}
type patchDeploymentVersionParams struct {
Deployment deploymentVersion `json:"deployment"`
}
type deploymentVersion struct {
Version string `json:"version"`
}
//CreateDeploymentJSON performs the call
func (c *Client) CreateDeploymentJSON(params DeploymentParams) (string, []error) {
deploymentparams := CreateDeploymentParams{Deployment: params}
response, body, errs := c.newRequest("POST", apibase+"deployments").
Send(deploymentparams).
End()
if response == nil {
return internalError(errs)
}
if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s", response.StatusCode, response.Body))
} else {
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
}
}
return body, errs
}
//CreateDeployment creates a deployment
func (c *Client) CreateDeployment(params DeploymentParams) (*Deployment, []error) {
// This is a POST not a GET, so it builds its own request
body, errs := c.CreateDeploymentJSON(params)
if errs != nil {
return nil, errs
}
deployed := Deployment{}
json.Unmarshal([]byte(body), &deployed)
return &deployed, nil
}
//GetDeploymentsJSON returns raw deployment
func (c *Client) GetDeploymentsJSON() (string, []error) { return c.getJSON("deployments") }
//GetDeployments returns deployment structure
func (c *Client) GetDeployments() (*[]Deployment, []error) {
body, errs := c.GetDeploymentsJSON()
if errs != nil {
return nil, errs
}
deploymentResponse := deploymentsResponse{}
json.Unmarshal([]byte(body), &deploymentResponse)
deployments := deploymentResponse.Embedded.Deployments
return &deployments, nil
}
//GetDeploymentJSON returns raw deployment
func (c *Client) GetDeploymentJSON(deploymentid string) (string, []error) {
return c.getJSON("deployments/" + deploymentid)
}
//GetDeployment returns deployment structure
func (c *Client) GetDeployment(deploymentid string) (*Deployment, []error) {
body, errs := c.GetDeploymentJSON(deploymentid)
if errs != nil {
return nil, errs
}
deployment := Deployment{}
json.Unmarshal([]byte(body), &deployment)
return &deployment, nil
}
//GetDeploymentByName returns a deployment of a given name
func (c *Client) GetDeploymentByName(deploymentName string) (*Deployment, []error) {
deployments, errs := c.GetDeployments()
if errs != nil {
return nil, errs
}
for _, deployment := range *deployments {
if deployment.Name == deploymentName {
return c.GetDeployment(deployment.ID)
}
}
return nil, []error{fmt.Errorf("deployment not found: %s", deploymentName)}
}
//DeprovisionDeploymentJSON performs the call
func (c *Client) DeprovisionDeploymentJSON(deploymentID string) (string, []error) {
response, body, errs := c.newRequest("DELETE", apibase+"deployments/"+deploymentID).
End()
if response == nil {
return internalError(errs)
}
if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
return body, errs
}
//DeprovisionDeployment deletes a deployment
func (c *Client) DeprovisionDeployment(deploymentID string) (*Recipe, []error) {
// This is a POST not a GET, so it builds its own request
body, errs := c.DeprovisionDeploymentJSON(deploymentID)
if errs != nil {
return nil, errs
}
deprovrecipe := Recipe{}
json.Unmarshal([]byte(body), &deprovrecipe)
return &deprovrecipe, nil
}
//PatchDeploymentJSON performs the call
func (c *Client) PatchDeploymentJSON(params PatchDeploymentParams) (string, []error) {
patchParams := patchDeployment{DeploymentID: params.DeploymentID,
Deployment: patchDeploymentParams{
CustomerBillingCode: params.CustomerBillingCode,
Notes: params.Notes,
}}
response, body, errs := c.newRequest("PATCH", apibase+"deployments/"+patchParams.DeploymentID).
Send(patchParams).
End()
if response == nil {
return internalError(errs)
}
if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else
errs = ProcessErrors(response.StatusCode, body)
}
return body, errs
}
//PatchDeployment patches a deployment
func (c *Client) PatchDeployment(params PatchDeploymentParams) (*Deployment, []error) {
// This is a POST not a GET, so it builds its own request
body, errs := c.PatchDeploymentJSON(params)
if errs != nil {
return nil, errs
}
deployed := Deployment{}
json.Unmarshal([]byte(body), &deployed)
return &deployed, nil
}