forked from wso2/product-apim-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportAPI.go
More file actions
428 lines (385 loc) · 14 KB
/
importAPI.go
File metadata and controls
428 lines (385 loc) · 14 KB
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
426
427
428
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 impl
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
jsoniter "github.com/json-iterator/go"
"github.com/wso2/product-apim-tooling/import-export-cli/specs/params"
v2 "github.com/wso2/product-apim-tooling/import-export-cli/specs/v2"
"github.com/Jeffail/gabs"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
)
// JSONData represents the JSON data
type JSONData struct {
ComplianceCheck ComplianceCheck `json:"compliance-check"`
}
// ComplianceCheck represents the compliance check
type ComplianceCheck struct {
Result string `json:"result"`
Violations []Violation `json:"violations"`
}
var (
reAPIName = regexp.MustCompile(`[~!@#;:%^*()+={}|\\<>"',&/$]`)
)
// extractAPIDefinition extracts API information from jsonContent
func extractAPIDefinition(jsonContent []byte) (*v2.APIDefinitionFile, error) {
api := &v2.APIDefinitionFile{}
err := json.Unmarshal(jsonContent, &api)
if err != nil {
return nil, err
}
return api, nil
}
// resolveImportFilePath resolves the archive/directory for import
// First will resolve in given path, if not found will try to load from exported directory
func resolveImportFilePath(file, defaultExportDirectory string) (string, error) {
// check current path
utils.Logln(utils.LogPrefixInfo + "Resolving for API path...")
if _, err := os.Stat(file); os.IsNotExist(err) {
// if the file not in given path it might be inside exported directory
utils.Logln(utils.LogPrefixInfo+"Looking for API in", defaultExportDirectory)
file = filepath.Join(defaultExportDirectory, file)
if _, err := os.Stat(file); os.IsNotExist(err) {
return "", err
}
}
absPath, err := filepath.Abs(file)
if err != nil {
return "", err
}
return absPath, nil
}
// resolveYamlOrJSON for a given filepath.
// first it will look for the yaml file, if not will fallback for json
// give filename without extension so resolver will resolve for file
// fn is resolved filename, jsonContent is file as a json object, error if anything wrong happen(or both files does not exists)
func resolveYamlOrJSON(filename string) (string, []byte, error) {
// lookup for yaml
yamlFp := filename + ".yaml"
if info, err := os.Stat(yamlFp); err == nil && !info.IsDir() {
utils.Logln(utils.LogPrefixInfo+"Loading", yamlFp)
// read it
fn := yamlFp
yamlContent, err := ioutil.ReadFile(fn)
if err != nil {
return "", nil, err
}
// load it as yaml
jsonContent, err := utils.YamlToJson(yamlContent)
if err != nil {
return "", nil, err
}
return fn, jsonContent, nil
}
jsonFp := filename + ".json"
if info, err := os.Stat(jsonFp); err == nil && !info.IsDir() {
utils.Logln(utils.LogPrefixInfo+"Loading", jsonFp)
// read it
fn := jsonFp
jsonContent, err := ioutil.ReadFile(fn)
if err != nil {
return "", nil, err
}
return fn, jsonContent, nil
}
return "", nil, fmt.Errorf("%s was not found as a YAML or JSON", filename)
}
// Substitutes environment variables in the project files.
func replaceEnvVariables(apiFilePath string) error {
for _, replacePath := range utils.EnvReplaceFilePaths {
absFile := filepath.Join(apiFilePath, replacePath)
// check if the path exists. If exists, proceed with processing. Otherwise, continue with the next items
if fi, err := os.Stat(absFile); err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
switch mode := fi.Mode(); {
case mode.IsDir():
utils.Logln(utils.LogPrefixInfo+"Substituting env variables of files in folder path: ", absFile)
if strings.EqualFold(replacePath, utils.InitProjectSequences) {
err = utils.EnvSubstituteInFolder(absFile, utils.EnvReplacePoliciesFileExtensions)
} else {
err = utils.EnvSubstituteInFolder(absFile, nil)
}
case mode.IsRegular():
utils.Logln(utils.LogPrefixInfo+"Substituting env of file: ", absFile)
err = utils.EnvSubstituteInFile(absFile, nil)
}
if err != nil {
return err
}
}
}
return nil
}
// importAPI imports an API to the API manager
func importAPI(endpoint, filePath, accessToken string, extraParams map[string]string, isOauth bool, dryRun bool,
apiLoggingCmdFormat string) error {
resp, err := ExecuteNewFileUploadRequest(endpoint, extraParams, "file",
filePath, accessToken, isOauth)
utils.Logf("Response : %v", resp)
if err != nil {
utils.Logln(utils.LogPrefixError, err)
return err
}
if dryRun {
if resp.StatusCode() == http.StatusOK && resp.String() != "" {
// 200 OK
var data JSONData
err := json.Unmarshal([]byte(resp.String()), &data)
if err != nil {
utils.Logln(utils.LogPrefixError, err)
fmt.Println("Error occurred while validating API")
return errors.New(resp.Status())
}
if data.ComplianceCheck.Result == "fail" {
PrintViolations(data.ComplianceCheck.Violations, apiLoggingCmdFormat)
} else if resp.StatusCode() == http.StatusOK {
fmt.Printf("No violations found for the API")
}
} else {
// We have an HTTP error
utils.Logln(utils.LogPrefixError, err)
fmt.Println("Error occurred while validating API")
return errors.New(resp.Status())
}
} else {
if resp.StatusCode() == http.StatusCreated || resp.StatusCode() == http.StatusOK {
// 201 Created or 200 OK
fmt.Println("Successfully imported API.")
} else {
// We have an HTTP error
utils.Logln(utils.LogPrefixError, err)
return errors.New(resp.Status())
}
}
return nil
}
// ImportAPIToEnv function is used with import-api command
func ImportAPIToEnv(accessOAuthToken, importEnvironment, importPath, apiParamsPath string, importAPIUpdate,
preserveProvider, importAPISkipCleanup, importAPIRotateRevision, importAPISkipDeployments bool, dryRun bool,
apiLoggingCmdFormat string) error {
publisherEndpoint := utils.GetPublisherEndpointOfEnv(importEnvironment, utils.MainConfigFilePath)
return ImportAPI(accessOAuthToken, publisherEndpoint, importEnvironment, importPath, apiParamsPath, importAPIUpdate,
preserveProvider, importAPISkipCleanup, importAPIRotateRevision, importAPISkipDeployments, dryRun, apiLoggingCmdFormat)
}
// ImportAPI function is used with import-api command
func ImportAPI(accessOAuthToken, publisherEndpoint, importEnvironment, importPath, apiParamsPath string, importAPIUpdate,
preserveProvider, importAPISkipCleanup, importAPIRotateRevision, importAPISkipDeployments bool,
dryRun bool, apiLoggingCmdFormat string) error {
exportDirectory := filepath.Join(utils.ExportDirectory, utils.ExportedApisDirName)
resolvedAPIFilePath, err := resolveImportFilePath(importPath, exportDirectory)
if err != nil {
return err
}
utils.Logln(utils.LogPrefixInfo+"API Location:", resolvedAPIFilePath)
utils.Logln(utils.LogPrefixInfo + "Creating workspace")
tmpPath, err := utils.GetTempCloneFromDirOrZip(resolvedAPIFilePath)
if err != nil {
return err
}
defer func() {
if importAPISkipCleanup {
utils.Logln(utils.LogPrefixInfo+"Leaving", tmpPath)
return
}
utils.Logln(utils.LogPrefixInfo+"Deleting", tmpPath)
err := os.RemoveAll(tmpPath)
if err != nil {
utils.Logln(utils.LogPrefixError + err.Error())
}
}()
apiFilePath := tmpPath
utils.Logln(utils.LogPrefixInfo + "Substituting environment variables in API files...")
err = replaceEnvVariables(apiFilePath)
if err != nil {
return err
}
if importAPISkipDeployments {
//If skip deployments flag used, deployment_environments files will be removed from import artifacts
loc := filepath.Join(apiFilePath, utils.DeploymentEnvFile)
utils.Logln(utils.LogPrefixInfo + "Removing the deployment environments file from " + loc)
err := utils.RemoveFileIfExists(loc)
if err != nil {
return err
}
}
if apiParamsPath != "" {
//Reading params file of the API and add configurations into temp artifact
err := handleCustomizedParameters(apiFilePath, apiParamsPath, importEnvironment)
if err != nil {
return err
}
}
// if apiFilePath contains a directory, zip it. Otherwise, leave it as it is.
apiFilePath, err, cleanupFunc := utils.CreateZipFileFromProject(apiFilePath, importAPISkipCleanup)
if err != nil {
return err
}
//cleanup the temporary artifacts once consuming the zip file
if cleanupFunc != nil {
defer cleanupFunc()
}
extraParams := map[string]string{}
publisherEndpoint += "/apis/import"
if importAPIUpdate {
publisherEndpoint += "?overwrite=" + strconv.FormatBool(true) + "&preserveProvider=" +
strconv.FormatBool(preserveProvider) + "&rotateRevision=" + strconv.FormatBool(importAPIRotateRevision)
} else {
publisherEndpoint += "?preserveProvider=" + strconv.FormatBool(preserveProvider) + "&rotateRevision=" +
strconv.FormatBool(importAPIRotateRevision)
}
if dryRun {
publisherEndpoint += "&dryRun=" + strconv.FormatBool(true)
}
utils.Logln(utils.LogPrefixInfo + "Import URL: " + publisherEndpoint)
err = importAPI(publisherEndpoint, apiFilePath, accessOAuthToken, extraParams, true, dryRun, apiLoggingCmdFormat)
return err
}
// envParamsFileProcess function is used to process the environment parameters when they are provided as a file
func envParamsFileProcess(importPath, paramsPath, importEnvironment string) error {
apiParams, err := params.LoadApiParamsFromFile(paramsPath)
if err != nil {
return err
}
// check whether import environment is included in params configuration
envParams := apiParams.GetEnv(importEnvironment)
if envParams == nil {
return errors.New("Environment '" + importEnvironment + "' does not exist in " + paramsPath)
} else {
// Create a source directory and add source content to it and then zip it
sourceFilePath := filepath.Join(importPath, "SourceArchive")
err = utils.MoveDirectoryContentsToNewDirectory(importPath, sourceFilePath)
if err != nil {
return err
}
err, cleanupFunc := utils.CreateZipFile(sourceFilePath, false)
if err != nil {
return err
}
//cleanup the temporary artifacts once consuming the zip file
if cleanupFunc != nil {
defer cleanupFunc()
}
//If environment parameters are present in parameter file
err = handleEnvParams(importPath, importPath, envParams)
if err != nil {
return err
}
}
return nil
}
// envParamsDirectoryProcess function is used to process the environment parameters when they are provided as a
// directory
func envParamsDirectoryProcess(importPath, paramsPath, importEnvironment string) error {
apiParams, err := params.LoadApiParamsFromDirectory(paramsPath)
if err != nil {
return err
}
// check whether import environment is included in api params configuration
envParams := apiParams.GetEnv(importEnvironment)
if envParams == nil {
return errors.New("Environment '" + importEnvironment + "' does not exist in " + paramsPath)
} else {
// Create a source directory and add source content to it and then zip it
sourceFilePath := filepath.Join(importPath, "SourceArchive")
err = utils.MoveDirectoryContentsToNewDirectory(importPath, sourceFilePath)
if err != nil {
return err
}
err, cleanupFunc := utils.CreateZipFile(sourceFilePath, false)
if err != nil {
return err
}
//cleanup the temporary artifacts once consuming the zip file
if cleanupFunc != nil {
defer cleanupFunc()
}
//create new directory for deployment configurations
deploymentDirectoryPath := filepath.Join(importPath, "Deployment")
err = utils.CreateDirIfNotExist(deploymentDirectoryPath)
if err != nil {
return err
}
//copy all the content in the params directory into the artifact to be imported
err = utils.CopyDirectoryContents(paramsPath, deploymentDirectoryPath)
if err != nil {
return err
}
//If environment parameters are present in parameter file inside the deployment params directory
err = handleEnvParams(importPath, deploymentDirectoryPath, envParams)
if err != nil {
return err
}
}
return nil
}
// handleCustomizedParameters handles the configurations provided with params file of the API and the resources that needs to
// transfer to server side will bundle with the artifact to be imported.
func handleCustomizedParameters(importPath, paramsPath, importEnvironment string) error {
utils.Logln(utils.LogPrefixInfo+"Loading parameters from", paramsPath)
if strings.Contains(paramsPath, ".yaml") {
utils.Logln(utils.LogPrefixInfo+"Processing Params file", paramsPath)
err := envParamsFileProcess(importPath, paramsPath, importEnvironment)
if err != nil {
return err
}
} else {
utils.Logln(utils.LogPrefixInfo+"Processing Params in the deployment directory", paramsPath)
err := envParamsDirectoryProcess(importPath, paramsPath, importEnvironment)
if err != nil {
return err
}
}
return nil
}
// Process env params and create the intermediate_params.yaml file to pass to the server
func handleEnvParams(tempDirectory string, destDirectory string, environmentParams *params.Environment) error {
// read api params from external parameters file
if len(environmentParams.Config) == 0 {
return errors.New("configs value is empty in the provided parameters")
}
envParamsJson, err := jsoniter.Marshal(environmentParams.Config)
if err != nil {
return err
}
var apiParamsPath string
apiParams, err := gabs.ParseJSON(envParamsJson)
paramsContent, err := utils.JsonToYaml(apiParams.Bytes())
if err != nil {
return err
}
//over-write the api_params.file with latest configurations
apiParamsPath = filepath.Join(destDirectory, utils.ParamsIntermediateFile)
utils.Logln(utils.LogPrefixInfo+"Adding the Params file into", apiParamsPath)
err = ioutil.WriteFile(apiParamsPath, paramsContent, 0644)
if err != nil {
return err
}
return nil
}