Skip to content

Instantly share code, notes, and snippets.

@notmyname
Created April 12, 2015 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notmyname/9202ba00f2de13057116 to your computer and use it in GitHub Desktop.
Save notmyname/9202ba00f2de13057116 to your computer and use it in GitHub Desktop.
# makes request to a swift cluster
import strutils
import httpclient
import json
const
auth_url = "some.domain/with/a/path"
username = "XXX"
apikey = "YYY"
tenant_id = "ZZZ"
region = "QQQ"
type
AuthCreds = tuple[storage_url, token: string]
proc auth_v2(url, username, key, tenant_id: string): AuthCreds =
var
request_body = %{"auth": %{"passwordCredentials":
%{"username": %username, "password": %apikey},
"tenantId": %tenant_id}}
headers = "Content-Type: application/json\r\n"
resp = post(url, extraHeaders=headers, body= $request_body)
token, storage_url: string
if resp.status == "200 OK": # and resp.headers content type is json
let catalog = parseJson(resp.body)
token = ($catalog["access"]["token"]["id"])[1.. -2]
for thing in catalog["access"]["serviceCatalog"]:
var t: string = ($(thing["type"]))[1.. -2]
if t == "object-store":
for endpoint in thing["endpoints"]:
var r: string = ($(endpoint["region"]))[1.. -2]
if r == region:
storage_url = ($(endpoint["publicURL"]))[1.. -2]
return (storage_url: storage_url, token: token)
else:
continue
else:
discard # do something interesting
result = (storage_url: storage_url, token: token)
# TODO: either account listing or getting a socket to do stuff on
var creds: AuthCreds
creds = auth_v2(auth_url, username, apikey, tenant_id)
if creds.token == nil:
discard # do soemthing like log or exit
echo(creds.storage_url)
echo(creds.token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment