-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathphpipam_section.py
More file actions
234 lines (212 loc) · 7.11 KB
/
Copy pathphpipam_section.py
File metadata and controls
234 lines (212 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Carson Anderson <rcanderson23@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: phpipam_section
author: "Carson Anderson (@rcanderson23)"
short_description: Set the state of a section
requirements: []
version_added: "2.8"
description:
- Creates, modifies, or destroys section in phpIPAM instance if necessary.
options:
username:
description:
- username that has permission to access phpIPAM API
required: True
password:
description:
- password for username provided
required: True
url:
description:
- API url for phpIPAM instance
required: True
section:
description:
- Section name that the subnet resides in.
required: True
master_section:
description:
- Master section for the section to be nested under.
- When master_section is not defined it defaults to the root.
required: False
default: 'root'
description:
description:
- Optional description displayed next to address in phpIPAM.
required: False
state:
description:
- States whether the section should be present or absent
choices: ["present", "absent"]
required: False
default: 'present'
'''
EXAMPLES = '''
- name: Create a section
phpipam_section:
username: username
password: secret
url: "https://ipam.domain.tld/api/app/"
section: 'ansible section'
description: "optional description"
state: present
- name: Create a section nested under 'ansible section'
phpipam_section:
username: username
password: secret
url: "https://ipam.domain.tld/api/app/"
section: 'section two'
master_section: 'ansbile section'
description: "section two"
state: present
- name: Delete a section
phpipam_section:
username: username
password: secret
url: "https://ipam.domain.tld/api/app/"
section: 'section two'
state: absent
'''
RETURN = '''
output:
description: dictionary containing phpIPAM response
returned: success
type: complex
contains:
code:
description: HTTP response code
returned: success
type: int
sample: 201
success:
description: True or False depending on if ip was successfully obtained
returned: success
type: bool
sample: True
time:
description: Amount of time operation took.
returned: success
type: float
sample: 0.015
message:
description: Response message of what happened
returned: success
type: string
sample: "Address created"
id:
description: ID of section created/modified
returned: success
type: string
sample: "206"
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.phpipam import PhpIpamWrapper
def set_master_section(session, master_section):
if master_section in ('', 'root'):
return '0'
else:
return session.get_section_id(master_section)
def main():
module = AnsibleModule(
argument_spec=dict(
username=dict(type=str, required=True),
password=dict(type=str, required=True, no_log=True),
url=dict(type=str, required=True),
section=dict(type=str, required=True),
master_section=dict(type=str, required=False, default='root'),
description=dict(type=str, required=False),
state=dict(default='present', choices=['present', 'absent'])
),
supports_check_mode=False
)
result = dict(
changed=False
)
username = module.params['username']
password = module.params['password']
url = module.params['url']
section = module.params['section']
master_section = module.params['master_section']
description = module.params['description']
state = module.params['state']
session = PhpIpamWrapper(username, password, url)
try:
session.create_session()
except AttributeError:
module.fail_json(msg='Error getting authorization token', **result)
section_url = url + 'sections/'
found_section = session.get_section(section)
master_section_id = set_master_section(session, master_section)
if master_section_id is None:
module.fail_json(msg='master_section does not exist', **result)
else:
optional_args = {'masterSection': set_master_section(session, master_section),
'description': description}
if state == 'present' and found_section is None:
# Create the section since it does not exist
if optional_args['masterSection'] == '0':
del optional_args['masterSection']
creation = session.create(session,
section_url,
name=section,
**optional_args)
if creation['code'] == 201:
result['changed'] = True
result['output'] = creation
module.exit_json(**result)
elif creation['code'] == 500:
result['output'] = creation
module.exit_json(**result)
else:
result['output'] = creation
module.fail_json(msg='Something went wrong', **result)
elif state == 'present':
# Potentially modify the section if it doesn't match
value_changed = False
payload = {}
section_id = session.get_section_id(section)
for k in optional_args:
if optional_args[k] != found_section[k]:
value_changed = True
payload[k] = optional_args[k]
if value_changed:
patch_response = session.modify(session,
section_url,
id=section_id,
**payload)
result['changed'] = True
result['output'] = patch_response
module.exit_json(**result)
else:
result['output'] = patch_response
module.exit_json(**result)
else:
# Ensure the section does not exist, delete if necessary
section_info = session.get_section(section)
try:
deletion = session.remove(session,
section_url,
section_info['id'])
if deletion['code'] == 200:
result['changed'] = True
result['output'] = deletion
module.exit_json(**result)
except KeyError:
result['ouput'] = 'Section did not exist'
module.exit_json(**result)
except TypeError:
result['output'] = 'Section did not exist'
module.exit_json(**result)
if __name__ == '__main__':
main()