David K. Bainbridge | 6e23ac8 | 2016-12-07 12:55:41 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | DOCUMENTATION = ''' |
| 4 | --- |
| 5 | module: maas_boot_resources |
| 6 | short_description: Manage MAAS boot resources |
| 7 | options: |
| 8 | maas: |
| 9 | description: |
| 10 | - URL of MAAS server |
| 11 | default: http://localhost/MAAS/api/1.0/ |
| 12 | key: |
| 13 | description: |
| 14 | - MAAS API key |
| 15 | required: yes |
| 16 | state: |
| 17 | description: |
| 18 | - possible states for this sshkey |
| 19 | choices: ['query', 'import'] |
| 20 | default: query |
| 21 | |
| 22 | requirements: [ipaddress, requests_oauthlib, maasclient] |
| 23 | author: David Bainbridge |
| 24 | ''' |
| 25 | |
| 26 | EXAMPLES = ''' |
| 27 | examples: |
| 28 | maas_boot_resource: |
| 29 | maas: http://my.maas.server.com/MAAS/api/1.0/ |
| 30 | key: 'xBvr9dx5k7S52myufC:fqBXV7hJgXegNZDw9c:K8hsmL47XjAppfQy2pDVW7G49p6PELgp' |
| 31 | state: query |
| 32 | ''' |
| 33 | |
| 34 | import sys |
| 35 | import json |
| 36 | import ipaddress |
| 37 | import requests |
| 38 | from maasclient.auth import MaasAuth |
| 39 | from maasclient import MaasClient |
| 40 | |
| 41 | # For some reason the maasclient doesn't provide a put method. So |
| 42 | # we will add it here |
| 43 | def put(client, url, params=None): |
| 44 | return requests.put(url=client.auth.api_url + url, |
| 45 | auth=client._oauth(), data=params) |
| 46 | |
| 47 | # Attempt to interpret the given value as a JSON object, if that fails |
| 48 | # just return it as a string |
| 49 | def string_or_object(val): |
| 50 | try: |
| 51 | return json.loads(val) |
| 52 | except: |
| 53 | return val |
| 54 | |
| 55 | # Return a copy of the given dictionary with any `null` valued entries |
| 56 | # removed |
| 57 | def remove_null(d_in): |
| 58 | d = d_in.copy() |
| 59 | to_remove = [] |
| 60 | for k in d.keys(): |
| 61 | if d[k] == None: |
| 62 | to_remove.append(k) |
| 63 | for k in to_remove: |
| 64 | del d[k] |
| 65 | return d |
| 66 | |
| 67 | def filter(filter_type, d, keys): |
| 68 | if filter_type == 'include': |
| 69 | for k in d.keys(): |
| 70 | if k not in keys: |
| 71 | d.pop(k, None) |
| 72 | else: |
| 73 | for k in d.keys(): |
| 74 | if k in keys: |
| 75 | d.pop(k, None) |
| 76 | |
| 77 | def main(): |
| 78 | module = AnsibleModule( |
| 79 | argument_spec = dict( |
| 80 | maas=dict(default='http://localhost/MAAS/api/1.0/'), |
| 81 | key=dict(required=True), |
| 82 | state=dict(default='query', choices=['query', 'import']) |
| 83 | ), |
| 84 | supports_check_mode = False |
| 85 | ) |
| 86 | |
| 87 | maas = module.params['maas'] |
| 88 | key = module.params['key'] |
| 89 | state = module.params['state'] |
| 90 | |
| 91 | # Authenticate into MAAS |
| 92 | auth = MaasAuth(maas, key) |
| 93 | maas = MaasClient(auth) |
| 94 | |
| 95 | if state == 'query': |
| 96 | res = maas.get('/boot-resources/') |
| 97 | if res.ok: |
| 98 | module.exit_json(changed=False, resources=json.loads(res.text)) |
| 99 | else: |
| 100 | module.fail_json(msg=string_or_object(res.text)) |
| 101 | elif state == 'import': |
| 102 | res = maas.post('/boot-resources/', dict(op='import')) |
| 103 | if res.ok: |
| 104 | module.exit_json(changed=True) |
| 105 | else: |
| 106 | module.fail_json(msg=string_or_object(res.text)) |
| 107 | else: |
| 108 | module.fail_json(msg='unknown state') |
| 109 | |
| 110 | # this is magic, see lib/ansible/module_common.py |
| 111 | #<<INCLUDE_ANSIBLE_MODULE_COMMON>> |
| 112 | if __name__ == '__main__': |
| 113 | main() |