mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-23 12:19:15 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -172,7 +172,7 @@ import re
|
|||
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
||||
|
||||
|
||||
class DNSimpleV2():
|
||||
class DNSimpleV2:
|
||||
"""class which uses dnsimple-python >= 2"""
|
||||
|
||||
def __init__(self, account_email, account_api_token, sandbox, module):
|
||||
|
|
@ -188,11 +188,18 @@ class DNSimpleV2():
|
|||
def dnsimple_client(self):
|
||||
"""creates a dnsimple client object"""
|
||||
if self.account_email and self.account_api_token:
|
||||
client = Client(sandbox=self.sandbox, email=self.account_email, access_token=self.account_api_token, user_agent="ansible/community.general")
|
||||
client = Client(
|
||||
sandbox=self.sandbox,
|
||||
email=self.account_email,
|
||||
access_token=self.account_api_token,
|
||||
user_agent="ansible/community.general",
|
||||
)
|
||||
else:
|
||||
msg = "Option account_email or account_api_token not provided. " \
|
||||
"Dnsimple authentication with a .dnsimple config file is not " \
|
||||
"supported with dnsimple-python>=2.0.0"
|
||||
msg = (
|
||||
"Option account_email or account_api_token not provided. "
|
||||
"Dnsimple authentication with a .dnsimple config file is not "
|
||||
"supported with dnsimple-python>=2.0.0"
|
||||
)
|
||||
raise DNSimpleException(msg)
|
||||
client.identity.whoami()
|
||||
self.client = client
|
||||
|
|
@ -205,9 +212,11 @@ class DNSimpleV2():
|
|||
if not account:
|
||||
accounts = Accounts(self.client).list_accounts().data
|
||||
if len(accounts) != 1:
|
||||
msg = "The provided dnsimple token is a user token with multiple accounts." \
|
||||
"Use an account token or a user token with access to a single account." \
|
||||
msg = (
|
||||
"The provided dnsimple token is a user token with multiple accounts."
|
||||
"Use an account token or a user token with access to a single account."
|
||||
"See https://support.dnsimple.com/articles/api-access-token/"
|
||||
)
|
||||
raise DNSimpleException(msg)
|
||||
account = accounts[0]
|
||||
self.account = account
|
||||
|
|
@ -239,9 +248,9 @@ class DNSimpleV2():
|
|||
|
||||
def get_records(self, zone, dnsimple_filter=None):
|
||||
"""return dns resource records which match a specified filter"""
|
||||
records_list = self._get_paginated_result(self.client.zones.list_records,
|
||||
account_id=self.account.id,
|
||||
zone=zone, filter=dnsimple_filter)
|
||||
records_list = self._get_paginated_result(
|
||||
self.client.zones.list_records, account_id=self.account.id, zone=zone, filter=dnsimple_filter
|
||||
)
|
||||
return [d.__dict__ for d in records_list]
|
||||
|
||||
def delete_record(self, domain, rid):
|
||||
|
|
@ -277,6 +286,7 @@ try:
|
|||
from dnsimple.service import Accounts
|
||||
from dnsimple.version import version as dnsimple_version
|
||||
from dnsimple.struct.zone_record import ZoneRecordUpdateInput, ZoneRecordInput
|
||||
|
||||
HAS_DNSIMPLE = True
|
||||
except ImportError:
|
||||
DNSIMPLE_IMP_ERR.append(traceback.format_exc())
|
||||
|
|
@ -287,52 +297,66 @@ from ansible.module_utils.basic import AnsibleModule, missing_required_lib, env_
|
|||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
account_email=dict(type='str', fallback=(env_fallback, ['DNSIMPLE_EMAIL'])),
|
||||
account_api_token=dict(type='str',
|
||||
no_log=True,
|
||||
fallback=(env_fallback, ['DNSIMPLE_API_TOKEN'])),
|
||||
domain=dict(type='str'),
|
||||
record=dict(type='str'),
|
||||
record_ids=dict(type='list', elements='str'),
|
||||
type=dict(type='str', choices=['A', 'ALIAS', 'CNAME', 'MX', 'SPF',
|
||||
'URL', 'TXT', 'NS', 'SRV', 'NAPTR',
|
||||
'PTR', 'AAAA', 'SSHFP', 'HINFO',
|
||||
'POOL', 'CAA']),
|
||||
ttl=dict(type='int', default=3600),
|
||||
value=dict(type='str'),
|
||||
priority=dict(type='int'),
|
||||
state=dict(type='str', choices=['present', 'absent'], default='present'),
|
||||
solo=dict(type='bool', default=False),
|
||||
sandbox=dict(type='bool', default=False),
|
||||
account_email=dict(type="str", fallback=(env_fallback, ["DNSIMPLE_EMAIL"])),
|
||||
account_api_token=dict(type="str", no_log=True, fallback=(env_fallback, ["DNSIMPLE_API_TOKEN"])),
|
||||
domain=dict(type="str"),
|
||||
record=dict(type="str"),
|
||||
record_ids=dict(type="list", elements="str"),
|
||||
type=dict(
|
||||
type="str",
|
||||
choices=[
|
||||
"A",
|
||||
"ALIAS",
|
||||
"CNAME",
|
||||
"MX",
|
||||
"SPF",
|
||||
"URL",
|
||||
"TXT",
|
||||
"NS",
|
||||
"SRV",
|
||||
"NAPTR",
|
||||
"PTR",
|
||||
"AAAA",
|
||||
"SSHFP",
|
||||
"HINFO",
|
||||
"POOL",
|
||||
"CAA",
|
||||
],
|
||||
),
|
||||
ttl=dict(type="int", default=3600),
|
||||
value=dict(type="str"),
|
||||
priority=dict(type="int"),
|
||||
state=dict(type="str", choices=["present", "absent"], default="present"),
|
||||
solo=dict(type="bool", default=False),
|
||||
sandbox=dict(type="bool", default=False),
|
||||
),
|
||||
required_together=[
|
||||
['record', 'value']
|
||||
],
|
||||
required_together=[["record", "value"]],
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
if not HAS_DNSIMPLE:
|
||||
module.fail_json(msg=missing_required_lib('dnsimple'), exception=DNSIMPLE_IMP_ERR[0])
|
||||
module.fail_json(msg=missing_required_lib("dnsimple"), exception=DNSIMPLE_IMP_ERR[0])
|
||||
|
||||
account_email = module.params.get('account_email')
|
||||
account_api_token = module.params.get('account_api_token')
|
||||
domain = module.params.get('domain')
|
||||
record = module.params.get('record')
|
||||
record_ids = module.params.get('record_ids')
|
||||
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')
|
||||
sandbox = module.params.get('sandbox')
|
||||
account_email = module.params.get("account_email")
|
||||
account_api_token = module.params.get("account_api_token")
|
||||
domain = module.params.get("domain")
|
||||
record = module.params.get("record")
|
||||
record_ids = module.params.get("record_ids")
|
||||
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")
|
||||
sandbox = module.params.get("sandbox")
|
||||
|
||||
DNSIMPLE_MAJOR_VERSION = LooseVersion(dnsimple_version).version[0]
|
||||
|
||||
try:
|
||||
if DNSIMPLE_MAJOR_VERSION < 2:
|
||||
module.fail_json(
|
||||
msg='Support for python-dnsimple < 2 has been removed in community.general 5.0.0. Update python-dnsimple to version >= 2.0.0.')
|
||||
msg="Support for python-dnsimple < 2 has been removed in community.general 5.0.0. Update python-dnsimple to version >= 2.0.0."
|
||||
)
|
||||
ds = DNSimpleV2(account_email, account_api_token, sandbox, module)
|
||||
# Let's figure out what operation we want to do
|
||||
# No domain, return a list
|
||||
|
|
@ -348,7 +372,7 @@ def main():
|
|||
typed_domain = str(domain)
|
||||
dr = ds.get_domain(typed_domain)
|
||||
# domain does not exist
|
||||
if state == 'present':
|
||||
if state == "present":
|
||||
if dr:
|
||||
module.exit_json(changed=False, result=dr)
|
||||
else:
|
||||
|
|
@ -373,15 +397,18 @@ def main():
|
|||
if not value:
|
||||
module.fail_json(msg="Missing the record value")
|
||||
|
||||
records_list = ds.get_records(domain, dnsimple_filter={'name': record})
|
||||
rr = next((r for r in records_list if r['name'] == record and r['type'] == record_type and r['content'] == value), None)
|
||||
if state == 'present':
|
||||
records_list = ds.get_records(domain, dnsimple_filter={"name": record})
|
||||
rr = next(
|
||||
(r for r in records_list 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_list if r['name'] == record and r['type'] == record_type]
|
||||
same_type = [r["id"] for r in records_list if r["name"] == record and r["type"] == record_type]
|
||||
if rr:
|
||||
same_type = [rid for rid in same_type if rid != rr['id']]
|
||||
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:
|
||||
|
|
@ -389,11 +416,11 @@ def main():
|
|||
changed = True
|
||||
if rr:
|
||||
# check if we need to update
|
||||
if rr['ttl'] != ttl or rr['priority'] != priority:
|
||||
if rr["ttl"] != ttl or rr["priority"] != priority:
|
||||
if module.check_mode:
|
||||
module.exit_json(changed=True)
|
||||
else:
|
||||
response = ds.update_record(domain, rr['id'], ttl, priority)
|
||||
response = ds.update_record(domain, rr["id"], ttl, priority)
|
||||
module.exit_json(changed=True, result=response)
|
||||
else:
|
||||
module.exit_json(changed=changed, result=rr)
|
||||
|
|
@ -408,7 +435,7 @@ def main():
|
|||
else:
|
||||
if rr:
|
||||
if not module.check_mode:
|
||||
ds.delete_record(domain, rr['id'])
|
||||
ds.delete_record(domain, rr["id"])
|
||||
module.exit_json(changed=True)
|
||||
else:
|
||||
module.exit_json(changed=False)
|
||||
|
|
@ -416,9 +443,9 @@ def main():
|
|||
# Make sure these record_ids either all exist or none
|
||||
if record_ids:
|
||||
current_records = ds.get_records(domain, dnsimple_filter=None)
|
||||
current_record_ids = [str(d['id']) for d in current_records]
|
||||
current_record_ids = [str(d["id"]) for d in current_records]
|
||||
wanted_record_ids = [str(r) for r in record_ids]
|
||||
if state == 'present':
|
||||
if state == "present":
|
||||
difference = list(set(wanted_record_ids) - set(current_record_ids))
|
||||
if difference:
|
||||
module.fail_json(msg=f"Missing the following records: {difference}")
|
||||
|
|
@ -443,5 +470,5 @@ def main():
|
|||
module.fail_json(msg="Unknown what you wanted me to do")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue