Skip to content

Commit

Permalink
Blueprint support now available on the command line
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Buhr committed Dec 19, 2014
1 parent 72c2bbe commit 4dd4d7d
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 43 deletions.
35 changes: 0 additions & 35 deletions apps/batch_test.json

This file was deleted.

88 changes: 88 additions & 0 deletions apps/bp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"name": "Blueprint Test",
"api_id": "b4fd294a-8771-11e4-bfb5-20c9d087eb63",
"org_id": "1",
"use_keyless": true,
"use_oauth2": false,
"oauth_meta": {
"allowed_access_types": null,
"allowed_authorize_types": null,
"auth_login_redirect": ""
},
"auth": {
"auth_header_name": ""
},
"use_basic_auth": false,
"notifications": {
"shared_secret": "",
"oauth_on_keychange_url": ""
},
"enable_signature_checking": false,
"definition": {
"location": "header",
"key": "version"
},
"version_data": {
"not_versioned": false,
"versions": {
"Blueprint Test": {
"name": "Blueprint Test",
"expires": "",
"paths": {
"ignored": null,
"white_list": null,
"black_list": null
},
"use_extended_paths": true,
"extended_paths": {
"ignored": null,
"white_list": [
{
"path": "/messages/{id}",
"method_actions": {
"DELETE": {
"action": "reply",
"code": 204,
"data": "",
"Headers": null
},
"GET": {
"action": "reply",
"code": 200,
"data": "Hello World!\n",
"Headers": {
"Content-Type": "text/plain"
}
}
}
}
],
"black_list": null
}
}
}
},
"proxy": {
"listen_path": "/b4fd294a-8771-11e4-bfb5-20c9d087eb63/",
"target_url": "http://httpbin.org/",
"strip_listen_path": true
},
"session_lifetime": 0,
"active": true,
"auth_provider": {
"name": "",
"storage_engine": "",
"meta": null
},
"session_provider": {
"name": "",
"storage_engine": "",
"meta": null
},
"event_handlers": {
"events": null
},
"enable_batch_request_support": false,
"enable_ip_whitelisting": false,
"allowed_ips": null
}
72 changes: 72 additions & 0 deletions blueprints/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"_version": "2.0",
"metadata": [],
"name": "Blueprint Test",
"description": "",
"resourceGroups": [
{
"name": "",
"description": "",
"resources": [
{
"name": "",
"description": "",
"uriTemplate": "/messages/{id}",
"model": {},
"parameters": [],
"actions": [
{
"name": "",
"description": "",
"method": "GET",
"parameters": [],
"examples": [
{
"name": "",
"description": "",
"requests": [],
"responses": [
{
"name": "200",
"description": "",
"headers": [
{
"name": "Content-Type",
"value": "text/plain"
}
],
"body": "Hello World!\n",
"schema": ""
}
]
}
]
},
{
"name": "",
"description": "",
"method": "DELETE",
"parameters": [],
"examples": [
{
"name": "",
"description": "",
"requests": [],
"responses": [
{
"name": "204",
"description": "",
"headers": [],
"body": "",
"schema": ""
}
]
}
]
}
]
}
]
}
]
}
83 changes: 80 additions & 3 deletions command_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/lonelycode/tykcommon"
"code.google.com/p/go-uuid/uuid"
"strings"
"fmt"
"encoding/json"
)

var CommandModeOptions = map[string]bool{
Expand All @@ -13,6 +15,8 @@ var CommandModeOptions = map[string]bool{
"--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/"`
Expand All @@ -25,8 +29,6 @@ func HandleCommandModeArgs(arguments map[string]interface{}) {
}

func handleBluePrintMode(arguments map[string]interface{}) {
log.Info("Importing Blueprint")

doCreate := arguments["--create-api"]
inputFile := arguments["--import-blueprint"]
if doCreate == true {
Expand All @@ -40,14 +42,69 @@ func handleBluePrintMode(arguments map[string]interface{}) {
return
}

createDefFromBluePrint(bp, orgId.(string), upstreamVal.(string), arguments["--as-mock"].(bool))
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) {
Expand All @@ -59,6 +116,7 @@ func createDefFromBluePrint(bp *BluePrintAST, orgId, upstreamURL string, as_mock
thisAD.OrgID = orgId
thisAD.VersionDefinition.Key = "version"
thisAD.VersionDefinition.Location ="header"
thisAD.VersionData.Versions = make(map[string]tykcommon.VersionInfo)
thisAD.Proxy.ListenPath = "/" + thisAD.APIID + "/"
thisAD.Proxy.StripListenPath = true
thisAD.Proxy.TargetURL = upstreamURL
Expand Down Expand Up @@ -91,3 +149,22 @@ func bluePrintLoadFile(filePath string) (*BluePrintAST, error) {
thisBlueprint.ReadString(string(bluePrintFileData))
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
}
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (

// Display configuration options
func displayConfig() {
log.Info(fmt.Sprintf("Listening on port: ", config.ListenPort))
log.Info("Listening on port: ", config.ListenPort)
}

// Create all globals and init connection handlers
Expand Down Expand Up @@ -295,6 +295,8 @@ func init() {
--org-id=><id> Assign the API Defintition to this org_id (required with create)
--upstream-target=<url> Set the upstream target for the definition
--as-mock Creates the API as a mock based on example fields
--for-api=<path> Adds blueprint to existing API Defintition as version
--as-version=<version> The version number to use when inserting
`

arguments, err := docopt.Parse(usage, nil, true, "v1.2.1", false, false)
Expand All @@ -303,19 +305,16 @@ func init() {
}

// Enable command mode
cmdMessage := "Command mode activated, this application will quite after operations are completed."
for k, _ := range(CommandModeOptions) {

v := arguments[k]

if v == true {
log.Info(cmdMessage)
HandleCommandModeArgs(arguments)
os.Exit(0)
}

if v != nil && v != false {
log.Info(cmdMessage)
HandleCommandModeArgs(arguments)
os.Exit(0)
}
Expand Down
5 changes: 4 additions & 1 deletion middleware_version_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import (
"github.com/lonelycode/tykcommon"
"errors"
"net/http"
"fmt"
)

// VersionCheck will check whether the version of the requested API the request is accessing has any restrictions on URL endpoints
Expand Down Expand Up @@ -49,7 +50,9 @@ func (v *VersionCheck) ProcessRequest(w http.ResponseWriter, r *http.Request, co
for header, value := range(thisMeta.Headers) {
w.Header().Add(header, value)
}
DoJSONWrite(w, thisMeta.Code, responseMessage)

w.WriteHeader(thisMeta.Code)
fmt.Fprintf(w, string(responseMessage))
return nil, 666
}

Expand Down

0 comments on commit 4dd4d7d

Please sign in to comment.