mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
feat(gandi_livedns): support personal access tokens (#8337)
* fix(gandi_livedns): fix unsafe conditionals in tests * feat(gandi_livedns): support personal access tokens Fixes #7639
This commit is contained in:
parent
bb73f28bf5
commit
a409f8fc2f
5 changed files with 69 additions and 27 deletions
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- gandi_livedns - adds support for personal access tokens (https://github.com/ansible-collections/community.general/issues/7639, https://github.com/ansible-collections/community.general/pull/8337).
|
|
@ -33,6 +33,7 @@ class GandiLiveDNSAPI(object):
|
|||
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 = ''
|
||||
|
@ -50,7 +51,12 @@ class GandiLiveDNSAPI(object):
|
|||
return s
|
||||
|
||||
def _gandi_api_call(self, api_call, method='GET', payload=None, error_on_404=True):
|
||||
headers = {'Authorization': 'Apikey {0}'.format(self.api_key),
|
||||
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:
|
||||
|
|
|
@ -25,11 +25,19 @@ attributes:
|
|||
diff_mode:
|
||||
support: none
|
||||
options:
|
||||
personal_access_token:
|
||||
description:
|
||||
- Scoped API token.
|
||||
- One of O(personal_access_token) and O(api_key) must be specified.
|
||||
type: str
|
||||
version_added: 9.0.0
|
||||
api_key:
|
||||
description:
|
||||
- Account API token.
|
||||
- Note that these type of keys are deprecated and might stop working at some point.
|
||||
Use personal access tokens instead.
|
||||
- One of O(personal_access_token) and O(api_key) must be specified.
|
||||
type: str
|
||||
required: true
|
||||
record:
|
||||
description:
|
||||
- Record to add.
|
||||
|
@ -73,7 +81,7 @@ EXAMPLES = r'''
|
|||
values:
|
||||
- 127.0.0.1
|
||||
ttl: 7200
|
||||
api_key: dummyapitoken
|
||||
personal_access_token: dummytoken
|
||||
register: record
|
||||
|
||||
- name: Create a mail CNAME record to www.my.com domain
|
||||
|
@ -84,7 +92,7 @@ EXAMPLES = r'''
|
|||
values:
|
||||
- www
|
||||
ttl: 7200
|
||||
api_key: dummyapitoken
|
||||
personal_access_token: dummytoken
|
||||
state: present
|
||||
|
||||
- name: Change its TTL
|
||||
|
@ -95,7 +103,7 @@ EXAMPLES = r'''
|
|||
values:
|
||||
- www
|
||||
ttl: 10800
|
||||
api_key: dummyapitoken
|
||||
personal_access_token: dummytoken
|
||||
state: present
|
||||
|
||||
- name: Delete the record
|
||||
|
@ -103,8 +111,18 @@ EXAMPLES = r'''
|
|||
domain: my.com
|
||||
type: CNAME
|
||||
record: mail
|
||||
api_key: dummyapitoken
|
||||
personal_access_token: dummytoken
|
||||
state: absent
|
||||
|
||||
- name: Use a (deprecated) API Key
|
||||
community.general.gandi_livedns:
|
||||
domain: my.com
|
||||
record: test
|
||||
type: A
|
||||
values:
|
||||
- 127.0.0.1
|
||||
ttl: 7200
|
||||
api_key: dummyapikey
|
||||
'''
|
||||
|
||||
RETURN = r'''
|
||||
|
@ -151,7 +169,8 @@ from ansible_collections.community.general.plugins.module_utils.gandi_livedns_ap
|
|||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
api_key=dict(type='str', required=True, no_log=True),
|
||||
api_key=dict(type='str', no_log=True),
|
||||
personal_access_token=dict(type='str', no_log=True),
|
||||
record=dict(type='str', required=True),
|
||||
state=dict(type='str', default='present', choices=['absent', 'present']),
|
||||
ttl=dict(type='int'),
|
||||
|
@ -163,6 +182,12 @@ def main():
|
|||
required_if=[
|
||||
('state', 'present', ['values', 'ttl']),
|
||||
],
|
||||
mutually_exclusive=[
|
||||
('api_key', 'personal_access_token'),
|
||||
],
|
||||
required_one_of=[
|
||||
('api_key', 'personal_access_token'),
|
||||
],
|
||||
)
|
||||
|
||||
gandi_api = GandiLiveDNSAPI(module)
|
||||
|
|
|
@ -45,10 +45,10 @@
|
|||
assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.record['values'] == {{ item['values'] }}
|
||||
- result.record.record == "{{ item.record }}"
|
||||
- result.record.type == "{{ item.type }}"
|
||||
- result.record.ttl == {{ item.ttl }}
|
||||
- result.record['values'] == item['values']
|
||||
- result.record.record == item.record
|
||||
- result.record.type == item.type
|
||||
- result.record.ttl == item.ttl
|
||||
|
||||
- name: test create a dns record idempotence
|
||||
community.general.gandi_livedns:
|
||||
|
@ -63,7 +63,16 @@
|
|||
assert:
|
||||
that:
|
||||
- result is not changed
|
||||
- result.record['values'] == {{ item['values'] }}
|
||||
- result.record.record == "{{ item.record }}"
|
||||
- result.record.type == "{{ item.type }}"
|
||||
- result.record.ttl == {{ item.ttl }}
|
||||
- result.record['values'] == item['values']
|
||||
- result.record.record == item.record
|
||||
- result.record.type == item.type
|
||||
- result.record.ttl == item.ttl
|
||||
|
||||
- name: test create a DNS record with personal access token
|
||||
community.general.gandi_livedns:
|
||||
personal_access_token: "{{ gandi_personal_access_token }}"
|
||||
record: "{{ item.record }}"
|
||||
domain: "{{ gandi_livedns_domain_name }}"
|
||||
values: "{{ item['values'] }}"
|
||||
ttl: "{{ item.ttl }}"
|
||||
type: "{{ item.type }}"
|
||||
|
|
|
@ -17,10 +17,10 @@
|
|||
assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.record['values'] == {{ item.update_values | default(item['values']) }}
|
||||
- result.record.record == "{{ item.record }}"
|
||||
- result.record.type == "{{ item.type }}"
|
||||
- result.record.ttl == {{ item.update_ttl | default(item.ttl) }}
|
||||
- result.record['values'] == (item.update_values | default(item['values']))
|
||||
- result.record.record == item.record
|
||||
- result.record.type == item.type
|
||||
- result.record.ttl == (item.update_ttl | default(item.ttl))
|
||||
|
||||
- name: test update or add another dns record
|
||||
community.general.gandi_livedns:
|
||||
|
@ -35,10 +35,10 @@
|
|||
assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.record['values'] == {{ item.update_values | default(item['values']) }}
|
||||
- result.record.record == "{{ item.record }}"
|
||||
- result.record.ttl == {{ item.update_ttl | default(item.ttl) }}
|
||||
- result.record.type == "{{ item.type }}"
|
||||
- result.record['values'] == (item.update_values | default(item['values']))
|
||||
- result.record.record == item.record
|
||||
- result.record.ttl == (item.update_ttl | default(item.ttl))
|
||||
- result.record.type == item.type
|
||||
|
||||
- name: test update or add another dns record idempotence
|
||||
community.general.gandi_livedns:
|
||||
|
@ -53,7 +53,7 @@
|
|||
assert:
|
||||
that:
|
||||
- result is not changed
|
||||
- result.record['values'] == {{ item.update_values | default(item['values']) }}
|
||||
- result.record.record == "{{ item.record }}"
|
||||
- result.record.ttl == {{ item.update_ttl | default(item.ttl) }}
|
||||
- result.record.type == "{{ item.type }}"
|
||||
- result.record['values'] == (item.update_values | default(item['values']))
|
||||
- result.record.record == item.record
|
||||
- result.record.ttl == (item.update_ttl | default(item.ttl))
|
||||
- result.record.type == item.type
|
||||
|
|
Loading…
Reference in a new issue