From 829dcf1d233b276c7353e6954377ce3e98ae9cce Mon Sep 17 00:00:00 2001 From: Alex Stephen Date: Mon, 13 Aug 2018 12:29:27 -0700 Subject: [PATCH] Bug fixes for gcp_compute_instance_group (#42816) --- .../google/gcp_compute_instance_group.py | 55 +++++---- .../gcp_compute_instance_group/tasks/main.yml | 108 +++++++++--------- 2 files changed, 80 insertions(+), 83 deletions(-) diff --git a/lib/ansible/modules/cloud/google/gcp_compute_instance_group.py b/lib/ansible/modules/cloud/google/gcp_compute_instance_group.py index ec2fcb9fa49..276798dcc82 100644 --- a/lib/ansible/modules/cloud/google/gcp_compute_instance_group.py +++ b/lib/ansible/modules/cloud/google/gcp_compute_instance_group.py @@ -80,19 +80,19 @@ options: required: false network: description: - - A reference to Network resource. + - The network to which all instances in the instance group belong. required: false region: description: - - A reference to Region resource. + - The region where the instance group is located (for regional resources). required: false subnetwork: description: - - A reference to Subnetwork resource. + - The subnetwork to which all instances in the instance group belong. required: false zone: description: - - A reference to Zone resource. + - A reference to the zone where the instance group resides. required: true extends_documentation_fragment: gcp ''' @@ -100,28 +100,24 @@ extends_documentation_fragment: gcp EXAMPLES = ''' - name: create a network gcp_compute_network: - name: 'network-instancegroup' + name: "network-instancegroup" project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: present register: network - name: create a instance group gcp_compute_instance_group: - name: testObject + name: "test_object" named_ports: - - name: ansible - port: 1234 + - name: ansible + port: 1234 network: "{{ network }}" - zone: 'us-central1-a' - project: testProject - auth_kind: service_account - service_account_file: /tmp/auth.pem - scopes: - - https://www.googleapis.com/auth/compute + zone: us-central1-a + project: "test_project" + auth_kind: "service_account" + service_account_file: "/tmp/auth.pem" state: present ''' @@ -172,22 +168,22 @@ RETURN = ''' type: int network: description: - - A reference to Network resource. + - The network to which all instances in the instance group belong. returned: success type: dict region: description: - - A reference to Region resource. + - The region where the instance group is located (for regional resources). returned: success type: str subnetwork: description: - - A reference to Subnetwork resource. + - The subnetwork to which all instances in the instance group belong. returned: success type: dict zone: description: - - A reference to Zone resource. + - A reference to the zone where the instance group resides. returned: success type: str ''' @@ -225,6 +221,9 @@ def main(): ) ) + if not module.params['scopes']: + module.params['scopes'] = ['https://www.googleapis.com/auth/compute'] + state = module.params['state'] kind = 'compute#instanceGroup' @@ -234,10 +233,10 @@ def main(): if fetch: if state == 'present': if is_different(module, fetch): - fetch = update(module, self_link(module), kind, fetch) + fetch = update(module, self_link(module), kind) changed = True else: - delete(module, self_link(module), kind, fetch) + delete(module, self_link(module), kind) fetch = {} changed = True else: @@ -257,11 +256,11 @@ def create(module, link, kind): return wait_for_operation(module, auth.post(link, resource_to_request(module))) -def update(module, link, kind, fetch): +def update(module, link, kind): module.fail_json(msg="InstanceGroup cannot be edited") -def delete(module, link, kind, fetch): +def delete(module, link, kind): auth = GcpSession(module, 'compute') return wait_for_operation(module, auth.delete(link)) @@ -271,7 +270,7 @@ def resource_to_request(module): u'kind': 'compute#instanceGroup', u'description': module.params.get('description'), u'name': module.params.get('name'), - u'namedPorts': InstaGroupNamedPortsArray(module.params.get('named_ports', []), module).to_request(), + u'namedPorts': InstanceGroupNamedPortsArray(module.params.get('named_ports', []), module).to_request(), u'network': replace_resource_dict(module.params.get(u'network', {}), 'selfLink'), u'region': region_selflink(module.params.get('region'), module.params), u'subnetwork': replace_resource_dict(module.params.get(u'subnetwork', {}), 'selfLink') @@ -346,7 +345,7 @@ def response_to_hash(module, response): u'description': response.get(u'description'), u'id': response.get(u'id'), u'name': response.get(u'name'), - u'namedPorts': InstaGroupNamedPortsArray(response.get(u'namedPorts', []), module).from_response(), + u'namedPorts': InstanceGroupNamedPortsArray(response.get(u'namedPorts', []), module).from_response(), u'network': response.get(u'network'), u'region': response.get(u'region'), u'subnetwork': response.get(u'subnetwork') @@ -374,7 +373,7 @@ def async_op_url(module, extra_data=None): def wait_for_operation(module, response): op_result = return_if_object(module, response, 'compute#operation') if op_result is None: - return None + return {} status = navigate_hash(op_result, ['status']) wait_done = wait_for_completion(status, op_result, module) return fetch_resource(module, navigate_hash(wait_done, ['targetLink']), 'compute#instanceGroup') @@ -399,7 +398,7 @@ def raise_if_errors(response, err_path, module): module.fail_json(msg=errors) -class InstaGroupNamedPortsArray(object): +class InstanceGroupNamedPortsArray(object): def __init__(self, request, module): self.module = module if request: diff --git a/test/integration/targets/gcp_compute_instance_group/tasks/main.yml b/test/integration/targets/gcp_compute_instance_group/tasks/main.yml index 011de8f99f9..32c7d237dca 100644 --- a/test/integration/targets/gcp_compute_instance_group/tasks/main.yml +++ b/test/integration/targets/gcp_compute_instance_group/tasks/main.yml @@ -15,42 +15,36 @@ # Pre-test setup - name: create a network gcp_compute_network: - name: 'network-instancegroup' + name: "network-instancegroup" project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: present register: network - name: delete a instance group gcp_compute_instance_group: name: "{{ resource_name }}" named_ports: - - name: ansible - port: 1234 + - name: ansible + port: 1234 network: "{{ network }}" - zone: 'us-central1-a' + zone: us-central1-a project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: absent #---------------------------------------------------------- - name: create a instance group gcp_compute_instance_group: name: "{{ resource_name }}" named_ports: - - name: ansible - port: 1234 + - name: ansible + port: 1234 network: "{{ network }}" - zone: 'us-central1-a' + zone: us-central1-a project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: present register: result - name: assert changed is true @@ -59,27 +53,32 @@ - result.changed == true - "result.kind == 'compute#instanceGroup'" - name: verify that instance_group was created - shell: | - gcloud compute instance-groups unmanaged describe --project="{{ gcp_project }}" --zone="us-central1-a" "{{ resource_name }}" - register: results -- name: verify that command succeeded - assert: - that: - - results.rc == 0 -# ---------------------------------------------------------------------------- -- name: create a instance group that already exists - gcp_compute_instance_group: - name: "{{ resource_name }}" - named_ports: - - name: ansible - port: 1234 - network: "{{ network }}" - zone: 'us-central1-a' + gcp_compute_instance_group_facts: + filters: + - name = {{ resource_name }} + zone: us-central1-a project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" scopes: - https://www.googleapis.com/auth/compute + register: results +- name: verify that command succeeded + assert: + that: + - results['items'] | length == 1 +# ---------------------------------------------------------------------------- +- name: create a instance group that already exists + gcp_compute_instance_group: + name: "{{ resource_name }}" + named_ports: + - name: ansible + port: 1234 + network: "{{ network }}" + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file }}" state: present register: result - name: assert changed is false @@ -92,15 +91,13 @@ gcp_compute_instance_group: name: "{{ resource_name }}" named_ports: - - name: ansible - port: 1234 + - name: ansible + port: 1234 network: "{{ network }}" - zone: 'us-central1-a' + zone: us-central1-a project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: absent register: result - name: assert changed is true @@ -109,29 +106,32 @@ - result.changed == true - result.has_key('kind') == False - name: verify that instance_group was deleted - shell: | - gcloud compute instance-groups unmanaged describe --project="{{ gcp_project }}" --zone="us-central1-a" "{{ resource_name }}" - register: results - failed_when: results.rc == 0 -- name: verify that command succeeded - assert: - that: - - results.rc == 1 - - "\"'projects/{{ gcp_project }}/zones/us-central1-a/instanceGroups/{{ resource_name }}' was not found\" in results.stderr" -# ---------------------------------------------------------------------------- -- name: delete a instance group that does not exist - gcp_compute_instance_group: - name: "{{ resource_name }}" - named_ports: - - name: ansible - port: 1234 - network: "{{ network }}" - zone: 'us-central1-a' + gcp_compute_instance_group_facts: + filters: + - name = {{ resource_name }} + zone: us-central1-a project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" scopes: - https://www.googleapis.com/auth/compute + register: results +- name: verify that command succeeded + assert: + that: + - results['items'] | length == 0 +# ---------------------------------------------------------------------------- +- name: delete a instance group that does not exist + gcp_compute_instance_group: + name: "{{ resource_name }}" + named_ports: + - name: ansible + port: 1234 + network: "{{ network }}" + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file }}" state: absent register: result - name: assert changed is false @@ -143,11 +143,9 @@ # Post-test teardown - name: delete a network gcp_compute_network: - name: 'network-instancegroup' + name: "network-instancegroup" project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: absent register: network