-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
189 lines (162 loc) · 5.54 KB
/
main.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/urfave/cli"
)
const srcApimEndpointEnv string = "SRC_WSO2_APIM_ENDPOINT"
const srcApimGatewayEndpointEnv string = "SRC_WSO2_APIM_GATEWAY_ENDPOINT"
const srcApimUsernameEnv string = "SRC_WSO2_APIM_USERNAME"
const srcApimPasswordEnv string = "SRC_WSO2_APIM_PASSWORD"
const dstApimEndpointEnv string = "DST_WSO2_APIM_ENDPOINT"
const dstApimGatewayEndpointEnv string = "DST_WSO2_APIM_GATEWAY_ENDPOINT"
const dstApimUsernameEnv string = "DST_WSO2_APIM_USERNAME"
const dstApimPasswordEnv string = "DST_WSO2_APIM_PASSWORD"
func main() {
app := cli.NewApp()
app.Name = "WSO2 API Manager CLI"
app.Usage = ""
app.Version = "0.0.1"
app.Commands = []cli.Command{
{
Name: "export",
Aliases: []string{"e"},
Usage: "Export APIs from a API Manager environment",
Action: func(c *cli.Context) error {
executeExport()
return nil
},
},
{
Name: "import",
Aliases: []string{"i"},
Usage: "Import APIs into a API Manager environment",
Action: func(c *cli.Context) error {
executeImport()
return nil
},
},
}
app.Run(os.Args)
}
func executeExport() {
srcApimEndpoint := os.Getenv(srcApimEndpointEnv)
if srcApimEndpoint == "" {
fmt.Println("Error: environment variable " + srcApimEndpointEnv + " not found")
return
}
srcApimGatewayEndpoint := os.Getenv(srcApimGatewayEndpointEnv)
if srcApimGatewayEndpoint == "" {
fmt.Println("Error: environment variable " + srcApimGatewayEndpointEnv + " not found")
return
}
srcApimUsername := os.Getenv(srcApimUsernameEnv)
if srcApimUsername == "" {
fmt.Println("Error: environment variable " + srcApimUsernameEnv + " not found")
return
}
srcApimPassword := os.Getenv(srcApimPasswordEnv)
if srcApimPassword == "" {
fmt.Println("Error: environment variable " + srcApimPasswordEnv + " not found")
return
}
if !strings.HasSuffix(srcApimGatewayEndpoint, "/") {
srcApimGatewayEndpoint = srcApimGatewayEndpoint + "/"
}
if !strings.HasSuffix(srcApimEndpoint, "/") {
srcApimEndpoint = srcApimEndpoint + "/"
}
tokenEndpoint := srcApimGatewayEndpoint + "token"
clientRegEndpoint := srcApimEndpoint + "client-registration/v0.11/register"
publisherEndpoint := srcApimEndpoint + "api/am/publisher"
exportEndpoint := srcApimEndpoint + "api-import-export-2.1.0-v2/export-api"
clientId, clientSecret := GetClientIdSecret(clientRegEndpoint, srcApimUsername, srcApimPassword)
token := GetToken(tokenEndpoint, srcApimUsername, srcApimPassword, clientId, clientSecret)
apis := GetAPIs(publisherEndpoint, token)
exportPath := "./export"
for _, api := range apis.List {
filePath, err := ExportAPI(exportEndpoint, srcApimUsername, srcApimPassword, exportPath, api)
if err != nil {
fmt.Println("Error: could not export API, " + err.Error())
continue
}
fmt.Println("API " + apiToString(api) + " exported successfully: " + filePath)
}
}
func executeImport() {
dstApimEndpoint := os.Getenv(dstApimEndpointEnv)
if dstApimEndpoint == "" {
fmt.Println("Error: environment variable " + dstApimEndpointEnv + " not found")
return
}
dstApimGatewayEndpoint := os.Getenv(dstApimGatewayEndpointEnv)
if dstApimGatewayEndpoint == "" {
fmt.Println("Error: environment variable " + dstApimGatewayEndpointEnv + " not found")
return
}
dstApimUsername := os.Getenv(dstApimUsernameEnv)
if dstApimUsername == "" {
fmt.Println("Error: environment variable " + dstApimUsernameEnv + " not found")
return
}
dstApimPassword := os.Getenv(dstApimPasswordEnv)
if dstApimPassword == "" {
fmt.Println("Error: environment variable " + dstApimPasswordEnv + " not found")
return
}
if !strings.HasSuffix(dstApimEndpoint, "/") {
dstApimEndpoint = dstApimEndpoint + "/"
}
importEndpoint := dstApimEndpoint + "api-import-export-2.1.0-v2/import-api"
searchDir := "./export/"
fileList := []string{}
err := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
if strings.HasSuffix(path, ".zip") {
fileList = append(fileList, path)
}
return nil
})
if err != nil {
fmt.Println("Error: could not read directory, " + err.Error())
}
for _, file := range fileList {
filePath, err := filepath.Abs(file)
if err != nil {
fmt.Println("Error: could not find absolute file path of file, " + err.Error())
}
fileName := filepath.Base(file)
err = ImportAPI(importEndpoint, dstApimUsername, dstApimPassword, filePath)
if err != nil {
fmt.Println("Error: could not import API " + fileName + ", " + err.Error())
continue
}
fmt.Println("API " + fileName + " imported successfully")
}
if !strings.HasSuffix(dstApimGatewayEndpoint, "/") {
dstApimGatewayEndpoint = dstApimGatewayEndpoint + "/"
}
tokenEndpoint := dstApimGatewayEndpoint + "token"
clientRegEndpoint := dstApimEndpoint + "client-registration/v0.11/register"
publisherEndpoint := dstApimEndpoint + "api/am/publisher"
publishEndpoint := publisherEndpoint + "/v0.11/apis/change-lifecycle"
clientId, clientSecret := GetClientIdSecret(clientRegEndpoint, dstApimUsername, dstApimPassword)
token := GetToken(tokenEndpoint, dstApimUsername, dstApimPassword, clientId, clientSecret)
apis := GetAPIsByStatus(publisherEndpoint, token, "CREATED")
if len(apis.List) > 0 {
for _, api := range apis.List {
if api.Status == "CREATED" {
err := PublishAPI(publishEndpoint, token, api.Id)
if err != nil {
fmt.Println("Error: could not publish API, " + err.Error())
continue
}
fmt.Println("API " + apiToString(api) + " published successfully")
}
}
}
}
func apiToString(api Api) string {
return api.Name + "-" + api.Version
}