forked from DinoChiesa/go-apigee-edge
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathshared_flows.go
293 lines (257 loc) · 10.1 KB
/
shared_flows.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
package apigee
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
)
const sharedFlows = "sharedflows"
// SharedFlowService is an interface for interfacing with the Apigee Edge Admin API
// dealing with shardFlows.
type SharedFlowService interface {
List() ([]string, *Response, error)
Get(string) (*SharedFlow, *Response, error)
Deploy(string, string, Revision, int, bool) (*SharedFlowRevisionDeployment, *Response, error)
Import(string, string) (*SharedFlowRevision, *Response, error)
Delete(string) (*DeletedSharedFlowInfo, *Response, error)
GetDeployments(string) (*SharedFlowDeployment, *Response, error)
ReDeploy(string, string, Revision, int, bool) (*SharedFlowRevisionDeployments, *Response, error)
Undeploy(string, string, Revision) (*SharedFlowRevisionDeployment, *Response, error)
}
// SharedFlowRevision holds information about a revision of a shared flow.
type SharedFlowRevision struct {
CreatedBy string `json:"createdBy,omitempty"`
CreatedAt Timestamp `json:"createdAt,omitempty"`
Description string `json:"description,omitempty"`
ContextInfo string `json:"contextInfo,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Name string `json:"name,omitempty"`
LastModifiedBy string `json:"lastModifiedBy,omitempty"`
LastModifiedAt Timestamp `json:"lastModifiedAt,omitempty"`
Revision Revision `json:"revision,omitempty"`
TargetEndpoints []string `json:"targetEndpoints,omitempty"`
TargetServers []string `json:"targetServers,omitempty"`
Resources []string `json:"resources,omitempty"`
Policies []string `json:"policies,omitempty"`
Type string `json:"type,omitempty"`
}
type SharedFlowServiceOp struct {
client *EdgeClient
}
type SharedFlow struct {
Revisions []Revision `json:"revision,omitempty"`
Name string `json:"name,omitempty"`
MetaData SharedFlowMetadata `json:"metaData,omitempty"`
}
// SharedFlowMetadata contains information related to the creation and last modified
// time and actor for a shared flow within an organization.
type SharedFlowMetadata struct {
LastModifiedBy string `json:"lastModifiedBy,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
LastModifiedAt Timestamp `json:"lastModifiedAt,omitempty"`
CreatedAt Timestamp `json:"createdAt,omitempty"`
}
// SharedFlowRevisionDeployment holds information about the deployment state of a
// single revision of a shared flow.
type SharedFlowRevisionDeployment struct {
Name string `json:",omitempty"`
Revision Revision `json:"revision,omitempty"`
Environment string `json:"environment,omitempty"`
Organization string `json:"organization,omitempty"`
State string `json:"state,omitempty"`
Servers []EdgeServer `json:"server,omitempty"`
}
// SharedFlowRevisionDeployments holds information about the deployment state of a
// single revision of a shared flow across environments
type SharedFlowRevisionDeployments struct {
Name string `json:"name,omitempty"`
Environments []SharedFlowRevisionDeployment `json:"environment,omitempty"`
Organization string `json:"organization,omitempty"`
}
// SharedFlowDeployment holds information about the deployment state of
// all revisions of a shared flow
type SharedFlowDeployment struct {
Environments []EnvironmentDeployment `json:"environment,omitempty"`
Name string `json:"name,omitempty"`
Organization string `json:"organization,omitempty"`
}
// DeletedSharedFlowInfo is a payload that contains very little useful
// information. This struct deserializes that information.
type DeletedSharedFlowInfo struct {
Name string `json:"name,omitempty"`
}
// Get retrieves the information about a SharedFlow in an organization, information including
// the list of available revisions, and the created and last modified dates and actors.
func (s *SharedFlowServiceOp) Get(name string) (*SharedFlow, *Response, error) {
path := path.Join(sharedFlows, name)
req, err := s.client.NewRequest("GET", path, nil, "")
if err != nil {
return nil, nil, err
}
sharedFlow := &SharedFlow{}
resp, err := s.client.Do(req, sharedFlow)
if err != nil {
return nil, nil, err
}
return sharedFlow, resp, err
}
// List retrieves the list of sharedFlow names for the organization referred by the EdgeClient.
func (s *SharedFlowServiceOp) List() ([]string, *Response, error) {
req, err := s.client.NewRequest("GET", sharedFlows, nil, "")
if err != nil {
return nil, nil, err
}
namelist := make([]string, 0)
resp, err := s.client.Do(req, &namelist)
if err != nil {
return nil, resp, err
}
return namelist, resp, err
}
// Deploy a revision of a ShareFlow to a specific environment within an organization.
func (s *SharedFlowServiceOp) Deploy(name, env string, rev Revision, delay int, override bool) (*SharedFlowRevisionDeployment, *Response, error) {
// TODO test this after creating a new one
deployURL, err := url.Parse(path.Join("environments", env, sharedFlows, name, "revisions", fmt.Sprintf("%d", rev), "deployments"))
if err != nil {
return nil, nil, nil
}
q := deployURL.Query()
q.Add("override", strconv.FormatBool(override))
q.Add("delay", fmt.Sprintf("%d", delay))
deployURL.RawQuery = q.Encode()
path := deployURL.String()
req, err := s.client.NewRequest("POST", path, nil, "application/x-www-form-urlencoded")
if err != nil {
return nil, nil, err
}
deployment := SharedFlowRevisionDeployment{}
resp, e := s.client.Do(req, &deployment)
return &deployment, resp, e
}
// Import an SharedFlow into an organization, creating a new shared flow revision.
// The sharedflow can be passed as "nil" in which case the name is derived from the source.
// The source can be either a filesystem directory containing an exploded shared flow bundle, OR
// the path of a zip file containing an SharedFlow bundle. Returns the shared flow revision information.
// This method does not deploy the imported shared flow. See the Deploy method.
func (s *SharedFlowServiceOp) Import(name string, source string) (*SharedFlowRevision, *Response, error) {
info, err := os.Stat(source)
if err != nil {
return nil, nil, err
}
zipfileName := source
log.Printf("[INFO] *** Import *** isDir: %#v\n", info.IsDir())
if info.IsDir() {
// create a temporary zip file
if name == "" {
name = filepath.Base(source)
}
log.Printf("[INFO] *** Import *** sharedFlowName: %#v\n", name)
tempDir, err := ioutil.TempDir("", "go-apigee-edge-")
if err != nil {
log.Printf("[ERROR] *** Import *** error: %#v\n", err)
return nil, nil, fmt.Errorf("while creating temp dir, error: %#v", err)
}
log.Printf("[INFO] *** Import *** tempDir: %#v\n", tempDir)
log.Printf("[INFO] *** Import *** sourceDir: %#v\n", source)
zipfileName = path.Join(tempDir, "sharedflow.zip")
err = zipDirectory(path.Join(source, "sharedflowbundle"), zipfileName, smartFilter)
if err != nil {
return nil, nil, fmt.Errorf("while creating temp dir, error: %#v", err)
}
log.Printf("[INFO] *** zipped %s into %s\n\n", source, 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
}
origURL, err := url.Parse(sharedFlows)
if err != nil {
return nil, nil, err
}
q := origURL.Query()
q.Add("action", "import")
q.Add("name", name)
origURL.RawQuery = q.Encode()
path := origURL.String()
ioreader, err := os.Open(zipfileName)
if err != nil {
return nil, nil, err
}
defer ioreader.Close()
req, err := s.client.NewRequest("POST", path, ioreader, "")
if err != nil {
return nil, nil, err
}
sharedFlowRevision := SharedFlowRevision{}
resp, err := s.client.Do(req, &sharedFlowRevision)
if err != nil {
return nil, resp, err
}
return &sharedFlowRevision, resp, err
}
// Delete an SharedFlow and all its revisions from an organization. This method
// will fail if any of the revisions of the named shared flow are currently deployed
// in any environment.
func (s *SharedFlowServiceOp) Delete(name string) (*DeletedSharedFlowInfo, *Response, error) {
path := path.Join(sharedFlows, name)
req, e := s.client.NewRequest("DELETE", path, nil, "")
if e != nil {
return nil, nil, e
}
sharedFlow := DeletedSharedFlowInfo{}
resp, err := s.client.Do(req, &sharedFlow)
if err != nil {
return nil, resp, err
}
return &sharedFlow, resp, err
}
// GetDeployments retrieves the information about deployments of a shared flow in
// an organization, including the environment names and revision numbers.
func (s *SharedFlowServiceOp) GetDeployments(name string) (*SharedFlowDeployment, *Response, error) {
path := path.Join(sharedFlows, name, "deployments")
req, e := s.client.NewRequest("GET", path, nil, "")
if e != nil {
return nil, nil, e
}
deployments := SharedFlowDeployment{}
resp, e := s.client.Do(req, &deployments)
if e != nil {
return nil, resp, e
}
return &deployments, resp, e
}
func (s *SharedFlowServiceOp) ReDeploy(sharedFlowName, env string, rev Revision, delay int, override bool) (*SharedFlowRevisionDeployments, *Response, error) {
req, e := prepareDeployRequest(sharedFlowName, env, sharedFlows, rev, delay, override, s.client)
deployment := SharedFlowRevisionDeployments{}
resp, e := s.client.Do(req, &deployment)
return &deployment, resp, e
}
// Undeploy a specific revision of a shared flow from a particular environment within an Edge organization.
func (s *SharedFlowServiceOp) Undeploy(sharedFlowName, env string, rev Revision) (*SharedFlowRevisionDeployment, *Response, error) {
path := path.Join("environments", env, sharedFlows, sharedFlowName, "revisions", fmt.Sprintf("%d", rev), "deployments")
// append the query params
origURL, err := url.Parse(path)
if err != nil {
return nil, nil, err
}
path = origURL.String()
req, e := s.client.NewRequest("DELETE", path, nil, "")
if e != nil {
return nil, nil, e
}
deployment := SharedFlowRevisionDeployment{}
resp, e := s.client.Do(req, &deployment)
if e != nil {
return nil, resp, e
}
return &deployment, resp, e
}