From e598eeeebee5dab45d3f9b292a7593cda8232bdf Mon Sep 17 00:00:00 2001 From: The Magician Date: Mon, 20 May 2019 11:43:38 -0700 Subject: [PATCH] Bug fixes for GCP modules (#55977) --- .../google/gcp_sourcerepo_repository_facts.py | 6 ++--- .../google/gcp_spanner_database_facts.py | 6 ++--- .../google/gcp_spanner_instance_facts.py | 6 ++--- .../modules/cloud/google/gcp_sql_database.py | 22 +++++++------------ .../cloud/google/gcp_sql_database_facts.py | 20 ++++++----------- .../cloud/google/gcp_sql_instance_facts.py | 6 ++--- .../cloud/google/gcp_sql_user_facts.py | 6 ++--- .../targets/gcp_sql_instance/tasks/main.yml | 4 ++-- 8 files changed, 32 insertions(+), 44 deletions(-) diff --git a/lib/ansible/modules/cloud/google/gcp_sourcerepo_repository_facts.py b/lib/ansible/modules/cloud/google/gcp_sourcerepo_repository_facts.py index e6c3a9a00d0..1104fc9dbd9 100644 --- a/lib/ansible/modules/cloud/google/gcp_sourcerepo_repository_facts.py +++ b/lib/ansible/modules/cloud/google/gcp_sourcerepo_repository_facts.py @@ -53,8 +53,8 @@ EXAMPLES = ''' ''' RETURN = ''' -items: - description: List of items +resources: + description: List of resources returned: always type: complex contains: @@ -99,7 +99,7 @@ def main(): items = items.get('repos') else: items = [] - return_value = {'items': items} + return_value = {'resources': items} module.exit_json(**return_value) diff --git a/lib/ansible/modules/cloud/google/gcp_spanner_database_facts.py b/lib/ansible/modules/cloud/google/gcp_spanner_database_facts.py index e5d02ce5acc..d3e0c5795d1 100644 --- a/lib/ansible/modules/cloud/google/gcp_spanner_database_facts.py +++ b/lib/ansible/modules/cloud/google/gcp_spanner_database_facts.py @@ -63,8 +63,8 @@ EXAMPLES = ''' ''' RETURN = ''' -items: - description: List of items +resources: + description: List of resources returned: always type: complex contains: @@ -111,7 +111,7 @@ def main(): items = items.get('databases') else: items = [] - return_value = {'items': items} + return_value = {'resources': items} module.exit_json(**return_value) diff --git a/lib/ansible/modules/cloud/google/gcp_spanner_instance_facts.py b/lib/ansible/modules/cloud/google/gcp_spanner_instance_facts.py index c1c5d40c764..7c64a9e5b52 100644 --- a/lib/ansible/modules/cloud/google/gcp_spanner_instance_facts.py +++ b/lib/ansible/modules/cloud/google/gcp_spanner_instance_facts.py @@ -53,8 +53,8 @@ EXAMPLES = ''' ''' RETURN = ''' -items: - description: List of items +resources: + description: List of resources returned: always type: complex contains: @@ -115,7 +115,7 @@ def main(): items = items.get('instances') else: items = [] - return_value = {'items': items} + return_value = {'resources': items} module.exit_json(**return_value) diff --git a/lib/ansible/modules/cloud/google/gcp_sql_database.py b/lib/ansible/modules/cloud/google/gcp_sql_database.py index c13402a1ed8..608cb2a6eb5 100644 --- a/lib/ansible/modules/cloud/google/gcp_sql_database.py +++ b/lib/ansible/modules/cloud/google/gcp_sql_database.py @@ -59,15 +59,10 @@ options: description: - The name of the database in the Cloud SQL instance. - This does not include the project ID or instance name. - required: false + required: true instance: description: - The name of the Cloud SQL instance. This does not include the project ID. - - 'This field represents a link to a Instance resource in GCP. It can be specified - in two ways. First, you can place a dictionary with key ''name'' and value of - your resource''s name Alternatively, you can add `register: name-of-resource` - to a gcp_sql_instance task and then set this instance field to "{{ name-of-resource - }}"' required: true extends_documentation_fragment: gcp ''' @@ -121,7 +116,7 @@ instance: description: - The name of the Cloud SQL instance. This does not include the project ID. returned: success - type: dict + type: str ''' ################################################################################ @@ -145,8 +140,8 @@ def main(): state=dict(default='present', choices=['present', 'absent'], type='str'), charset=dict(type='str'), collation=dict(type='str'), - name=dict(type='str'), - instance=dict(required=True, type='dict'), + name=dict(required=True, type='str'), + instance=dict(required=True, type='str'), ) ) @@ -199,6 +194,7 @@ def delete(module, link, kind): def resource_to_request(module): request = { u'kind': 'sql#database', + u'instance': module.params.get('instance'), u'charset': module.params.get('charset'), u'collation': module.params.get('collation'), u'name': module.params.get('name'), @@ -217,13 +213,11 @@ def fetch_resource(module, link, kind, allow_not_found=True): def self_link(module): - res = {'project': module.params['project'], 'instance': replace_resource_dict(module.params['instance'], 'name'), 'name': module.params['name']} - return "https://www.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance}/databases/{name}".format(**res) + return "https://www.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance}/databases/{name}".format(**module.params) def collection(module): - res = {'project': module.params['project'], 'instance': replace_resource_dict(module.params['instance'], 'name')} - return "https://www.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance}/databases".format(**res) + return "https://www.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance}/databases".format(**module.params) def return_if_object(module, response, kind, allow_not_found=False): @@ -271,7 +265,7 @@ def is_different(module, response): # Remove unnecessary properties from the response. # This is for doing comparisons with Ansible's current parameters. def response_to_hash(module, response): - return {u'charset': response.get(u'charset'), u'collation': response.get(u'collation'), u'name': response.get(u'name')} + return {u'charset': response.get(u'charset'), u'collation': response.get(u'collation'), u'name': module.params.get('name')} def async_op_url(module, extra_data=None): diff --git a/lib/ansible/modules/cloud/google/gcp_sql_database_facts.py b/lib/ansible/modules/cloud/google/gcp_sql_database_facts.py index 215fef21881..41d96b1e091 100644 --- a/lib/ansible/modules/cloud/google/gcp_sql_database_facts.py +++ b/lib/ansible/modules/cloud/google/gcp_sql_database_facts.py @@ -43,11 +43,6 @@ options: instance: description: - The name of the Cloud SQL instance. This does not include the project ID. - - 'This field represents a link to a Instance resource in GCP. It can be specified - in two ways. First, you can place a dictionary with key ''name'' and value of - your resource''s name Alternatively, you can add `register: name-of-resource` - to a gcp_sql_instance task and then set this instance field to "{{ name-of-resource - }}"' required: true extends_documentation_fragment: gcp ''' @@ -63,8 +58,8 @@ EXAMPLES = ''' ''' RETURN = ''' -items: - description: List of items +resources: + description: List of resources returned: always type: complex contains: @@ -88,13 +83,13 @@ items: description: - The name of the Cloud SQL instance. This does not include the project ID. returned: success - type: dict + type: str ''' ################################################################################ # Imports ################################################################################ -from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest, replace_resource_dict +from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest import json ################################################################################ @@ -103,7 +98,7 @@ import json def main(): - module = GcpModule(argument_spec=dict(instance=dict(required=True, type='dict'))) + module = GcpModule(argument_spec=dict(instance=dict(required=True, type='str'))) if not module.params['scopes']: module.params['scopes'] = ['https://www.googleapis.com/auth/sqlservice.admin'] @@ -113,13 +108,12 @@ def main(): items = items.get('items') else: items = [] - return_value = {'items': items} + return_value = {'resources': items} module.exit_json(**return_value) def collection(module): - res = {'project': module.params['project'], 'instance': replace_resource_dict(module.params['instance'], 'name')} - return "https://www.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance}/databases".format(**res) + return "https://www.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance}/databases".format(**module.params) def fetch_list(module, link): diff --git a/lib/ansible/modules/cloud/google/gcp_sql_instance_facts.py b/lib/ansible/modules/cloud/google/gcp_sql_instance_facts.py index 9f1b9ce362c..7a9bdc58aa3 100644 --- a/lib/ansible/modules/cloud/google/gcp_sql_instance_facts.py +++ b/lib/ansible/modules/cloud/google/gcp_sql_instance_facts.py @@ -53,8 +53,8 @@ EXAMPLES = ''' ''' RETURN = ''' -items: - description: List of items +resources: + description: List of resources returned: always type: complex contains: @@ -373,7 +373,7 @@ def main(): items = items.get('items') else: items = [] - return_value = {'items': items} + return_value = {'resources': items} module.exit_json(**return_value) diff --git a/lib/ansible/modules/cloud/google/gcp_sql_user_facts.py b/lib/ansible/modules/cloud/google/gcp_sql_user_facts.py index 3cfe6326594..5b151ef59e6 100644 --- a/lib/ansible/modules/cloud/google/gcp_sql_user_facts.py +++ b/lib/ansible/modules/cloud/google/gcp_sql_user_facts.py @@ -63,8 +63,8 @@ EXAMPLES = ''' ''' RETURN = ''' -items: - description: List of items +resources: + description: List of resources returned: always type: complex contains: @@ -114,7 +114,7 @@ def main(): items = items.get('items') else: items = [] - return_value = {'items': items} + return_value = {'resources': items} module.exit_json(**return_value) diff --git a/test/integration/targets/gcp_sql_instance/tasks/main.yml b/test/integration/targets/gcp_sql_instance/tasks/main.yml index eace727c02f..af75218749b 100644 --- a/test/integration/targets/gcp_sql_instance/tasks/main.yml +++ b/test/integration/targets/gcp_sql_instance/tasks/main.yml @@ -59,7 +59,7 @@ - name: verify that command succeeded assert: that: - - results['items'] | length >= 1 + - results['resources'] | length >= 1 # ---------------------------------------------------------------------------- - name: create a instance that already exists gcp_sql_instance: @@ -113,7 +113,7 @@ - name: verify that command succeeded assert: that: - - results['items'] | length == 0 + - results['resources'] | length == 0 # ---------------------------------------------------------------------------- - name: delete a instance that does not exist gcp_sql_instance: