-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetal_ip.py
281 lines (235 loc) · 7.97 KB
/
metal_ip.py
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/python
# -*- coding: utf-8 -*-
try:
from metal_python.api import IpApi
from metal_python import models
from metal_python import rest
METAL_PYTHON_AVAILABLE = True
except ImportError:
METAL_PYTHON_AVAILABLE = False
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.metal import AUTH_SPEC, ANSIBLE_CI_MANAGED_TAG, init_driver_for_module
ANSIBLE_METADATA = {
'metadata_version': '0.1',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: metal_ip
short_description: A module to manage metal ip entities
version_added: "2.8"
description:
- Manages ip entities in the metal-api.
- Requires metal_python to be installed.
options:
name:
description:
- >-
The name of the ip, which must be unique within a project
(in case ip is not provided).
Otherwise, the module cannot figure out if the ip was already created or not.
required: false
description:
description:
- The description of the ip.
required: false
ip:
description:
- The ip address to allocate.
required: false
network:
description:
- The network to allocate the ip in.
required: false
project:
description:
- The project of the ip.
required: false
type:
description:
- The type of the ip.
default: static
choices:
- static
- ephemeral
state:
description:
- Assert the state of the ip.
- >-
Use C(present) to create or update a ip and C(absent) to
delete it.
default: present
choices:
- absent
- present
author:
- metal-stack
'''
EXAMPLES = '''
- name: allocate a specific ip
metal_ip:
ip: 212.34.83.13
name: my-ip
description: "my static ip"
network: internet-fra-equ01
type: static
project: 9ec6882a-cd94-42a7-b667-ffaed43557c7
- name: allocate a random ip
metal_ip:
name: my-ip
description: "my random ip"
network: internet-fra-equ01
type: static
project: 9ec6882a-cd94-42a7-b667-ffaed43557c7
- name: free an ip
metal_ip:
ip: 212.34.83.13
state: absent
'''
RETURN = '''
ip:
description:
- ip address
returned: always
type: str
sample: 212.34.83.13
'''
class Instance(object):
def __init__(self, module):
if not METAL_PYTHON_AVAILABLE:
raise RuntimeError("metal_python must be installed")
self._module = module
self.changed = False
self._ip = None
self.ip_address = module.params.get('ip')
self._name = module.params.get('name')
self._project = module.params.get('project')
self._network = module.params.get('network')
self._description = module.params.get('description')
self._type = module.params.get('type')
self._tags = module.params.get('tags') if module.params.get('tags') else []
self._state = module.params.get('state')
self._driver = init_driver_for_module(self._module)
self._api_client = IpApi(api_client=self._driver.client)
if self.ip_address is None and (self._project is None or self._name is None):
module.fail_json(msg="either ip or name must be given")
def run(self):
if self._module.check_mode:
return
self._find()
if self._state == "present":
if self._ip:
self._update()
return
self._allocate()
self.changed = True
elif self._state == "absent":
if not self.ip_address:
self._module.fail_json(msg="ip is a required argument when state is absent")
if self._ip:
self._free()
self.changed = True
def _find(self):
if self.ip_address:
try:
self._ip = self._api_client.find_ip(self.ip_address)
except rest.ApiException as e:
if e.status != 404:
self._module.fail_json(msg="request to metal-api failed", error=str(e))
return
r = models.V1IPFindRequest(
name=self._name,
projectid=self._project,
)
try:
ips = self._api_client.find_i_ps(r)
except rest.ApiException as e:
self._module.fail_json(msg="request to metal-api failed", error=str(e))
return
if len(ips) > 1:
self._module.fail_json(
msg="multiple ips of name '%s' exist in project '%s'. "
"module idempotence depends on unique names within a project, "
"please ensure unique names or ip in params.",
project=self._project, name=self._name)
elif len(ips) == 1:
self._ip = ips[0]
self.ip_address = self._ip.ipaddress
def _update(self):
r = models.V1IPUpdateRequest(
ipaddress=self.ip_address,
type=self._ip.type,
)
if self._ip.description != self._description:
self.changed = True
r.description = self._description
if self._ip.name != self._name:
self.changed = True
r.name = self._name
for tag in self._ip.tags:
# we need to maintain tags from the metal-ccm that are required for inserting an ip address
# into the metalLB ip pool
if tag.startswith("cluster.metal-stack.io/id/namespace/service"):
self._tags.append(tag)
self._tags.append(ANSIBLE_CI_MANAGED_TAG)
if sorted(self._ip.tags) != sorted(self._tags):
self.changed = True
r.tags = self._tags
if self._ip.type != self._type:
self.changed = True
r.type = self._type
if self.changed:
try:
self._ip = self._api_client.update_ip(r)
except rest.ApiException as e:
self._module.fail_json(msg="request to metal-api failed", error=str(e))
def _allocate(self):
self._tags.append(ANSIBLE_CI_MANAGED_TAG)
r = models.V1IPAllocateRequest(
description=self._description,
name=self._name,
networkid=self._network,
projectid=self._project,
tags=self._tags,
type=self._type
)
try:
self._ip = self._api_client.allocate_ip(r)
except rest.ApiException as e:
self._module.fail_json(msg="request to metal-api failed", error=str(e))
self.ip_address = self._ip.ipaddress
def _free(self):
if ANSIBLE_CI_MANAGED_TAG not in self._ip.tags:
self._module.fail_json(msg="entity does not have label attached: %s" % ANSIBLE_CI_MANAGED_TAG,
project=self._project,
name=self._name)
try:
self._api_client.free_ip(self.ip_address)
except rest.ApiException as e:
self._module.fail_json(msg="request to metal-api failed", error=str(e))
def main():
argument_spec = AUTH_SPEC.copy()
argument_spec.update(dict(
ip=dict(type='str', required=False),
name=dict(type='str', required=False),
project=dict(type='str', required=False),
description=dict(type='str', required=False),
network=dict(type='str', required=False),
tags=dict(type='list', required=False),
type=dict(type='str', choices=['static', 'ephemeral'], default='ephemeral'),
state=dict(type='str', choices=['present', 'absent'], default='present'),
))
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
)
instance = Instance(module)
instance.run()
result = dict(
changed=instance.changed,
ip=instance.ip_address,
)
module.exit_json(**result)
if __name__ == '__main__':
main()