-
Notifications
You must be signed in to change notification settings - Fork 24
/
deployable.go
425 lines (371 loc) · 12.1 KB
/
deployable.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package apigee
import (
"path"
"net/url"
"fmt"
"time"
"strings"
"archive/zip"
"os"
"path/filepath"
"io"
"io/ioutil"
"errors"
)
// DeployableAsset contains information about an API Proxy or SharedFlow within an Apigee organization.
type DeployableAsset struct {
Revisions []Revision `json:"revision,omitempty"`
Name string `json:"name,omitempty"`
MetaData DeployableMetadata `json:"metaData,omitempty"`
}
// DeployableRevision holds information about a revision of an API Proxy, or a SharedFlow.
type DeployableRevision struct {
Name string `json:"name,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Revision Revision `json:"revision,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
CreatedAt Timestamp `json:"createdAt,omitempty"`
LastModifiedBy string `json:"lastModifiedBy,omitempty"`
LastModifiedAt Timestamp `json:"lastModifiedAt,omitempty"`
Description string `json:"description,omitempty"`
ContextInfo string `json:"contextInfo,omitempty"`
TargetEndpoints []string `json:"targetEndpoints,omitempty"`
TargetServers []string `json:"targetServers,omitempty"`
Resources []string `json:"resources,omitempty"`
ProxyEndpoints []string `json:"proxyEndpoints,omitempty"`
SharedFlows []string `json:"sharedFlows,omitempty"`
Policies []string `json:"policies,omitempty"`
Type string `json:"type,omitempty"`
}
// ProxyMetadata contains information related to the creation and last modified
// time and actor for an API Proxy within an organization.
type DeployableMetadata struct {
LastModifiedBy string `json:"lastModifiedBy,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
LastModifiedAt Timestamp `json:"lastModifiedAt,omitempty"`
CreatedAt Timestamp `json:"createdAt,omitempty"`
}
// When Delete returns successfully, it returns a payload that contains very little useful
// information. This struct deserializes that information.
type DeletedItemInfo struct {
Name string `json:"name,omitempty"`
}
// When inquiring the deployment status of an API PRoxy revision, even implicitly
// as when performing a Deploy or Undeploy, the response includes the deployment
// status for each particular Edge Server in the environment. This struct
// deserializes that information. It will normally not be useful at all. In rare
// cases, it may be useful in helping to diagnose problems. For example, if there
// is a problem with a deployment change, as when a Message Processor is
// experiencing a problem and cannot undeploy, or more commonly, cannot deploy an
// API Proxy, this struct will hold relevant information.
type ApigeeServer struct {
Status string `json:"status,omitempty"`
Uuid string `json:"uUID,omitempty"`
Type []string `json:"type,omitempty"`
}
// Deployment (nee ProxyDeployment) holds information about the deployment state of a
// all revisions of an API Proxy or SharedFlow.
type Deployment struct {
Environments []EnvironmentDeployment `json:"environment,omitempty"`
Name string `json:"name,omitempty"`
Organization string `json:"organization,omitempty"`
}
type EnvironmentDeployment struct {
Name string `json:"name,omitempty"`
Revision []RevisionDeployment `json:"revision,omitempty"`
}
type RevisionDeployment struct {
Number Revision `json:"name,omitempty"`
State string `json:"state,omitempty"`
Servers []ApigeeServer `json:"server,omitempty"`
}
type Deployable struct { }
func (s *Deployable) List(client *ApigeeClient, uriPathElement string) ([]string, *Response, error) {
req, e := client.NewRequest("GET", uriPathElement, nil)
if e != nil {
return nil, nil, e
}
namelist := make([]string,0)
resp, e := client.Do(req, &namelist)
if e != nil {
return nil, resp, e
}
return namelist, resp, e
}
func (s *Deployable) Get(client *ApigeeClient, uriPathElement, assetName string) (*DeployableAsset, *Response, error) {
path := path.Join(uriPathElement, assetName)
req, e := client.NewRequest("GET", path, nil)
if e != nil {
return nil, nil, e
}
returnedAsset := DeployableAsset{}
resp, e := client.Do(req, &returnedAsset)
if e != nil {
return nil, resp, e
}
return &returnedAsset, resp, e
}
func smartFilter(path string) bool {
if strings.HasSuffix(path, "~") {
return false
}
if strings.HasSuffix(path, "#") && strings.HasPrefix(path, "#") {
return false
}
return true
}
func zipDirectory (source string, target string, filter func(string) bool) error {
zipfile, err := os.Create(target)
if err != nil {
return err
}
defer zipfile.Close()
archive := zip.NewWriter(zipfile)
defer archive.Close()
info, err := os.Stat(source)
if err != nil {
return nil
}
var baseDir string
if info.IsDir() {
baseDir = filepath.Base(source)
}
filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if filter == nil || filter(path) {
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
if baseDir != "" {
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
}
// This archive will be unzipped by a Java process. When ZIP64 extensions
// are used, Java insists on having Deflate as the compression method (0x08)
// even for directories.
header.Method = zip.Deflate
if info.IsDir() {
header.Name += "/"
}
writer, err := archive.CreateHeader(header)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
}
return err
})
return err
}
func (s *Deployable) Import(client *ApigeeClient, uriPathElement, assetName, source string) (*DeployableRevision, *Response, error) {
info, err := os.Stat(source)
if err != nil {
return nil, nil, err
}
zipfileName := source
if info.IsDir() {
// create a temporary zip file
if assetName == "" {
assetName = filepath.Base(source)
}
tempDir, e := ioutil.TempDir("", "go-apigee-")
if e != nil {
return nil, nil, errors.New(fmt.Sprintf("while creating temp dir, error: %#v", e))
}
zipfileName = path.Join(tempDir, "bundle.zip")
var filePathElement string;
if uriPathElement == "apis" {
filePathElement = "apiproxy"
} else {
filePathElement = "sharedflowbundle"
}
e = zipDirectory (path.Join(source, filePathElement), zipfileName, smartFilter)
if e != nil {
return nil, nil, errors.New(fmt.Sprintf("while creating temp dir, error: %#v", e))
}
fmt.Printf("zipped %s into %s\n\n", source, zipfileName)
cleanup := func(filename string) {
_ = os.Remove(filename)
// if e != nil {
// //..
// }
}
defer cleanup(zipfileName)
}
if !strings.HasSuffix(zipfileName, ".zip") {
return nil, nil, errors.New("source must be a zipfile")
}
info, err = os.Stat(zipfileName)
if err != nil {
return nil, nil, err
}
// append the query params
origURL, err := url.Parse(uriPathElement)
if err != nil {
return nil, nil, err
}
q := origURL.Query()
q.Add("action", "import")
q.Add("name", assetName)
origURL.RawQuery = q.Encode()
path := origURL.String()
ioreader, err := os.Open(zipfileName)
if err != nil {
return nil, nil, err
}
defer ioreader.Close()
req, e := client.NewRequest("POST", path, ioreader)
if e != nil {
return nil, nil, e
}
returnedRevision := DeployableRevision{}
resp, e := client.Do(req, &returnedRevision)
if e != nil {
return nil, resp, e
}
return &returnedRevision, resp, e
}
func (s *Deployable) Export(client *ApigeeClient, uriPathElement, assetName string, rev Revision) (string, *Response, error) {
// curl -u USER:PASSWORD \
// http://MGMTSERVER/v1/o/ORGNAME/apis/APINAME/revisions/REVNUMBER?format=bundle > bundle.zip
path := path.Join(uriPathElement, assetName, "revisions", fmt.Sprintf("%d",rev))
// TODO: factor out method: appendQueryParams
// append the required query param
origURL, err := url.Parse(path)
if err != nil {
return "", nil, err
}
q := origURL.Query()
q.Add("format", "bundle")
origURL.RawQuery = q.Encode()
path = origURL.String()
req, e := client.NewRequest("GET", path, nil)
if e != nil {
return "", nil, e
}
req.Header.Del("Accept")
var assetType string;
if uriPathElement == "apis" {
assetType = "apiproxy"
} else {
assetType = "sharedflowbundle"
}
t := time.Now()
filename := fmt.Sprintf("%s-%s-r%d-%d%02d%02d-%02d%02d%02d.zip",
assetType, assetName,
rev, t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
out, e := os.Create(filename)
if e != nil {
return "", nil, e
}
resp, e := client.Do(req, out)
if e != nil {
return "", resp, e
}
out.Close()
return filename, resp, e
}
func (s *Deployable) DeleteRevision(client *ApigeeClient, uriPathElement, assetName string, rev Revision) (*DeployableRevision, *Response, error) {
path := path.Join(uriPathElement, assetName, "revisions", fmt.Sprintf("%d",rev))
req, e := client.NewRequest("DELETE", path, nil)
if e != nil {
return nil, nil, e
}
proxyRev := DeployableRevision{}
resp, e := client.Do(req, &proxyRev)
if e != nil {
return nil, resp, e
}
return &proxyRev, resp, e
}
func (s *Deployable) Undeploy(client *ApigeeClient, uriPathElement, assetName, env string, rev Revision) (*RevisionDeployment, *Response, error) {
path := path.Join(uriPathElement, assetName, "revisions", fmt.Sprintf("%d",rev), "deployments")
// append the query params
origURL, err := url.Parse(path)
if err != nil {
return nil, nil, err
}
q := origURL.Query()
q.Add("action", "undeploy")
q.Add("env", env)
origURL.RawQuery = q.Encode()
path = origURL.String()
req, e := client.NewRequest("POST", path, nil)
if e != nil {
return nil, nil, e
}
deployment := RevisionDeployment{}
resp, e := client.Do(req, &deployment)
if e != nil {
return nil, resp, e
}
return &deployment, resp, e
}
func (s *Deployable) Deploy(client *ApigeeClient, uriPathElement, assetName, basepath, env string, rev Revision) (*RevisionDeployment, *Response, error) {
path := path.Join(uriPathElement, assetName, "revisions", fmt.Sprintf("%d",rev), "deployments")
// append the query params
origURL, err := url.Parse(path)
if err != nil {
return nil, nil, err
}
q := origURL.Query()
q.Add("action", "deploy")
q.Add("override", "true")
q.Add("delay", DeploymentDelay)
q.Add("env", env)
if basepath != "" {
q.Add("basepath", basepath)
}
origURL.RawQuery = q.Encode()
path = origURL.String()
req, e := client.NewRequest("POST", path, nil)
if e != nil {
return nil, nil, e
}
deployment := RevisionDeployment{}
resp, e := client.Do(req, &deployment)
if e != nil {
return nil, resp, e
}
return &deployment, resp, e
}
// Delete an API Proxy and all its revisions from an organization. This method
// will fail if any of the revisions of the named API Proxy are currently deployed
// in any environment.
func (s *Deployable) Delete(client *ApigeeClient, uriPathElement, assetName string) (*DeletedItemInfo, *Response, error) {
path := path.Join(uriPathElement, assetName)
req, e := client.NewRequest("DELETE", path, nil)
if e != nil {
return nil, nil, e
}
item := DeletedItemInfo{}
resp, e := client.Do(req, &item)
if e != nil {
return nil, resp, e
}
return &item, resp, e
}
func (s *Deployable) GetDeployments(client *ApigeeClient, uriPathElement, assetName string) (*Deployment, *Response, error) {
path := path.Join(uriPathElement, assetName, "deployments")
req, e := client.NewRequest("GET", path, nil)
if e != nil {
return nil, nil, e
}
deployments := Deployment{}
resp, e := client.Do(req, &deployments)
if e != nil {
return nil, resp, e
}
return &deployments, resp, e
}