-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathselectel_dns.py
319 lines (274 loc) · 10.4 KB
/
selectel_dns.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
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
#!/usr/bin/python
ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community',
'metadata_version': '1.0'}
DOCUMENTATION = '''
---
module: selectel_dns
short_description: Interface with Selectel DNS hosting.
description:
- "Manages domains and records via the Selectel DNS API, see the docs: U(https://my.selectel.ru/domains/doc)"
options:
api_token:
description:
- Account API token. If omitted, the env variable SELECTEL_API_TOKEN will be looked for.
required: false
default: null
domain:
description:
- Domain to work with. Can be the domain name (e.g. "mydomain.com") or the numeric ID of the domain in Selectel. If omitted, a list of domains will be returned.
- If domain is present but the domain doesn't exist, it will be created.
required: false
default: null
record:
description:
- Record to add, if blank a record for the domain will be created, supports the wildcard (*)
required: false
default: null
type:
description:
- The type of DNS record to create
required: false
choices: [ 'A', 'CNAME', 'MX', 'SPF', 'TXT', 'NS', 'SRV', 'AAAA' ]
default: null
ttl:
description:
- The TTL to give the new record
required: false
default: 3600 (one hour)
value:
description:
- Record value
- "Must be specified when trying to ensure a record exists"
required: false
default: null
priority:
description:
- Record priority
required: false
default: null
state:
description:
- whether the record should exist or not
required: false
choices: [ 'present', 'absent' ]
default: null
solo:
description:
- Whether the record should be the only one for that record type and record name. Only use with state=present on a record
required: false
default: null
requirements: [ selectel_dns_api ]
author: "Stanislav Popov (@popstas)"
'''
EXAMPLES = '''
# create domain
- selectel_dns:
domain: my.com
state: present
delegate_to: localhost
# delete a domain
- selectel_dns:
domain: my.com
state: absent
delegate_to: localhost
# create a test.my.com A record to point to 127.0.0.01
- selectel_dns:
domain: my.com
record: test
type: A
value: 127.0.0.1
delegate_to: localhost
# create a my.com CNAME record to example.com
- selectel_dns
domain: my.com
record: ''
type: CNAME
value: example.com
state: present
delegate_to: localhost
# change it's ttl
- selectel_dns:
domain: my.com
record: ''
type: CNAME
value: example.com
ttl: 600
state: present
delegate_to: localhost
# and delete the record
- selectel_dns:
domain: my.com
record: ''
type: CNAME
value: example.com
state: absent
delegate_to: localhost
'''
import os
import re
import sys
from ansible.module_utils.basic import AnsibleModule
try:
import selectel_dns_api
from selectel_dns_api.rest import ApiException
HAS_SELECTEL_DNS = True
except ImportError:
HAS_SELECTEL_DNS = False
def domain_idna(domain):
if sys.version_info.major == 2:
if isinstance(domain, str):
return domain.decode('utf-8').encode('idna')
else:
return domain.encode('idna')
return str(domain.encode('idna'), 'utf-8')
def main():
module = AnsibleModule(
argument_spec=dict(
api_token=dict(required=False, no_log=True),
domain=dict(required=False),
record=dict(required=False),
type=dict(required=False, choices=[
'A', 'CNAME', 'MX', 'SPF', 'TXT', 'NS', 'SRV', 'AAAA']),
ttl=dict(required=False, default=3600, type='int'),
value=dict(required=False),
priority=dict(required=False, type='int'),
state=dict(required=False, choices=['present', 'absent']),
solo=dict(required=False, type='bool'),
),
required_together=(
['record', 'value']
),
supports_check_mode=True,
)
if not HAS_SELECTEL_DNS:
module.fail_json(msg="selectel-dns-api required for this module")
api_token = module.params.get('api_token')
domain = module.params.get('domain')
record = module.params.get('record')
record_type = module.params.get('type')
ttl = module.params.get('ttl')
value = module.params.get('value')
priority = module.params.get('priority')
state = module.params.get('state')
is_solo = module.params.get('solo')
if not api_token:
if not os.environ.get('SELECTEL_API_KEY'):
module.fail_json(
msg="You must define api_token or env SELECTEL_API_KEY")
api_token = os.environ.get('SELECTEL_API_KEY')
config = selectel_dns_api.rest.Configuration()
config.api_key = {'X-Token': api_token}
domains_api = selectel_dns_api.DomainsApi()
records_api = selectel_dns_api.RecordsApi()
try:
# No domain, return a list
if not domain:
domains = domains_api.get_domains()
module.exit_json(changed=False, result=[d.name for d in domains])
else:
domain = domain_idna(domain)
# Domain & No record
if record is None:
domains = domains_api.get_domains()
dr = next((d for d in domains if domain_idna(d.name) == domain), None)
if state == 'present':
if dr:
module.exit_json(changed=False, result=dr.to_dict())
else:
if module.check_mode:
module.exit_json(changed=True)
else:
data = selectel_dns_api.NewDomain(domain)
module.exit_json(
changed=True, result=domains_api.add_domain(data).name)
elif state == 'absent':
if dr:
if not module.check_mode:
domains_api.delete_domain(dr.id)
module.exit_json(changed=True)
else:
module.exit_json(changed=False)
else:
module.fail_json(
msg="'%s' is an unknown value for the state argument" % state)
# need the not none check since record could be an empty string
if record is not None:
try:
dr = domains_api.get_domain_by_name(str(domain))
except ApiException as e:
if e.status == 404:
module.fail_json(msg="Domain not exists")
else:
module.fail_json(msg="Unable to contact Selectel: %s" % e)
if not record_type:
module.fail_json(msg="Missing the record type")
if not value:
module.fail_json(msg="Missing the record value")
if record == '' or record == '@':
record = dr.name
if not re.search('%s$' % dr.name, record):
record = '%s.%s' % (record, dr.name)
records = records_api.get_resource_records_by_domain_id(dr.id)
rr = next((r for r in records if r.name == record and r.type
== record_type and r.content == value), None)
if state == 'present':
changed = False
if is_solo:
# delete any records that have the same name and record type
same_type = [r.id for r in records if r.name
== record and r.type == record_type]
if rr:
same_type = [rid for rid in same_type if rid != rr.id]
if same_type:
if not module.check_mode:
for rid in same_type:
records_api.delete_resource_record(dr.id, rid)
changed = True
if rr:
# check if we need to update
if rr.ttl != ttl or rr.priority != priority:
data = selectel_dns_api.NewOrUpdatedRecord(
name=record, type=record_type, content=value)
if ttl:
data.ttl = ttl
if priority:
data.priority = priority
if module.check_mode:
module.exit_json(changed=True)
else:
rr = records_api.update_resource_record(dr.id, rr.id, data)
module.exit_json(changed=True, result=rr.to_dict())
else:
module.exit_json(changed=changed, result=rr.to_dict())
else:
# create it
data = selectel_dns_api.NewOrUpdatedRecord(
name=record, type=record_type, content=value)
if ttl:
data.ttl = ttl
if priority:
data.priority = priority
if module.check_mode:
module.exit_json(changed=True)
else:
rr = records_api.add_resource_record(data, dr.id)
module.exit_json(changed=True, result=rr.to_dict())
elif state == 'absent':
if rr:
if not module.check_mode:
records_api.delete_resource_record(dr.id, rr.id)
module.exit_json(changed=True)
else:
module.exit_json(changed=False)
else:
module.fail_json(
msg="'%s' is an unknown value for the state argument" % state)
except ApiException as e:
module.fail_json(msg="Unable to contact Selectel: %s" % e)
module.fail_json(msg="Unknown what you wanted me to do")
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
if __name__ == '__main__':
main()