1 of 14

MAAS HTTP API with Python Requests

Brendan Donegan (Canonical/MAAS)

2 of 14

Brief introduction to MAAS

  • Metal as a service
  • Manage bare metal resources
  • Provides a variety of ways to interact
    • Web UI
    • CLI
      • HTTP API
    • Python client library (TBA)

3 of 14

Python/Requests

  • Extremely easy to get up and running
  • Few quirks
  • Prerequisites
    • requests
    • requests-oauthlib

4 of 14

Initial setup

$ virtualenv --python=python3 maashttp

$ . maashttp/bin/activate

$ pip install requests

$ pip install requests-oauthlib

5 of 14

API Key

  • Running instance of MAAS, with admin user created (at least)
  • Available in user preferences screen

6 of 14

Setting up

>>> import requests

>>> from requests_oauthlib import OAuth1

>>> MAAS_API = “http://192.168.122.230/MAAS/api/2.0/

>>> consumer_key, key, secret = “<api key>”.split(“:”)

>>> oauth = OAuth1(consumer_key, resource_owner_key=key, resource_owner_secret=secret, signature_method=”PLAINTEXT”)

>>> APP_JSON_HEADER = {“Accept”: “application/json”}

7 of 14

API documentation

https://maas.ubuntu.com/docs2.1/api.html

  • URL format
    • GET/PUT/POST/DELETE
    • Operations
    • Params
    • Object IDs in URLS

8 of 14

Reading objects

>>> response = requests.get(MAAS_API + “/machines/”, auth=oauth, headers=APP_JSON_HEADER)

>>> response.ok

>>> pprint(response.json())

>>> response.status_code

>>> response.text

>>> system_id = response.json()[0][‘system_id’]

9 of 14

Reading specific objects

>>> response = requests.get(MAAS_API + “/machines/{system_id}/”.format(system_id=system_id), auth=oauth, headers=APP_JSON_HEADER)

>>> pprint(response.json())

10 of 14

Creating objects

>>> data = {‘architecture’: ‘amd64/generic’, ‘mac_addresses’: ‘00:00:00:ff:ff:ff’, ‘power_type’: ‘virsh’}

>>> response = requests.post(MAAS_API + “/machines/”, data=data, auth=oauth, headers=APP_JSON_HEADER)

>>> response.json()

…. <JSON representation of the created machine>

11 of 14

Performing operations

>>> response = requests.post(“/api/2.0/machines/?op=set_zone”, auth=oauth, headers=APP_JSON_HEADER)

>>>

12 of 14

Updating object

>>> data = {‘hostname’: ‘foo’}

>>> response = requests.put(MAAS_API + ‘/machines/{system_id}/’.format(system_id=system_id), auth=oauth, headers=APP_JSON_HEADER)

>>> response.json()

<updated section>

...

13 of 14

Deleting objects

>>> response = requests.delete(MAAS_API + “/machines/{system_id}/”.format(system_id=system_id))

>>> requests.ok

14 of 14

Questions?