summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/general/plugins/module_utils/gandi_livedns_api.py
blob: 824fea46e749e74084f5bd5cfaac20932ac60db2 (plain)
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
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Gregory Thiemonge <gregory.thiemonge@gmail.com>
# Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
# SPDX-License-Identifier: BSD-2-Clause

from __future__ import absolute_import, division, print_function
__metaclass__ = type

import json

from ansible.module_utils.common.text.converters import to_native, to_text
from ansible.module_utils.urls import fetch_url


class GandiLiveDNSAPI(object):

    api_endpoint = 'https://api.gandi.net/v5/livedns'
    changed = False

    error_strings = {
        400: 'Bad request',
        401: 'Permission denied',
        404: 'Resource not found',
    }

    attribute_map = {
        'record': 'rrset_name',
        'type': 'rrset_type',
        'ttl': 'rrset_ttl',
        'values': 'rrset_values'
    }

    def __init__(self, module):
        self.module = module
        self.api_key = module.params['api_key']
        self.personal_access_token = module.params['personal_access_token']

    def _build_error_message(self, module, info):
        s = ''
        body = info.get('body')
        if body:
            errors = module.from_json(body).get('errors')
            if errors:
                error = errors[0]
                name = error.get('name')
                if name:
                    s += '{0} :'.format(name)
                description = error.get('description')
                if description:
                    s += description
        return s

    def _gandi_api_call(self, api_call, method='GET', payload=None, error_on_404=True):
        authorization_header = (
            'Bearer {0}'.format(self.personal_access_token)
            if self.personal_access_token
            else 'Apikey {0}'.format(self.api_key)
        )
        headers = {'Authorization': authorization_header,
                   'Content-Type': 'application/json'}
        data = None
        if payload:
            try:
                data = json.dumps(payload)
            except Exception as e:
                self.module.fail_json(msg="Failed to encode payload as JSON: %s " % to_native(e))

        resp, info = fetch_url(self.module,
                               self.api_endpoint + api_call,
                               headers=headers,
                               data=data,
                               method=method)

        error_msg = ''
        if info['status'] >= 400 and (info['status'] != 404 or error_on_404):
            err_s = self.error_strings.get(info['status'], '')

            error_msg = "API Error {0}: {1}".format(err_s, self._build_error_message(self.module, info))

        result = None
        try:
            content = resp.read()
        except AttributeError:
            content = None

        if content:
            try:
                result = json.loads(to_text(content, errors='surrogate_or_strict'))
            except (getattr(json, 'JSONDecodeError', ValueError)) as e:
                error_msg += "; Failed to parse API response with error {0}: {1}".format(to_native(e), content)

        if error_msg:
            self.module.fail_json(msg=error_msg)

        return result, info['status']

    def build_result(self, result, domain):
        if result is None:
            return None

        res = {}
        for k in self.attribute_map:
            v = result.get(self.attribute_map[k], None)
            if v is not None:
                if k == 'record' and v == '@':
                    v = ''
                res[k] = v

        res['domain'] = domain

        return res

    def build_results(self, results, domain):
        if results is None:
            return []
        return [self.build_result(r, domain) for r in results]

    def get_records(self, record, type, domain):
        url = '/domains/%s/records' % (domain)
        if record:
            url += '/%s' % (record)
            if type:
                url += '/%s' % (type)

        records, status = self._gandi_api_call(url, error_on_404=False)

        if status == 404:
            return []

        if not isinstance(records, list):
            records = [records]

        # filter by type if record is not set
        if not record and type:
            records = [r
                       for r in records
                       if r['rrset_type'] == type]

        return records

    def create_record(self, record, type, values, ttl, domain):
        url = '/domains/%s/records' % (domain)
        new_record = {
            'rrset_name': record,
            'rrset_type': type,
            'rrset_values': values,
            'rrset_ttl': ttl,
        }
        record, status = self._gandi_api_call(url, method='POST', payload=new_record)

        if status in (200, 201,):
            return new_record

        return None

    def update_record(self, record, type, values, ttl, domain):
        url = '/domains/%s/records/%s/%s' % (domain, record, type)
        new_record = {
            'rrset_values': values,
            'rrset_ttl': ttl,
        }
        record = self._gandi_api_call(url, method='PUT', payload=new_record)[0]
        return record

    def delete_record(self, record, type, domain):
        url = '/domains/%s/records/%s/%s' % (domain, record, type)

        self._gandi_api_call(url, method='DELETE')

    def delete_dns_record(self, record, type, values, domain):
        if record == '':
            record = '@'

        records = self.get_records(record, type, domain)

        if records:
            cur_record = records[0]

            self.changed = True

            if values is not None and set(cur_record['rrset_values']) != set(values):
                new_values = set(cur_record['rrset_values']) - set(values)
                if new_values:
                    # Removing one or more values from a record, we update the record with the remaining values
                    self.update_record(record, type, list(new_values), cur_record['rrset_ttl'], domain)
                    records = self.get_records(record, type, domain)
                    return records[0], self.changed

            if not self.module.check_mode:
                self.delete_record(record, type, domain)
        else:
            cur_record = None

        return None, self.changed

    def ensure_dns_record(self, record, type, ttl, values, domain):
        if record == '':
            record = '@'

        records = self.get_records(record, type, domain)

        if records:
            cur_record = records[0]

            do_update = False
            if ttl is not None and cur_record['rrset_ttl'] != ttl:
                do_update = True
            if values is not None and set(cur_record['rrset_values']) != set(values):
                do_update = True

            if do_update:
                if self.module.check_mode:
                    result = dict(
                        rrset_type=type,
                        rrset_name=record,
                        rrset_values=values,
                        rrset_ttl=ttl
                    )
                else:
                    self.update_record(record, type, values, ttl, domain)

                    records = self.get_records(record, type, domain)
                    result = records[0]
                self.changed = True
                return result, self.changed
            else:
                return cur_record, self.changed

        if self.module.check_mode:
            new_record = dict(
                rrset_type=type,
                rrset_name=record,
                rrset_values=values,
                rrset_ttl=ttl
            )
            result = new_record
        else:
            result = self.create_record(record, type, values, ttl, domain)

        self.changed = True
        return result, self.changed