blob: 4abec69577f9fc683c5c092e6dd396719960f109 [file] [log] [blame]
David K. Bainbridge6e23ac82016-12-07 12:55:41 -08001#!/usr/bin/python
2
Jonathan Hart93956f52017-08-22 13:12:42 -07003# Copyright 2017-present Open Networking Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
David K. Bainbridge6e23ac82016-12-07 12:55:41 -080018DOCUMENTATION = '''
19---
20module: maas_boot_resources
21short_description: Manage MAAS boot resources
22options:
23 maas:
24 description:
25 - URL of MAAS server
26 default: http://localhost/MAAS/api/1.0/
27 key:
28 description:
29 - MAAS API key
30 required: yes
31 state:
32 description:
33 - possible states for this sshkey
34 choices: ['query', 'import']
35 default: query
36
37requirements: [ipaddress, requests_oauthlib, maasclient]
38author: David Bainbridge
39'''
40
41EXAMPLES = '''
42examples:
43 maas_boot_resource:
44 maas: http://my.maas.server.com/MAAS/api/1.0/
45 key: 'xBvr9dx5k7S52myufC:fqBXV7hJgXegNZDw9c:K8hsmL47XjAppfQy2pDVW7G49p6PELgp'
46 state: query
47'''
48
49import sys
50import json
51import ipaddress
52import requests
53from maasclient.auth import MaasAuth
54from maasclient import MaasClient
55
56# For some reason the maasclient doesn't provide a put method. So
57# we will add it here
58def put(client, url, params=None):
59 return requests.put(url=client.auth.api_url + url,
60 auth=client._oauth(), data=params)
61
62# Attempt to interpret the given value as a JSON object, if that fails
63# just return it as a string
64def string_or_object(val):
65 try:
66 return json.loads(val)
67 except:
68 return val
69
70# Return a copy of the given dictionary with any `null` valued entries
71# removed
72def remove_null(d_in):
73 d = d_in.copy()
74 to_remove = []
75 for k in d.keys():
76 if d[k] == None:
77 to_remove.append(k)
78 for k in to_remove:
79 del d[k]
80 return d
81
82def filter(filter_type, d, keys):
83 if filter_type == 'include':
84 for k in d.keys():
85 if k not in keys:
86 d.pop(k, None)
87 else:
88 for k in d.keys():
89 if k in keys:
90 d.pop(k, None)
91
92def main():
93 module = AnsibleModule(
94 argument_spec = dict(
95 maas=dict(default='http://localhost/MAAS/api/1.0/'),
96 key=dict(required=True),
97 state=dict(default='query', choices=['query', 'import'])
98 ),
99 supports_check_mode = False
100 )
101
102 maas = module.params['maas']
103 key = module.params['key']
104 state = module.params['state']
105
106 # Authenticate into MAAS
107 auth = MaasAuth(maas, key)
108 maas = MaasClient(auth)
109
110 if state == 'query':
111 res = maas.get('/boot-resources/')
112 if res.ok:
113 module.exit_json(changed=False, resources=json.loads(res.text))
114 else:
115 module.fail_json(msg=string_or_object(res.text))
116 elif state == 'import':
117 res = maas.post('/boot-resources/', dict(op='import'))
118 if res.ok:
119 module.exit_json(changed=True)
120 else:
121 module.fail_json(msg=string_or_object(res.text))
122 else:
123 module.fail_json(msg='unknown state')
124
125# this is magic, see lib/ansible/module_common.py
126#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
127if __name__ == '__main__':
128 main()