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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: oneandone_public_ip
short_description: Configure 1&1 public IPs
description:
- Create, update, and remove public IPs.
This module has a dependency on 1and1 >= 1.0.
extends_documentation_fragment:
- community.general.attributes
attributes:
check_mode:
support: full
diff_mode:
support: none
options:
state:
description:
- Define a public ip state to create, remove, or update.
type: str
required: false
default: 'present'
choices: [ "present", "absent", "update" ]
auth_token:
description:
- Authenticating API token provided by 1&1.
type: str
api_url:
description:
- Custom API URL. Overrides the
ONEANDONE_API_URL environment variable.
type: str
required: false
reverse_dns:
description:
- Reverse DNS name. maxLength=256
type: str
required: false
datacenter:
description:
- ID of the datacenter where the IP will be created (only for unassigned IPs).
type: str
choices: [US, ES, DE, GB]
default: US
required: false
type:
description:
- Type of IP. Currently, only IPV4 is available.
type: str
choices: ["IPV4", "IPV6"]
default: 'IPV4'
required: false
public_ip_id:
description:
- The ID of the public IP used with update and delete states.
type: str
wait:
description:
- wait for the instance to be in state 'running' before returning
required: false
default: true
type: bool
wait_timeout:
description:
- how long before wait gives up, in seconds
type: int
default: 600
wait_interval:
description:
- Defines the number of seconds to wait when using the _wait_for methods
type: int
default: 5
requirements:
- "1and1"
author:
- Amel Ajdinovic (@aajdinov)
- Ethan Devenport (@edevenport)
'''
EXAMPLES = '''
- name: Create a public IP
community.general.oneandone_public_ip:
auth_token: oneandone_private_api_key
reverse_dns: example.com
datacenter: US
type: IPV4
- name: Update a public IP
community.general.oneandone_public_ip:
auth_token: oneandone_private_api_key
public_ip_id: public ip id
reverse_dns: secondexample.com
state: update
- name: Delete a public IP
community.general.oneandone_public_ip:
auth_token: oneandone_private_api_key
public_ip_id: public ip id
state: absent
'''
RETURN = '''
public_ip:
description: Information about the public ip that was processed
type: dict
sample: '{"id": "F77CC589EBC120905B4F4719217BFF6D", "ip": "10.5.132.106"}'
returned: always
'''
import os
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils.oneandone import (
get_datacenter,
get_public_ip,
OneAndOneResources,
wait_for_resource_creation_completion
)
HAS_ONEANDONE_SDK = True
try:
import oneandone.client
except ImportError:
HAS_ONEANDONE_SDK = False
DATACENTERS = ['US', 'ES', 'DE', 'GB']
TYPES = ['IPV4', 'IPV6']
def _check_mode(module, result):
if module.check_mode:
module.exit_json(
changed=result
)
def create_public_ip(module, oneandone_conn):
"""
Create new public IP
module : AnsibleModule object
oneandone_conn: authenticated oneandone object
Returns a dictionary containing a 'changed' attribute indicating whether
any public IP was added.
"""
reverse_dns = module.params.get('reverse_dns')
datacenter = module.params.get('datacenter')
ip_type = module.params.get('type')
wait = module.params.get('wait')
wait_timeout = module.params.get('wait_timeout')
wait_interval = module.params.get('wait_interval')
if datacenter is not None:
datacenter_id = get_datacenter(oneandone_conn, datacenter)
if datacenter_id is None:
_check_mode(module, False)
module.fail_json(
msg='datacenter %s not found.' % datacenter)
try:
_check_mode(module, True)
public_ip = oneandone_conn.create_public_ip(
reverse_dns=reverse_dns,
ip_type=ip_type,
datacenter_id=datacenter_id)
if wait:
wait_for_resource_creation_completion(oneandone_conn,
OneAndOneResources.public_ip,
public_ip['id'],
wait_timeout,
wait_interval)
public_ip = oneandone_conn.get_public_ip(public_ip['id'])
changed = True if public_ip else False
return (changed, public_ip)
except Exception as e:
module.fail_json(msg=str(e))
def update_public_ip(module, oneandone_conn):
"""
Update a public IP
module : AnsibleModule object
oneandone_conn: authenticated oneandone object
Returns a dictionary containing a 'changed' attribute indicating whether
any public IP was changed.
"""
reverse_dns = module.params.get('reverse_dns')
public_ip_id = module.params.get('public_ip_id')
wait = module.params.get('wait')
wait_timeout = module.params.get('wait_timeout')
wait_interval = module.params.get('wait_interval')
public_ip = get_public_ip(oneandone_conn, public_ip_id, True)
if public_ip is None:
_check_mode(module, False)
module.fail_json(
msg='public IP %s not found.' % public_ip_id)
try:
_check_mode(module, True)
public_ip = oneandone_conn.modify_public_ip(
ip_id=public_ip['id'],
reverse_dns=reverse_dns)
if wait:
wait_for_resource_creation_completion(oneandone_conn,
OneAndOneResources.public_ip,
public_ip['id'],
wait_timeout,
wait_interval)
public_ip = oneandone_conn.get_public_ip(public_ip['id'])
changed = True if public_ip else False
return (changed, public_ip)
except Exception as e:
module.fail_json(msg=str(e))
def delete_public_ip(module, oneandone_conn):
"""
Delete a public IP
module : AnsibleModule object
oneandone_conn: authenticated oneandone object
Returns a dictionary containing a 'changed' attribute indicating whether
any public IP was deleted.
"""
public_ip_id = module.params.get('public_ip_id')
public_ip = get_public_ip(oneandone_conn, public_ip_id, True)
if public_ip is None:
_check_mode(module, False)
module.fail_json(
msg='public IP %s not found.' % public_ip_id)
try:
_check_mode(module, True)
deleted_public_ip = oneandone_conn.delete_public_ip(
ip_id=public_ip['id'])
changed = True if deleted_public_ip else False
return (changed, {
'id': public_ip['id']
})
except Exception as e:
module.fail_json(msg=str(e))
def main():
module = AnsibleModule(
argument_spec=dict(
auth_token=dict(
type='str', no_log=True,
default=os.environ.get('ONEANDONE_AUTH_TOKEN')),
api_url=dict(
type='str',
default=os.environ.get('ONEANDONE_API_URL')),
public_ip_id=dict(type='str'),
reverse_dns=dict(type='str'),
datacenter=dict(
choices=DATACENTERS,
default='US'),
type=dict(
choices=TYPES,
default='IPV4'),
wait=dict(type='bool', default=True),
wait_timeout=dict(type='int', default=600),
wait_interval=dict(type='int', default=5),
state=dict(type='str', default='present', choices=['present', 'absent', 'update']),
),
supports_check_mode=True
)
if not HAS_ONEANDONE_SDK:
module.fail_json(msg='1and1 required for this module')
if not module.params.get('auth_token'):
module.fail_json(
msg='auth_token parameter is required.')
if not module.params.get('api_url'):
oneandone_conn = oneandone.client.OneAndOneService(
api_token=module.params.get('auth_token'))
else:
oneandone_conn = oneandone.client.OneAndOneService(
api_token=module.params.get('auth_token'), api_url=module.params.get('api_url'))
state = module.params.get('state')
if state == 'absent':
if not module.params.get('public_ip_id'):
module.fail_json(
msg="'public_ip_id' parameter is required to delete a public ip.")
try:
(changed, public_ip) = delete_public_ip(module, oneandone_conn)
except Exception as e:
module.fail_json(msg=str(e))
elif state == 'update':
if not module.params.get('public_ip_id'):
module.fail_json(
msg="'public_ip_id' parameter is required to update a public ip.")
try:
(changed, public_ip) = update_public_ip(module, oneandone_conn)
except Exception as e:
module.fail_json(msg=str(e))
elif state == 'present':
try:
(changed, public_ip) = create_public_ip(module, oneandone_conn)
except Exception as e:
module.fail_json(msg=str(e))
module.exit_json(changed=changed, public_ip=public_ip)
if __name__ == '__main__':
main()
|