219 lines
8.3 KiB
Python
219 lines
8.3 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
# MIT License (see LICENSE)
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
DOCUMENTATION = r'''
|
|
module: domain
|
|
short_description: Register a domain at Interweb Angola or manage one you have
|
|
version_added: 1.0.0
|
|
description:
|
|
- If the domain is already in the account, manages its nameservers, transfer lock and renewal. Nothing is bought.
|
|
- If it is not, registers it with the account credit (only when O(register=true), the default).
|
|
- Domains are not deleted through the API, so there is no V(absent) state.
|
|
options:
|
|
domain:
|
|
description: Domain name, such as C(example.co.ao).
|
|
type: str
|
|
required: true
|
|
register:
|
|
description: Register the domain when it is not in the account. With V(false) the task fails instead of buying.
|
|
type: bool
|
|
default: true
|
|
years:
|
|
description: Years to register, from 1 to 10. Only used when registering.
|
|
type: int
|
|
default: 1
|
|
promo_code:
|
|
description: Promo code, only used when registering.
|
|
type: str
|
|
idempotency_key:
|
|
description:
|
|
- Value for the C(Idempotency-Key) of the registration. Default, a new random value.
|
|
- A repeated task with the same key and the same options within 24 hours gets the same answer and is not charged again.
|
|
type: str
|
|
nameservers:
|
|
description: From 2 to 5 nameservers. When not set, they are left as they are.
|
|
type: list
|
|
elements: str
|
|
locked:
|
|
description: Transfer lock. When not set, it is left as it is.
|
|
type: bool
|
|
auto_renew:
|
|
description: Whether renewal invoices are issued. When not set, it is left as it is.
|
|
type: bool
|
|
wait:
|
|
description: After registering, wait until the domain shows in the account, then apply the settings.
|
|
type: bool
|
|
default: true
|
|
wait_timeout:
|
|
description: Seconds to wait.
|
|
type: int
|
|
default: 900
|
|
extends_documentation_fragment:
|
|
- interweb_ao.cloud.auth
|
|
author:
|
|
- Interweb Angola (@interweb-ao)
|
|
'''
|
|
|
|
EXAMPLES = r'''
|
|
- name: Register the domain if needed, with our nameservers and the lock on
|
|
interweb_ao.cloud.domain:
|
|
domain: example.co.ao
|
|
years: 1
|
|
locked: true
|
|
auto_renew: true
|
|
|
|
- name: Point a domain we already have to other nameservers, never buying
|
|
interweb_ao.cloud.domain:
|
|
domain: example.co.ao
|
|
register: false
|
|
nameservers:
|
|
- ns1.example.co.ao
|
|
- ns2.example.co.ao
|
|
'''
|
|
|
|
RETURN = r'''
|
|
domain:
|
|
description: The domain, as GET /v1/domains/{domain} answers, with C(nameservers) and C(locked).
|
|
returned: always
|
|
type: dict
|
|
registration:
|
|
description: The answer of the registration (amount, currency, action_id).
|
|
returned: when the domain was registered
|
|
type: dict
|
|
availability:
|
|
description: In check mode, when the domain is not in the account, whether it can be registered and its price.
|
|
returned: in check mode, for a domain not in the account
|
|
type: dict
|
|
simulated:
|
|
description: True when the API only simulated the writes (sandbox_ key).
|
|
returned: always
|
|
type: bool
|
|
actions:
|
|
description: What was done or, in check mode, what would be done.
|
|
returned: always
|
|
type: list
|
|
elements: str
|
|
'''
|
|
|
|
import time
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
from ..module_utils.api import (
|
|
ApiError, auth_argument_spec, fail, get_client, new_idempotency_key, same_names, seg,
|
|
)
|
|
|
|
|
|
def read_domain(client, name):
|
|
try:
|
|
return client.get('/v1/domains/' + seg(name))
|
|
except ApiError as e:
|
|
if e.status == 404:
|
|
return None
|
|
raise
|
|
|
|
|
|
def main():
|
|
spec = auth_argument_spec()
|
|
spec.update(
|
|
domain=dict(type='str', required=True),
|
|
register=dict(type='bool', default=True),
|
|
years=dict(type='int', default=1),
|
|
promo_code=dict(type='str'),
|
|
idempotency_key=dict(type='str', no_log=False),
|
|
nameservers=dict(type='list', elements='str'),
|
|
locked=dict(type='bool'),
|
|
auto_renew=dict(type='bool'),
|
|
wait=dict(type='bool', default=True),
|
|
wait_timeout=dict(type='int', default=900),
|
|
)
|
|
module = AnsibleModule(argument_spec=spec, supports_check_mode=True)
|
|
p = module.params
|
|
name = p['domain'].strip().lower().rstrip('.')
|
|
if p['nameservers'] is not None and not 2 <= len(p['nameservers']) <= 5:
|
|
module.fail_json(msg='Give from 2 to 5 nameservers.')
|
|
if not 1 <= p['years'] <= 10:
|
|
module.fail_json(msg='years must be from 1 to 10.')
|
|
client = get_client(module, ['domains:read'] if module.check_mode else ['domains:read', 'domains:write'])
|
|
result = dict(changed=False, domain={}, simulated=False, actions=[])
|
|
|
|
try:
|
|
d = read_domain(client, name)
|
|
if d is None:
|
|
if not p['register']:
|
|
module.fail_json(msg='%s is not in the account and register is false.' % name)
|
|
result['changed'] = True
|
|
result['actions'].append('register %s for %d year(s)' % (name, p['years']))
|
|
if module.check_mode:
|
|
result['availability'] = client.get('/v1/domains/availability', query=dict(domain=name))
|
|
module.exit_json(**result)
|
|
body = dict(domain=name, years=p['years'])
|
|
if p['nameservers']:
|
|
body['nameservers'] = p['nameservers']
|
|
if p['locked'] is not None:
|
|
body['locked'] = p['locked']
|
|
if p['promo_code']:
|
|
body['promo_code'] = p['promo_code']
|
|
r = client.request('POST', '/v1/domains/register', body=body, idempotency_key=p['idempotency_key'] or new_idempotency_key())
|
|
result['registration'] = r.data
|
|
if r.simulated:
|
|
result['simulated'] = True
|
|
result['warnings'] = ['The API key is a sandbox_ key: the registration was simulated and the domain was not registered.']
|
|
module.exit_json(**result)
|
|
if not p['wait']:
|
|
module.exit_json(**result)
|
|
deadline = time.time() + p['wait_timeout']
|
|
while d is None and time.time() < deadline:
|
|
time.sleep(15)
|
|
d = read_domain(client, name)
|
|
if d is None:
|
|
result['warnings'] = ['%s is not in the account yet; run the task again later to apply the other settings.' % name]
|
|
module.exit_json(**result)
|
|
|
|
base = '/v1/domains/' + seg(name)
|
|
if p['nameservers'] is not None:
|
|
have = client.get(base + '/nameservers').get('nameservers') or []
|
|
if not same_names(have, p['nameservers']):
|
|
result['actions'].append('nameservers %s' % ', '.join(p['nameservers']))
|
|
result.setdefault('diff', dict(before={}, after={}))
|
|
result['diff']['before']['nameservers'] = have
|
|
result['diff']['after']['nameservers'] = p['nameservers']
|
|
if not module.check_mode:
|
|
r = client.request('PUT', base + '/nameservers', body=dict(nameservers=p['nameservers']))
|
|
result['simulated'] = result['simulated'] or r.simulated
|
|
if p['locked'] is not None:
|
|
have = client.get(base + '/lock').get('locked')
|
|
if have != p['locked']:
|
|
result['actions'].append('transfer lock %s' % ('on' if p['locked'] else 'off'))
|
|
if not module.check_mode:
|
|
r = client.request('PUT', base + '/lock', body=dict(locked=p['locked']))
|
|
result['simulated'] = result['simulated'] or r.simulated
|
|
if p['auto_renew'] is not None and d.get('auto_renew') != p['auto_renew']:
|
|
result['actions'].append('auto renew %s' % ('on' if p['auto_renew'] else 'off'))
|
|
if not module.check_mode:
|
|
r = client.request('PUT', base + '/auto-renew', body=dict(auto_renew=p['auto_renew']))
|
|
result['simulated'] = result['simulated'] or r.simulated
|
|
|
|
result['changed'] = bool(result['actions'])
|
|
if result['changed'] and not module.check_mode:
|
|
d = read_domain(client, name) or d
|
|
try:
|
|
d['nameservers'] = client.get(base + '/nameservers').get('nameservers')
|
|
except ApiError:
|
|
pass
|
|
try:
|
|
d['locked'] = client.get(base + '/lock').get('locked')
|
|
except ApiError:
|
|
pass
|
|
result['domain'] = d
|
|
module.exit_json(**result)
|
|
except ApiError as e:
|
|
fail(module, e, **result)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|