.. _walkthru-code: ################################# Space Walkthrough - Complete Code ################################# The following is all the code snippets from :ref:`space-walkthru`, collected and rearranged into a single script. .. code-block:: python from __future__ import print_function import requests from time import sleep from arcapix.config import config def main(): url = "https://localhost" # --- Get an auth Token --- authserver = config['arcapix.auth.server.url'] # typically https://localhost payload = {'grant_type': 'password', 'username': 'myuser', 'password': 'mypassword'} resp = requests.post(authserver + '/oauth2/token', data=payload) assert resp.status_code == 200 token = resp.json()['access_token'] # --- Set up a requests Session --- session = requests.Session() session.auth = (token, '') # if you get SSLErrors from the self-signed certificates, uncomment the following # session.verify = False # --- Check we have permission to create spaces --- resp = session.options(url + '/spaces/') assert 'POST' in resp.headers['Allow'] # --- Find a profile for the filesystem 'mmfs1', pool 'sas1' --- params = {'where': '{"name": "mmfs1-sas1"}'} resp = session.get(url + '/profiles/', params=params) assert resp.headers['X-Total-Count'] == '1' # note: string profile = resp.json()['collection']['items'][0]['href'] # --- Find an exposer for the filesystem mmfs1 --- params = {'where': '{"type": "gpfsnative", "name": "mmfs1"}'} resp = session.get(url + '/exposers/', params=params) exposer = resp.json()['collection']['items'][0]['href'] # --- Create a template from directory '/mmfs1/project_template' --- template_data = { "template": { "data": [ {"name": "name", "value": "project_template"}, {"name": "type", "value": "filesystemtemplate"}, {"name": "template_location", "value": "/mmfs1/project_template"} ] } } resp = session.post( url + '/templates/', json=template_data, headers={"Content-Type": "application/vnd.collection+json"} ) # wait for template to become ACTIVE checkurl = resp.headers['Location'] wait_until_active(session, checkurl) resp = session.get(resp.headers['Location']) template = resp.json()['collection']['items'][0]['href'] # --- Check template creation job logs --- href = get_link_href(resp.json(), 0, 'Jobs') resp = session.get(url + href) href = get_link_href(resp.json(), 0, "stderr") resp = session.get(url + href) print(resp.text) # --- Create the space --- space_data = { "template": { "data": [ {"name": "name", "value": "sleepy-snake"}, {"name": "relativepath", "value": "projects/sleepy_snake"}, {"name": "exposers", "value": exposer}, {"name": "profile", "value": profile}, {"name": "templates", "value": template}, {"name": "size", "value": 4 * 1024 * 1024 * 1024} # 4GB ] } } resp = session.post( url + '/spaces/', json=space_data, headers={"Content-Type": "application/vnd.collection+json"} ) # wait for the space to become ACTIVE checkurl = resp.headers['Location'] wait_until_active(session, checkurl) # Get the ID of the newly created space params = {'where': '{"name": "sleep-snake"}'} resp = session.get(url + '/spaces/', params=params) print(resp.json()) space = resp.json()['collection']['items'][0]['href'] # --- Create a snapshot of the space --- snapshot_data = { "template": { "data": [ {"name": "name", "value": "snake-snap"}, {"name": "type", "value": "gpfsspacesnapshot"}, {"name": "space", "value": space}, ] } } resp = session.post( url + '/snapshots/', json=snapshot_data, headers={"Content-Type": "application/vnd.collection+json"} ) # wait for the snapshot to become ACTIVE checkurl = resp.headers['Location'] wait_until_active(session, checkurl) # --- Helper Functions --- def get_status(collection, item): for data in collection['collection']['items'][item]['data']: if data['name'] == 'status': return data['value'] else: raise KeyError("Status field not found") def get_link_href(collection, item, name): for link in collection['collection']['items'][item]['links']: if link['name'] == name: return link['href'] else: raise KeyError(name) def wait_until_active(session, checkurl, timeout=10): timeout = int(timeout) for _ in range(timeout): sleep(1) resp = session.get(checkurl) status = get_status(resp.json(), 0) if status == 'ACTIVE': break else: raise Exception( "Item didn't become active after %ss - got status '%s'" % (timeout, status) ) if __name__ == '__main__': main()