-
Notifications
You must be signed in to change notification settings - Fork 0
/
transip_nameserver.py
executable file
·97 lines (72 loc) · 2.69 KB
/
transip_nameserver.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2023 Roy Lenferink
# Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
---
module: transip_nameserver
author: Roy Lenferink (@rlenferink)
short_description: module for managing nameservers for a TransIP domain
version_added: "0.1.0"
description:
- Ensure the nameservers are set for the given domain.
Consult the README.md for examples for the role.
options: {}
"""
import json
import requests
from ansible.module_utils.basic import AnsibleModule
def fetch_nameservers(headers, domain_name):
response = requests.get(f'https://api.transip.nl/v6/domains/{domain_name}/nameservers', headers=headers)
if response.ok:
nameservers = response.json()["nameservers"]
return nameservers
raise Exception(response.json()["error"])
def update_nameservers(headers, domain_name, nameservers):
data = {
"nameservers": nameservers
}
response = requests.put(f'https://api.transip.nl/v6/domains/{domain_name}/nameservers', headers=headers,
data=json.dumps(data))
if response.ok:
return
raise Exception(response.json()["error"])
def main():
module = AnsibleModule(
argument_spec=dict(
token=dict(required=True, no_log=True),
domain=dict(required=True),
nameservers=dict(required=True, type='list'),
),
mutually_exclusive=[],
required_by={},
supports_check_mode=False
)
# Module parameters
token = module.params['token']
domain = module.params['domain']
nameservers = module.params['nameservers']
# Headers to use for communication with the TransIP API
request_headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
}
result = dict(changed=False, warnings=list())
try:
# Get the existing nameservers
existing_nameservers = fetch_nameservers(request_headers, domain)
# Only update the nameservers if they differ
# Do not sort them before comparing, since the order of items is important
# (e.g. primary vs secondary nameserver)
if existing_nameservers != nameservers:
update_nameservers(request_headers, domain, nameservers)
result['domain'] = domain
result['nameservers'] = nameservers
result.update(changed=True)
except Exception as e:
module.fail_json(msg=f"Error while updating nameservers: {e.args}")
module.exit_json(**result)
if __name__ == "__main__":
main()