forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_mode.go
181 lines (146 loc) · 4.85 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
174
175
176
177
178
179
180
181
package main
import (
"encoding/json"
"fmt"
"github.com/lonelycode/go-uuid/uuid"
"github.com/TykTechnologies/tykcommon"
"io/ioutil"
"strings"
)
var CommandModeOptions = map[string]bool{
"--import-blueprint": true,
"--import-swagger": true,
"--create-api": true,
"--org-id": true,
"--upstream-target": true,
"--as-mock": true,
"--for-api": true,
"--as-version": true,
}
// ./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"]
inputFile := arguments["--import-blueprint"]
if doCreate == true {
upstreamVal := arguments["--upstream-target"]
orgId := arguments["--org-id"]
if upstreamVal != nil && orgId != nil {
// Create the API with the blueprint
bp, err := bluePrintLoadFile(inputFile.(string))
if err != nil {
log.Error("File load error: ", err)
return
}
def, dErr := createDefFromBluePrint(bp, orgId.(string), upstreamVal.(string), arguments["--as-mock"].(bool))
if dErr != nil {
log.Error("Failed to create API Defintition from file")
return
}
printDef(def)
return
}
log.Error("No upstream target or org ID defined, these are both required")
} else {
// 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
}
thisDefFromFile, fileErr := apiDefLoadFile(forApiPath.(string))
if fileErr != nil {
log.Error("failed to load and decode file data for API Definition: ", fileErr)
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)
}
insertErr := bp.InsertIntoAPIDefinitionAsVersion(versionData, &thisDefFromFile, versionName.(string))
if insertErr != nil {
log.Error("Insertion failed: ", insertErr)
return
}
printDef(&thisDefFromFile)
}
}
func printDef(def *tykcommon.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.Printf(fixed)
}
func createDefFromBluePrint(bp *BluePrintAST, orgId, upstreamURL string, as_mock bool) (*tykcommon.APIDefinition, error) {
thisAD := tykcommon.APIDefinition{}
thisAD.Name = bp.Name
thisAD.Active = true
thisAD.UseKeylessAccess = true
thisAD.APIID = uuid.NewUUID().String()
thisAD.OrgID = orgId
thisAD.VersionDefinition.Key = "version"
thisAD.VersionDefinition.Location = "header"
thisAD.VersionData.Versions = make(map[string]tykcommon.VersionInfo)
thisAD.VersionData.NotVersioned = false
thisAD.Proxy.ListenPath = "/" + thisAD.APIID + "/"
thisAD.Proxy.StripListenPath = true
thisAD.Proxy.TargetURL = upstreamURL
versionData, err := bp.ConvertIntoApiVersion(as_mock)
if err != nil {
log.Error("onversion into API Def failed: ", err)
}
bp.InsertIntoAPIDefinitionAsVersion(versionData, &thisAD, strings.Trim(bp.Name, " "))
return &thisAD, nil
}
func bluePrintLoadFile(filePath string) (*BluePrintAST, error) {
thisBlueprint, astErr := GetImporterForSource(ApiaryBluePrint)
if astErr != nil {
log.Error("Couldn't get blueprint importer: ", astErr)
return thisBlueprint.(*BluePrintAST), astErr
}
bluePrintFileData, err := ioutil.ReadFile(filePath)
if err != nil {
log.Error("Couldn't load blueprint file: ", err)
return thisBlueprint.(*BluePrintAST), err
}
readErr := thisBlueprint.ReadString(string(bluePrintFileData))
if readErr != nil {
log.Error("Failed to decode object")
return thisBlueprint.(*BluePrintAST), readErr
}
return thisBlueprint.(*BluePrintAST), nil
}
func apiDefLoadFile(filePath string) (tykcommon.APIDefinition, error) {
thisDef := tykcommon.APIDefinition{}
defFileData, err := ioutil.ReadFile(filePath)
if err != nil {
log.Error("Couldn't load API Definition file: ", err)
return thisDef, err
}
jsonErr := json.Unmarshal(defFileData, &thisDef)
if jsonErr != nil {
log.Error("Failed to unmarshal the JSON definition: ", jsonErr)
return thisDef, jsonErr
}
return thisDef, nil
}