1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Bug fixes for GCP Dns Managed Zones (#38631)

This commit is contained in:
Alex Stephen 2018-04-25 13:20:55 -07:00 committed by Ryan Brown
parent f8853d83e3
commit dd735b2122
2 changed files with 98 additions and 25 deletions

View file

@ -62,7 +62,7 @@ options:
name: name:
description: description:
- User assigned name for this resource. - User assigned name for this resource.
Must be unique within the project. - Must be unique within the project.
required: true required: true
name_server_set: name_server_set:
description: description:
@ -74,7 +74,7 @@ extends_documentation_fragment: gcp
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: Create a Managed Zone - name: create a managed zone
gcp_dns_managed_zone: gcp_dns_managed_zone:
name: testObject name: testObject
dns_name: test.somewild2.example.com. dns_name: test.somewild2.example.com.
@ -84,25 +84,50 @@ EXAMPLES = '''
service_account_file: /tmp/auth.pem service_account_file: /tmp/auth.pem
scopes: scopes:
- https://www.googleapis.com/auth/ndev.clouddns.readwrite - https://www.googleapis.com/auth/ndev.clouddns.readwrite
state: 'present' state: present
''' '''
RETURN = ''' RETURN = '''
description:
description:
- A mutable string of at most 1024 characters associated with this
resource for the user's convenience. Has no effect on the managed
zone's function.
returned: success
type: str
dns_name:
description:
- The DNS name of this managed zone, for instance "example.com.".
returned: success
type: str
id: id:
description: description:
- Unique identifier for the resource; defined by the server. - Unique identifier for the resource; defined by the server.
returned: success returned: success
type: int type: int
name:
description:
- User assigned name for this resource.
- Must be unique within the project.
returned: success
type: str
name_servers: name_servers:
description: description:
- Delegate your managed_zone to these virtual name servers; - Delegate your managed_zone to these virtual name servers; defined
defined by the server by the server.
returned: success
type: list
name_server_set:
description:
- Optionally specifies the NameServerSet for this ManagedZone. A
NameServerSet is a set of DNS name servers that all host the same
ManagedZones. Most users will leave this field unset.
returned: success returned: success
type: list type: list
creation_time: creation_time:
description: description:
- The time that this resource was created on the server. - The time that this resource was created on the server.
This is in RFC3339 text format. - This is in RFC3339 text format.
returned: success returned: success
type: str type: str
''' '''
@ -111,7 +136,7 @@ RETURN = '''
# Imports # Imports
################################################################################ ################################################################################
from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequestException from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest, replace_resource_dict
import json import json
################################################################################ ################################################################################
@ -128,7 +153,7 @@ def main():
description=dict(type='str'), description=dict(type='str'),
dns_name=dict(type='str'), dns_name=dict(type='str'),
name=dict(required=True, type='str'), name=dict(required=True, type='str'),
name_server_set=dict(type='list'), name_server_set=dict(type='list', elements='str')
) )
) )
@ -142,6 +167,7 @@ def main():
if state == 'present': if state == 'present':
if is_different(module, fetch): if is_different(module, fetch):
fetch = update(module, self_link(module), kind) fetch = update(module, self_link(module), kind)
changed = True
else: else:
delete(module, self_link(module), kind) delete(module, self_link(module), kind)
fetch = {} fetch = {}
@ -150,17 +176,16 @@ def main():
if state == 'present': if state == 'present':
fetch = create(module, collection(module), kind) fetch = create(module, collection(module), kind)
changed = True changed = True
if fetch:
fetch.update({'changed': changed})
else: else:
fetch = {'changed': changed} fetch = {}
fetch.update({'changed': changed})
module.exit_json(**fetch) module.exit_json(**fetch)
def create(module, link, kind): def create(module, link, kind):
auth = GcpSession(module, 'g') auth = GcpSession(module, 'dns')
return return_if_object(module, auth.post(link, resource_to_request(module)), kind) return return_if_object(module, auth.post(link, resource_to_request(module)), kind)
@ -169,17 +194,17 @@ def update(module, link, kind):
def delete(module, link, kind): def delete(module, link, kind):
auth = GcpSession(module, 'g') auth = GcpSession(module, 'dns')
return return_if_object(module, auth.delete(link), kind) return return_if_object(module, auth.delete(link), kind)
def resource_to_request(module): def resource_to_request(module):
request = { request = {
u'kind': 'dns#managedZone', u'kind': 'dns#managedZone',
u'description': module.params['description'], u'description': module.params.get('description'),
u'dnsName': module.params['dns_name'], u'dnsName': module.params.get('dns_name'),
u'name': module.params['name'], u'name': module.params.get('name'),
u'nameServerSet': module.params['name_server_set'], u'nameServerSet': module.params.get('name_server_set')
} }
return_vals = {} return_vals = {}
for k, v in request.items(): for k, v in request.items():
@ -190,7 +215,7 @@ def resource_to_request(module):
def fetch_resource(module, link, kind): def fetch_resource(module, link, kind):
auth = GcpSession(module, 'g') auth = GcpSession(module, 'dns')
return return_if_object(module, auth.get(link), kind) return return_if_object(module, auth.get(link), kind)
@ -212,12 +237,10 @@ def return_if_object(module, response, kind):
return None return None
try: try:
response.raise_for_status module.raise_for_status(response)
result = response.json() result = response.json()
except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst: except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst:
module.fail_json(msg="Invalid JSON response with error: %s" % inst) module.fail_json(msg="Invalid JSON response with error: %s" % inst)
except GcpRequestException as inst:
module.fail_json(msg="Network error: %s" % inst)
if navigate_hash(result, ['error', 'errors']): if navigate_hash(result, ['error', 'errors']):
module.fail_json(msg=navigate_hash(result, ['error', 'errors'])) module.fail_json(msg=navigate_hash(result, ['error', 'errors']))
@ -229,14 +252,34 @@ def return_if_object(module, response, kind):
def is_different(module, response): def is_different(module, response):
request = resource_to_request(module) request = resource_to_request(module)
response = response_to_hash(module, response)
# Remove all output-only from response. # Remove all output-only from response.
return_vals = {} response_vals = {}
for k, v in response.items(): for k, v in response.items():
if k in request: if k in request:
return_vals[k] = v response_vals[k] = v
return request != return_vals request_vals = {}
for k, v in request.items():
if k in response:
request_vals[k] = v
return GcpRequest(request_vals) != GcpRequest(response_vals)
# Remove unnecessary properties from the response.
# This is for doing comparisons with Ansible's current parameters.
def response_to_hash(module, response):
return {
u'description': response.get(u'description'),
u'dnsName': response.get(u'dnsName'),
u'id': response.get(u'id'),
u'name': response.get(u'name'),
u'nameServers': response.get(u'nameServers'),
u'nameServerSet': response.get(u'nameServerSet'),
u'creationTime': response.get(u'creationTime')
}
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -12,6 +12,18 @@
# https://www.github.com/GoogleCloudPlatform/magic-modules # https://www.github.com/GoogleCloudPlatform/magic-modules
# #
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Pre-test setup
- name: delete a managed zone
gcp_dns_managed_zone:
name: "{{ resource_name }}"
dns_name: test.somewild2.example.com.
description: 'test zone'
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/ndev.clouddns.readwrite
state: absent
#---------------------------------------------------------- #----------------------------------------------------------
- name: create a managed zone - name: create a managed zone
gcp_dns_managed_zone: gcp_dns_managed_zone:
@ -30,6 +42,14 @@
that: that:
- result.changed == true - result.changed == true
- "result.kind == 'dns#managedZone'" - "result.kind == 'dns#managedZone'"
- name: verify that managed_zone was created
shell: |
gcloud dns managed-zones describe --project="{{ gcp_project }}" "{{ resource_name }}"
register: results
- name: verify that command succeeded
assert:
that:
- results.rc == 0
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
- name: create a managed zone that already exists - name: create a managed zone that already exists
gcp_dns_managed_zone: gcp_dns_managed_zone:
@ -66,6 +86,16 @@
that: that:
- result.changed == true - result.changed == true
- result.has_key('kind') == False - result.has_key('kind') == False
- name: verify that managed_zone was deleted
shell: |
gcloud dns managed-zones describe --project="{{ gcp_project }}" "{{ resource_name }}"
register: results
failed_when: results.rc == 0
- name: verify that command succeeded
assert:
that:
- results.rc == 1
- "\"{{ resource_name }} was not found.\" in results.stderr"
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
- name: delete a managed zone that does not exist - name: delete a managed zone that does not exist
gcp_dns_managed_zone: gcp_dns_managed_zone: