Skip to content

Instantly share code, notes, and snippets.

@smaglio81
Created October 16, 2016 17:29
Show Gist options
  • Save smaglio81/1170bd193dbbf21f78f0e342f5ccc880 to your computer and use it in GitHub Desktop.
Save smaglio81/1170bd193dbbf21f78f0e342f5ccc880 to your computer and use it in GitHub Desktop.
#
# This script will set up a full tyk environment on your machine
# and also create a demo user for you with one command
# USAGE
# -----
#
# $> ./setup.sh {IP ADDRESS OF DOCKER VM}
# OSX users will need to specify a virtual IP, linux users can use 127.0.0.1
# Proxy
$TYK_DASHBOARD_DOMAIN="tyk_dashboard"
# Tyk dashboard settings
$RANDOM = [Random]::new().Next()
$TYK_DASHBOARD_USERNAME="[email protected]"
$TYK_DASHBOARD_PASSWORD="test123"
# Tyk portal settings
$TYK_PORTAL_DOMAIN="www.tyk-portal-test.com"
$TYK_PORTAL_PATH="/portal/"
$DOCKER_IP="127.0.0.1"
<#if($DOCKER_HOST) {
Write-Host "Detected a Docker VM..."
$REMTCP="$DOCKER_HOST#tcp://"
$DOCKER_IP="$REMTCP%:*"
}#>
if($args[0]) {
$DOCKER_IP=$args[0]
Write-Host "Docker host address explicitly set."
Write-Host "Using $DOCKER_IP as Tyk host address."
}
if($args[1]) {
$TYK_PORTAL_DOMAIN=$args[1]
Write-Host "Docker portal domain address explicitly set."
Write-Host "Using $TYK_PORTAL_DOMAIN as Tyk host address."
}
if($args[0] -eq $null) {
Write-Host "Using $DOCKER_IP as Tyk host address."
Write-Host "If this is wrong, please specify the instance IP address (e.g. ./setup.sh 192.168.1.1)"
}
function Invoke-Docker([string] $UrlExt, [string] $Auth, [string] $Data, [switch] $Get, [switch] $Put) {
$uri = "http://$($DOCKER_IP):3000/$UrlExt"
$headers = New-Object 'System.Collections.Generic.Dictionary[[string],[string]]'
if($Auth) { $headers.Add("authorization", $Auth); } else { $headers.Add("admin-auth", "12345"); }
$headers.Add("Contenty-Type","application/json");
if($Get) {
$result = Invoke-WebRequest -Uri $uri -Method Get -Headers $headers
} else {
if($Put) {
$result = Invoke-WebRequest -Uri $uri -Method Put -Headers $headers -Body $Data
} else {
$result = Invoke-WebRequest -Uri $uri -Method Post -Headers $headers -Body $Data
}
}
if($result.StatusCode -ne 200) { throw "Error Calling $uri with `"$Data`".`nStatus Code = $($result.StatusCode)`nRawContent = $($result.RawContent)" }
return $result.Content
}
Write-Host "Creating Organisation"
$ORG_DATA = Invoke-Docker -UrlExt "admin/organisations" -Data '{"owner_name": "TestOrg5 Ltd.","owner_slug": "testorg", "cname_enabled":true}'
$ORG_ID = (ConvertFrom-Json $ORG_DATA).Meta
Write-Host "ORG ID: $ORG_ID"
Write-Host "Adding new user"
$USER_DATA = Invoke-Docker -UrlExt "admin/users" -Data "{`"first_name`": `"John`",`"last_name`": `"Smith`",`"email_address`": `"$TYK_DASHBOARD_USERNAME`",`"active`": true,`"org_id`": `"$ORG_ID`"}"
$USER_AUTH = (ConvertFrom-Json $USER_DATA).Message
$USER_LIST = Invoke-Docker -UrlExt "api/users" -Auth $USER_AUTH -Get
$USER_ID = (ConvertFrom-Json $USER_LIST).Users[0].id
Write-Host "USER AUTH: $USER_AUTH"
Write-Host "USER ID: $USER_ID"
Write-Host "Setting password"
$OK = Invoke-Docker -UrlExt "api/users/$USER_ID/actions/reset" -Auth $USER_AUTH -Data "{`"new_password`":`"$TYK_DASHBOARD_PASSWORD`"}"
Write-Host "Setting up the portal domain"
$OK = Invoke-Docker -UrlExt "admin/organisations/$ORG_ID/generate-portals" -Data "domain=`"$TYK_PORTAL_DOMAIN`""
Write-Host "Setting up the Portal catalogue"
$CATALOGUE_DATA = Invoke-Docker -UrlExt "api/portal/catalogue" -Auth $USER_AUTH -Data "{`"org_id`": `"$ORG_ID`"}"
$CATALOGUE_ID = (ConvertFrom-Json $CATALOGUE_DATA).Message
$OK = Invoke-Docker -UrlExt "api/portal/catalogue" -Auth $USER_AUTH -Get
Write-Host "Setting target URL for Portal APIs"
$API_LIST = Invoke-Docker -UrlExt "api/apis" -Auth $USER_AUTH -Get
$API_PORTAL_DATA = (ConvertFrom-Json $API_LIST).apis[2]
$API_PORTAL_DATA.sort_by = 9; $API_PORTAL_DATA.api_definition.proxy.target_url = $API_PORTAL_DATA.api_definition.proxy.target_url.Replace("localhost", $TYK_DASHBOARD_DOMAIN);
$API_PORTAL_DATA.api_definition.proxy.listen_path = $TYK_PORTAL_PATH;
$API_PORTAL_ID = $API_PORTAL_DATA.api_definition.id;
$API_PORTAL_API_DATA = (ConvertFrom-Json $API_LIST).Apis[1]
$API_PORTAL_API_DATA.api_definition.proxy.target_url = $API_PORTAL_API_DATA.api_definition.proxy.target_url.Replace("localhost", $TYK_DASHBOARD_DOMAIN);
$API_PORTAL_API_DATA.api_definition.proxy.listen_path = "/portal-api/";
$API_PORTAL_API_ID = $API_PORTAL_API_DATA.api_definition.id;
$API_PORTAL_ASSETS_DATA = (ConvertFrom-Json $API_LIST).Apis[0]
$API_PORTAL_ASSETS_DATA.api_definition.proxy.target_url = $API_PORTAL_ASSETS_DATA.api_definition.proxy.target_url.Replace("localhost", $TYK_DASHBOARD_DOMAIN);
$API_PORTAL_ASSETS_ID = $API_PORTAL_ASSETS_DATA.api_definition.id;
$OK = Invoke-Docker -UrlExt "api/apis/$API_PORTAL_ID" -Auth $USER_AUTH -Data "$(ConvertTo-Json $API_PORTAL_DATA -Depth 999)" -Put
$OK = Invoke-Docker -UrlExt "api/apis/$API_PORTAL_API_ID" -Auth $USER_AUTH -Data "$(ConvertTo-Json $API_PORTAL_API_DATA -Depth 999)" -Put
$OK = Invoke-Docker -UrlExt "api/apis/$API_PORTAL_ASSETS_ID" -Auth $USER_AUTH -Data "$(ConvertTo-Json $API_PORTAL_ASSETS_DATA -Depth 999)" -Put
Write-Host "Creating the Portal Home page"
$OK = Invoke-Docker -UrlExt "api/portal/pages" -Auth $USER_AUTH -Data "{`"is_homepage`": true, `"template_name`":`"`", `"title`":`"Tyk Developer Portal`", `"slug`":`"home`", `"fields`": {`"JumboCTATitle`": `"Tyk Developer Portal`", `"SubHeading`": `"Sub Header`", `"JumboCTALink`": `"#cta`", `"JumboCTALinkTitle`": `"Your awesome APIs, hosted with Tyk!`", `"PanelOneContent`": `"Panel 1 content.`", `"PanelOneLink`": `"#panel1`", `"PanelOneLinkTitle`": `"Panel 1 Button`", `"PanelOneTitle`": `"Panel 1 Title`", `"PanelThereeContent`": `"`", `"PanelThreeContent`": `"Panel 3 content.`", `"PanelThreeLink`": `"#panel3`", `"PanelThreeLinkTitle`": `"Panel 3 Button`", `"PanelThreeTitle`": `"Panel 3 Title`", `"PanelTwoContent`": `"Panel 2 content.`", `"PanelTwoLink`": `"#panel2`", `"PanelTwoLinkTitle`": `"Panel 2 Button`", `"PanelTwoTitle`": `"Panel 2 Title`"}}"
Write-Host ""
Write-Host "DONE"
Write-Host "===="
Write-Host "Login at http://$($DOCKER_IP):3000/"
Write-Host "Username: $TYK_DASHBOARD_USERNAME"
Write-Host "Password: $TYK_DASHBOARD_PASSWORD"
Write-Host "Portal: http://$TYK_PORTAL_DOMAIN$TYK_PORTAL_PATH"
Write-Host ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment