|
|
@ -35,7 +35,7 @@ class DnsimpleClient(object): |
|
|
def __init__(self, token, account, sandbox): |
|
|
def __init__(self, token, account, sandbox): |
|
|
self.account = account |
|
|
self.account = account |
|
|
sess = Session() |
|
|
sess = Session() |
|
|
sess.headers.update({'Authorization': 'Bearer {}'.format(token)}) |
|
|
|
|
|
|
|
|
sess.headers.update({'Authorization': f'Bearer {token}'}) |
|
|
self._sess = sess |
|
|
self._sess = sess |
|
|
if sandbox: |
|
|
if sandbox: |
|
|
self.base = 'https://api.sandbox.dnsimple.com/v2/' |
|
|
self.base = 'https://api.sandbox.dnsimple.com/v2/' |
|
|
@ -43,7 +43,7 @@ class DnsimpleClient(object): |
|
|
self.base = 'https://api.dnsimple.com/v2/' |
|
|
self.base = 'https://api.dnsimple.com/v2/' |
|
|
|
|
|
|
|
|
def _request(self, method, path, params=None, data=None): |
|
|
def _request(self, method, path, params=None, data=None): |
|
|
url = '{}{}{}'.format(self.base, self.account, path) |
|
|
|
|
|
|
|
|
url = f'{self.base}{self.account}{path}' |
|
|
resp = self._sess.request(method, url, params=params, json=data) |
|
|
resp = self._sess.request(method, url, params=params, json=data) |
|
|
if resp.status_code == 401: |
|
|
if resp.status_code == 401: |
|
|
raise DnsimpleClientUnauthorized() |
|
|
raise DnsimpleClientUnauthorized() |
|
|
@ -53,7 +53,7 @@ class DnsimpleClient(object): |
|
|
return resp |
|
|
return resp |
|
|
|
|
|
|
|
|
def zone(self, name): |
|
|
def zone(self, name): |
|
|
path = '/zones/{}'.format(name) |
|
|
|
|
|
|
|
|
path = f'/zones/{name}' |
|
|
return self._request('GET', path).json() |
|
|
return self._request('GET', path).json() |
|
|
|
|
|
|
|
|
def domain_create(self, name): |
|
|
def domain_create(self, name): |
|
|
@ -64,7 +64,7 @@ class DnsimpleClient(object): |
|
|
|
|
|
|
|
|
page = 1 |
|
|
page = 1 |
|
|
while True: |
|
|
while True: |
|
|
data = self._request('GET', '/zones/{}/records'.format(zone_name), |
|
|
|
|
|
|
|
|
data = self._request('GET', f'/zones/{zone_name}/records', |
|
|
{'page': page}).json() |
|
|
{'page': page}).json() |
|
|
ret += data['data'] |
|
|
ret += data['data'] |
|
|
pagination = data['pagination'] |
|
|
pagination = data['pagination'] |
|
|
@ -75,11 +75,11 @@ class DnsimpleClient(object): |
|
|
return ret |
|
|
return ret |
|
|
|
|
|
|
|
|
def record_create(self, zone_name, params): |
|
|
def record_create(self, zone_name, params): |
|
|
path = '/zones/{}/records'.format(zone_name) |
|
|
|
|
|
|
|
|
path = f'/zones/{zone_name}/records' |
|
|
self._request('POST', path, data=params) |
|
|
self._request('POST', path, data=params) |
|
|
|
|
|
|
|
|
def record_delete(self, zone_name, record_id): |
|
|
def record_delete(self, zone_name, record_id): |
|
|
path = '/zones/{}/records/{}'.format(zone_name, record_id) |
|
|
|
|
|
|
|
|
path = f'/zones/{zone_name}/records/{record_id}' |
|
|
self._request('DELETE', path) |
|
|
self._request('DELETE', path) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -102,7 +102,7 @@ class DnsimpleProvider(BaseProvider): |
|
|
'PTR', 'SPF', 'SRV', 'SSHFP', 'TXT')) |
|
|
'PTR', 'SPF', 'SRV', 'SSHFP', 'TXT')) |
|
|
|
|
|
|
|
|
def __init__(self, id, token, account, sandbox=False, *args, **kwargs): |
|
|
def __init__(self, id, token, account, sandbox=False, *args, **kwargs): |
|
|
self.log = logging.getLogger('DnsimpleProvider[{}]'.format(id)) |
|
|
|
|
|
|
|
|
self.log = logging.getLogger(f'DnsimpleProvider[{id}]') |
|
|
self.log.debug('__init__: id=%s, token=***, account=%s', id, account) |
|
|
self.log.debug('__init__: id=%s, token=***, account=%s', id, account) |
|
|
super(DnsimpleProvider, self).__init__(id, *args, **kwargs) |
|
|
super(DnsimpleProvider, self).__init__(id, *args, **kwargs) |
|
|
self._client = DnsimpleClient(token, account, sandbox) |
|
|
self._client = DnsimpleClient(token, account, sandbox) |
|
|
@ -148,7 +148,7 @@ class DnsimpleProvider(BaseProvider): |
|
|
return { |
|
|
return { |
|
|
'ttl': record['ttl'], |
|
|
'ttl': record['ttl'], |
|
|
'type': _type, |
|
|
'type': _type, |
|
|
'value': '{}.'.format(record['content']) |
|
|
|
|
|
|
|
|
'value': f'{record["content"]}.' |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
_data_for_ALIAS = _data_for_CNAME |
|
|
_data_for_ALIAS = _data_for_CNAME |
|
|
@ -158,7 +158,7 @@ class DnsimpleProvider(BaseProvider): |
|
|
for record in records: |
|
|
for record in records: |
|
|
values.append({ |
|
|
values.append({ |
|
|
'preference': record['priority'], |
|
|
'preference': record['priority'], |
|
|
'exchange': '{}.'.format(record['content']) |
|
|
|
|
|
|
|
|
'exchange': f'{record["content"]}.' |
|
|
}) |
|
|
}) |
|
|
return { |
|
|
return { |
|
|
'ttl': records[0]['ttl'], |
|
|
'ttl': records[0]['ttl'], |
|
|
@ -197,7 +197,7 @@ class DnsimpleProvider(BaseProvider): |
|
|
for record in records: |
|
|
for record in records: |
|
|
content = record['content'] |
|
|
content = record['content'] |
|
|
if content[-1] != '.': |
|
|
if content[-1] != '.': |
|
|
content = '{}.'.format(content) |
|
|
|
|
|
|
|
|
content = f'{content}.' |
|
|
values.append(content) |
|
|
values.append(content) |
|
|
return { |
|
|
return { |
|
|
'ttl': records[0]['ttl'], |
|
|
'ttl': records[0]['ttl'], |
|
|
@ -230,7 +230,7 @@ class DnsimpleProvider(BaseProvider): |
|
|
) |
|
|
) |
|
|
continue |
|
|
continue |
|
|
|
|
|
|
|
|
target = '{}.'.format(target) if target != "." else "." |
|
|
|
|
|
|
|
|
target = f'{target}.' if target != "." else "." |
|
|
|
|
|
|
|
|
values.append({ |
|
|
values.append({ |
|
|
'port': port, |
|
|
'port': port, |
|
|
@ -296,7 +296,7 @@ class DnsimpleProvider(BaseProvider): |
|
|
before = len(zone.records) |
|
|
before = len(zone.records) |
|
|
for name, types in values.items(): |
|
|
for name, types in values.items(): |
|
|
for _type, records in types.items(): |
|
|
for _type, records in types.items(): |
|
|
data_for = getattr(self, '_data_for_{}'.format(_type)) |
|
|
|
|
|
|
|
|
data_for = getattr(self, f'_data_for_{_type}') |
|
|
record = Record.new(zone, name, data_for(_type, records), |
|
|
record = Record.new(zone, name, data_for(_type, records), |
|
|
source=self, lenient=lenient) |
|
|
source=self, lenient=lenient) |
|
|
zone.add_record(record, lenient=lenient) |
|
|
zone.add_record(record, lenient=lenient) |
|
|
@ -354,8 +354,7 @@ class DnsimpleProvider(BaseProvider): |
|
|
def _params_for_CAA(self, record): |
|
|
def _params_for_CAA(self, record): |
|
|
for value in record.values: |
|
|
for value in record.values: |
|
|
yield { |
|
|
yield { |
|
|
'content': '{} {} "{}"'.format(value.flags, value.tag, |
|
|
|
|
|
value.value), |
|
|
|
|
|
|
|
|
'content': f'{value.flags} {value.tag} "{value.value}"', |
|
|
'name': record.name, |
|
|
'name': record.name, |
|
|
'ttl': record.ttl, |
|
|
'ttl': record.ttl, |
|
|
'type': record._type |
|
|
'type': record._type |
|
|
@ -385,9 +384,8 @@ class DnsimpleProvider(BaseProvider): |
|
|
|
|
|
|
|
|
def _params_for_NAPTR(self, record): |
|
|
def _params_for_NAPTR(self, record): |
|
|
for value in record.values: |
|
|
for value in record.values: |
|
|
content = '{} {} "{}" "{}" "{}" {}' \ |
|
|
|
|
|
.format(value.order, value.preference, value.flags, |
|
|
|
|
|
value.service, value.regexp, value.replacement) |
|
|
|
|
|
|
|
|
content = f'{value.order} {value.preference} "{value.flags}" ' \ |
|
|
|
|
|
f'"{value.service}" "{value.preference}" {value.flags}' |
|
|
yield { |
|
|
yield { |
|
|
'content': content, |
|
|
'content': content, |
|
|
'name': record.name, |
|
|
'name': record.name, |
|
|
@ -398,8 +396,7 @@ class DnsimpleProvider(BaseProvider): |
|
|
def _params_for_SRV(self, record): |
|
|
def _params_for_SRV(self, record): |
|
|
for value in record.values: |
|
|
for value in record.values: |
|
|
yield { |
|
|
yield { |
|
|
'content': '{} {} {}'.format(value.weight, value.port, |
|
|
|
|
|
value.target), |
|
|
|
|
|
|
|
|
'content': f'{value.weight} {value.port} {value.target}', |
|
|
'name': record.name, |
|
|
'name': record.name, |
|
|
'priority': value.priority, |
|
|
'priority': value.priority, |
|
|
'ttl': record.ttl, |
|
|
'ttl': record.ttl, |
|
|
@ -409,9 +406,8 @@ class DnsimpleProvider(BaseProvider): |
|
|
def _params_for_SSHFP(self, record): |
|
|
def _params_for_SSHFP(self, record): |
|
|
for value in record.values: |
|
|
for value in record.values: |
|
|
yield { |
|
|
yield { |
|
|
'content': '{} {} {}'.format(value.algorithm, |
|
|
|
|
|
value.fingerprint_type, |
|
|
|
|
|
value.fingerprint), |
|
|
|
|
|
|
|
|
'content': f'{value.algorithm} {value.fingerprint_type} ' |
|
|
|
|
|
f'{value.fingerprint}', |
|
|
'name': record.name, |
|
|
'name': record.name, |
|
|
'ttl': record.ttl, |
|
|
'ttl': record.ttl, |
|
|
'type': record._type |
|
|
'type': record._type |
|
|
@ -419,7 +415,7 @@ class DnsimpleProvider(BaseProvider): |
|
|
|
|
|
|
|
|
def _apply_Create(self, change): |
|
|
def _apply_Create(self, change): |
|
|
new = change.new |
|
|
new = change.new |
|
|
params_for = getattr(self, '_params_for_{}'.format(new._type)) |
|
|
|
|
|
|
|
|
params_for = getattr(self, f'_params_for_{new._type}') |
|
|
for params in params_for(new): |
|
|
for params in params_for(new): |
|
|
self._client.record_create(new.zone.name[:-1], params) |
|
|
self._client.record_create(new.zone.name[:-1], params) |
|
|
|
|
|
|
|
|
@ -450,7 +446,7 @@ class DnsimpleProvider(BaseProvider): |
|
|
|
|
|
|
|
|
for change in changes: |
|
|
for change in changes: |
|
|
class_name = change.__class__.__name__ |
|
|
class_name = change.__class__.__name__ |
|
|
getattr(self, '_apply_{}'.format(class_name))(change) |
|
|
|
|
|
|
|
|
getattr(self, f'_apply_{class_name}')(change) |
|
|
|
|
|
|
|
|
# Clear out the cache if any |
|
|
# Clear out the cache if any |
|
|
self._zone_records.pop(desired.name, None) |
|
|
self._zone_records.pop(desired.name, None) |