forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_mode.go
173 lines (142 loc) · 4.44 KB
/
command_mode.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"github.com/satori/go.uuid"
"github.com/TykTechnologies/tyk/apidef"
)
var commandModeOptions = []string{
"--import-blueprint",
"--import-swagger",
"--create-api",
"--org-id",
"--upstream-target",
"--as-mock",
"--for-api",
"--as-version",
}
// ./tyk --import-blueprint=blueprint.json --create-api --org-id=<id> --upstream-target="http://widgets.com/api/"`
func handleCommandModeArgs(arguments map[string]interface{}) {
if arguments["--import-blueprint"] != nil {
handleBluePrintMode(arguments)
}
if arguments["--import-swagger"] != nil {
handleSwaggerMode(arguments)
}
}
func handleBluePrintMode(arguments map[string]interface{}) {
doCreate := arguments["--create-api"].(bool)
inputFile := arguments["--import-blueprint"]
if !doCreate {
// Different branch, here we need an API Definition to modify
forAPIPath := arguments["--for-api"]
if forAPIPath == nil {
log.Error("If ading to an API, the path to the defintiton must be listed")
return
}
versionName := arguments["--as-version"]
if versionName == nil {
log.Error("No version defined for this import operation, please set an import ID using the --as-version flag")
return
}
defFromFile, err := apiDefLoadFile(forAPIPath.(string))
if err != nil {
log.Error("failed to load and decode file data for API Definition: ", err)
return
}
bp, err := bluePrintLoadFile(inputFile.(string))
if err != nil {
log.Error("File load error: ", err)
return
}
versionData, err := bp.ConvertIntoApiVersion(arguments["--as-mock"].(bool))
if err != nil {
log.Error("onversion into API Def failed: ", err)
}
if err := bp.InsertIntoAPIDefinitionAsVersion(versionData, defFromFile, versionName.(string)); err != nil {
log.Error("Insertion failed: ", err)
return
}
printDef(defFromFile)
}
upstreamVal := arguments["--upstream-target"]
orgID := arguments["--org-id"]
if upstreamVal == nil && orgID == nil {
log.Error("No upstream target or org ID defined, these are both required")
return
}
// Create the API with the blueprint
bp, err := bluePrintLoadFile(inputFile.(string))
if err != nil {
log.Error("File load error: ", err)
return
}
def, err := createDefFromBluePrint(bp, orgID.(string), upstreamVal.(string), arguments["--as-mock"].(bool))
if err != nil {
log.Error("Failed to create API Defintition from file")
return
}
printDef(def)
}
func printDef(def *apidef.APIDefinition) {
asJSON, err := json.MarshalIndent(def, "", " ")
if err != nil {
log.Error("Marshalling failed: ", err)
}
// The id attribute is for BSON only and breaks the parser if it's empty, cull it here.
fixed := strings.Replace(string(asJSON), ` "id": "",`, "", 1)
fmt.Println(fixed)
}
func createDefFromBluePrint(bp *BluePrintAST, orgID, upstreamURL string, asMock bool) (*apidef.APIDefinition, error) {
ad := apidef.APIDefinition{
Name: bp.Name,
Active: true,
UseKeylessAccess: true,
APIID: uuid.NewV4().String(),
OrgID: orgID,
}
ad.VersionDefinition.Key = "version"
ad.VersionDefinition.Location = "header"
ad.VersionData.Versions = make(map[string]apidef.VersionInfo)
ad.Proxy.ListenPath = "/" + ad.APIID + "/"
ad.Proxy.StripListenPath = true
ad.Proxy.TargetURL = upstreamURL
versionData, err := bp.ConvertIntoApiVersion(asMock)
if err != nil {
log.Error("onversion into API Def failed: ", err)
}
err = bp.InsertIntoAPIDefinitionAsVersion(versionData, &ad, strings.Trim(bp.Name, " "))
return &ad, err
}
func bluePrintLoadFile(filePath string) (*BluePrintAST, error) {
blueprint, err := GetImporterForSource(ApiaryBluePrint)
if err != nil {
log.Error("Couldn't get blueprint importer: ", err)
return nil, err
}
bluePrintFileData, err := ioutil.ReadFile(filePath)
if err != nil {
log.Error("Couldn't load blueprint file: ", err)
return nil, err
}
if err := blueprint.ReadString(string(bluePrintFileData)); err != nil {
log.Error("Failed to decode object")
return nil, err
}
return blueprint.(*BluePrintAST), nil
}
func apiDefLoadFile(filePath string) (*apidef.APIDefinition, error) {
defFileData, err := ioutil.ReadFile(filePath)
if err != nil {
log.Error("Couldn't load API Definition file: ", err)
return nil, err
}
def := &apidef.APIDefinition{}
if err := json.Unmarshal(defFileData, &def); err != nil {
log.Error("Failed to unmarshal the JSON definition: ", err)
return nil, err
}
return def, nil
}