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
107 lines (100 loc) · 4.65 KB
/
importAPI.go
File metadata and controls
107 lines (100 loc) · 4.65 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
/*
* 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 cmd
import (
"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/credentials"
"github.com/wso2/product-apim-tooling/import-export-cli/impl"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
)
var (
importAPIFile string
importEnvironment string
importAPICmdPreserveProvider bool
importAPIUpdate bool
importAPIParamsFile string
importAPISkipCleanup bool
importAPIRotateRevision bool
importAPISkipDeployments bool
dryRun bool
apiLoggingCmdFormat string
)
const (
// ImportAPI command related usage info
ImportAPICmdLiteral = "api"
importAPICmdShortDesc = "Import API"
importAPICmdLongDesc = "Import an API to an environment"
)
const importAPICmdExamples = utils.ProjectName + ` ` + ImportCmdLiteral + ` ` + ImportAPICmdLiteral + ` -f qa/TwitterAPI.zip -e dev
` + utils.ProjectName + ` ` + ImportCmdLiteral + ` ` + ImportAPICmdLiteral + ` -f staging/FacebookAPI.zip -e production
` + utils.ProjectName + ` ` + ImportCmdLiteral + ` ` + ImportAPICmdLiteral + ` -f ~/myapi -e production --update --rotate-revision
` + utils.ProjectName + ` ` + ImportCmdLiteral + ` ` + ImportAPICmdLiteral + ` -f ~/myapi -e production --update
NOTE: Both the flags (--file (-f) and --environment (-e)) are mandatory`
// ImportAPICmd represents the importAPI command
var ImportAPICmd = &cobra.Command{
Use: ImportAPICmdLiteral + " --file <path-to-api> --environment " +
"<environment>",
Short: importAPICmdShortDesc,
Long: importAPICmdLongDesc,
Example: importAPICmdExamples,
Run: func(cmd *cobra.Command, args []string) {
utils.Logln(utils.LogPrefixInfo + ImportAPICmdLiteral + " called")
cred, err := GetCredentials(importEnvironment)
if err != nil {
utils.HandleErrorAndExit("Error getting credentials", err)
}
accessOAuthToken, err := credentials.GetOAuthAccessToken(cred, importEnvironment)
if err != nil {
utils.HandleErrorAndExit("Error while getting an access token for importing API", err)
}
err = impl.ImportAPIToEnv(accessOAuthToken, importEnvironment, importAPIFile, importAPIParamsFile, importAPIUpdate,
importAPICmdPreserveProvider, importAPISkipCleanup, importAPIRotateRevision, importAPISkipDeployments, dryRun,
apiLoggingCmdFormat)
if err != nil {
utils.HandleErrorAndExit("Error importing API", err)
return
}
},
}
// init using Cobra
func init() {
ImportCmd.AddCommand(ImportAPICmd)
ImportAPICmd.Flags().StringVarP(&importAPIFile, "file", "f", "",
"Name of the API to be imported")
ImportAPICmd.Flags().StringVarP(&importEnvironment, "environment", "e",
"", "Environment from the which the API should be imported")
ImportAPICmd.Flags().BoolVar(&importAPICmdPreserveProvider, "preserve-provider", true,
"Preserve existing provider of API after importing")
ImportAPICmd.Flags().BoolVar(&importAPIUpdate, "update", false, "Update an "+
"existing API or create a new API")
ImportAPICmd.Flags().BoolVar(&importAPIRotateRevision, "rotate-revision", false, "Rotate the "+
"revisions with each update")
ImportAPICmd.Flags().BoolVar(&importAPISkipDeployments, "skip-deployments", false, "Update only "+
"the working copy and skip deployment steps in import")
ImportAPICmd.Flags().StringVarP(&importAPIParamsFile, "params", "", "", "Provide an API Manager params file "+
"or a directory generated using \"gen deployment-dir\" command")
ImportAPICmd.Flags().BoolVarP(&importAPISkipCleanup, "skip-cleanup", "", false, "Leave "+
"all temporary files created during import process")
ImportAPICmd.Flags().BoolVarP(&dryRun, "dry-run", "", false, "Get "+
"verification of the governance compliance of the API without importing it")
ImportAPICmd.Flags().StringVarP(&apiLoggingCmdFormat, "format", "", "", "Output format of violation results in "+
"dry-run mode. Supported formats: [table, json, list]. If not provided, the default format is table.")
// Mark required flags
_ = ImportAPICmd.MarkFlagRequired("environment")
_ = ImportAPICmd.MarkFlagRequired("file")
}