From b88442064fbdafadf6976ea129c6b6cc8306aa1b Mon Sep 17 00:00:00 2001 From: Alex Stephen Date: Tue, 14 Aug 2018 06:52:40 -0700 Subject: [PATCH] Bug fixes for gcp_compute_target_pool (#42825) --- .../cloud/google/gcp_compute_target_pool.py | 72 +++++++++++++------ .../gcp_compute_target_pool/tasks/main.yml | 48 +++++++------ 2 files changed, 76 insertions(+), 44 deletions(-) diff --git a/lib/ansible/modules/cloud/google/gcp_compute_target_pool.py b/lib/ansible/modules/cloud/google/gcp_compute_target_pool.py index e033b94db27..dac771de36d 100644 --- a/lib/ansible/modules/cloud/google/gcp_compute_target_pool.py +++ b/lib/ansible/modules/cloud/google/gcp_compute_target_pool.py @@ -48,7 +48,17 @@ options: default: 'present' backup_pool: description: - - A reference to TargetPool resource. + - This field is applicable only when the containing target pool is serving a forwarding + rule as the primary pool, and its failoverRatio field is properly set to a value + between [0, 1]. + - 'backupPool and failoverRatio together define the fallback behavior of the primary + target pool: if the ratio of the healthy instances in the primary pool is at or + below failoverRatio, traffic arriving at the load-balanced IP will be directed to + the backup pool.' + - In case where failoverRatio and backupPool are not set, or all the instances in + the backup pool are unhealthy, the traffic will be directed back to the primary + pool in the "force" mode, where traffic will be spread to the healthy instances + with the best effort, or to all instances when no instance is healthy. required: false description: description: @@ -70,7 +80,10 @@ options: required: false health_check: description: - - A reference to HttpHealthCheck resource. + - A reference to a HttpHealthCheck resource. + - A member instance in this pool is considered healthy if and only if the health checks + pass. If not specified it means all member instances will be considered healthy + at all times. required: false instances: description: @@ -98,28 +111,39 @@ options: choices: ['NONE', 'CLIENT_IP', 'CLIENT_IP_PROTO'] region: description: - - A reference to Region resource. + - The region where the target pool resides. required: true extends_documentation_fragment: gcp +notes: + - "API Reference: U(https://cloud.google.com/compute/docs/reference/rest/v1/targetPools)" + - "Official Documentation: U(https://cloud.google.com/compute/docs/load-balancing/network/target-pools)" ''' EXAMPLES = ''' - name: create a target pool gcp_compute_target_pool: - name: testObject - region: 'us-west1' - project: testProject - auth_kind: service_account - service_account_file: /tmp/auth.pem - scopes: - - https://www.googleapis.com/auth/compute + name: "test_object" + region: us-west1 + project: "test_project" + auth_kind: "service_account" + service_account_file: "/tmp/auth.pem" state: present ''' RETURN = ''' backup_pool: description: - - A reference to TargetPool resource. + - This field is applicable only when the containing target pool is serving a forwarding + rule as the primary pool, and its failoverRatio field is properly set to a value + between [0, 1]. + - 'backupPool and failoverRatio together define the fallback behavior of the primary + target pool: if the ratio of the healthy instances in the primary pool is at or + below failoverRatio, traffic arriving at the load-balanced IP will be directed to + the backup pool.' + - In case where failoverRatio and backupPool are not set, or all the instances in + the backup pool are unhealthy, the traffic will be directed back to the primary + pool in the "force" mode, where traffic will be spread to the healthy instances + with the best effort, or to all instances when no instance is healthy. returned: success type: dict creation_timestamp: @@ -149,7 +173,10 @@ RETURN = ''' type: str health_check: description: - - A reference to HttpHealthCheck resource. + - A reference to a HttpHealthCheck resource. + - A member instance in this pool is considered healthy if and only if the health checks + pass. If not specified it means all member instances will be considered healthy + at all times. returned: success type: dict id: @@ -185,7 +212,7 @@ RETURN = ''' type: str region: description: - - A reference to Region resource. + - The region where the target pool resides. returned: success type: str ''' @@ -220,6 +247,9 @@ def main(): ) ) + if not module.params['scopes']: + module.params['scopes'] = ['https://www.googleapis.com/auth/compute'] + state = module.params['state'] kind = 'compute#targetPool' @@ -229,10 +259,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: @@ -252,12 +282,12 @@ 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): auth = GcpSession(module, 'compute') return wait_for_operation(module, auth.put(link, resource_to_request(module))) -def delete(module, link, kind, fetch): +def delete(module, link, kind): auth = GcpSession(module, 'compute') return wait_for_operation(module, auth.delete(link)) @@ -343,15 +373,15 @@ def is_different(module, response): # This is for doing comparisons with Ansible's current parameters. def response_to_hash(module, response): return { - u'backupPool': response.get(u'backupPool'), + u'backupPool': replace_resource_dict(module.params.get(u'backup_pool', {}), 'selfLink'), u'creationTimestamp': response.get(u'creationTimestamp'), u'description': response.get(u'description'), u'failoverRatio': response.get(u'failoverRatio'), u'healthCheck': response.get(u'healthCheck'), u'id': response.get(u'id'), u'instances': response.get(u'instances'), - u'name': response.get(u'name'), - u'sessionAffinity': response.get(u'sessionAffinity') + u'name': module.params.get('name'), + u'sessionAffinity': module.params.get('session_affinity') } @@ -367,7 +397,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#targetPool') diff --git a/test/integration/targets/gcp_compute_target_pool/tasks/main.yml b/test/integration/targets/gcp_compute_target_pool/tasks/main.yml index e6c7826afb5..e08b6f55856 100644 --- a/test/integration/targets/gcp_compute_target_pool/tasks/main.yml +++ b/test/integration/targets/gcp_compute_target_pool/tasks/main.yml @@ -16,23 +16,19 @@ - name: delete a target pool gcp_compute_target_pool: name: "{{ resource_name }}" - region: 'us-west1' + region: us-west1 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 target pool gcp_compute_target_pool: name: "{{ resource_name }}" - region: 'us-west1' + region: us-west1 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 @@ -41,23 +37,28 @@ - result.changed == true - "result.kind == 'compute#targetPool'" - name: verify that target_pool was created - shell: | - gcloud compute target-pools describe --project="{{ gcp_project}}" --region=us-west1 "{{ resource_name }}" + gcp_compute_target_pool_facts: + filters: + - name = {{ resource_name }} + region: us-west1 + 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.rc == 0 + - results['items'] | length == 1 # ---------------------------------------------------------------------------- - name: create a target pool that already exists gcp_compute_target_pool: name: "{{ resource_name }}" - region: 'us-west1' + region: us-west1 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 false @@ -69,12 +70,10 @@ - name: delete a target pool gcp_compute_target_pool: name: "{{ resource_name }}" - region: 'us-west1' + region: us-west1 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 @@ -83,25 +82,28 @@ - result.changed == true - result.has_key('kind') == False - name: verify that target_pool was deleted - shell: | - gcloud compute target-pools describe --project="{{ gcp_project}}" --region=us-west1 "{{ resource_name }}" + gcp_compute_target_pool_facts: + filters: + - name = {{ resource_name }} + region: us-west1 + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file }}" + scopes: + - https://www.googleapis.com/auth/compute register: results - failed_when: results.rc == 0 - name: verify that command succeeded assert: that: - - results.rc == 1 - - "\"'projects/{{ gcp_project }}/regions/us-west1/targetPools/{{ resource_name }}' was not found\" in results.stderr" + - results['items'] | length == 0 # ---------------------------------------------------------------------------- - name: delete a target pool that does not exist gcp_compute_target_pool: name: "{{ resource_name }}" - region: 'us-west1' + region: us-west1 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 false