From d1399ec5a28e01625e4fc58896a077080e0f52f8 Mon Sep 17 00:00:00 2001 From: Alex Stephen Date: Mon, 13 Aug 2018 07:55:21 -0700 Subject: [PATCH] Bug fixes for gcp_compute_backend_service (#42805) --- .../google/gcp_compute_backend_service.py | 81 +++++++------ .../tasks/main.yml | 112 ++++++++---------- 2 files changed, 97 insertions(+), 96 deletions(-) diff --git a/lib/ansible/modules/cloud/google/gcp_compute_backend_service.py b/lib/ansible/modules/cloud/google/gcp_compute_backend_service.py index 101180115e..ea54f18f97 100644 --- a/lib/ansible/modules/cloud/google/gcp_compute_backend_service.py +++ b/lib/ansible/modules/cloud/google/gcp_compute_backend_service.py @@ -83,7 +83,12 @@ options: required: false group: description: - - A reference to InstanceGroup resource. + - This instance group defines the list of instances that serve traffic. Member virtual + machine instances from each instance group must live in the same zone as the instance + group itself. + - No two backends in a backend service are allowed to use same Instance Group resource. + - When the BackendService has load balancing scheme INTERNAL, the instance group must + be in a zone within the same region as the BackendService. required: false max_connections: description: @@ -215,7 +220,8 @@ options: choices: ['HTTP', 'HTTPS', 'TCP', 'SSL'] region: description: - - A reference to Region resource. + - The region where the regional backend service resides. + - This field is not applicable to global backend services. required: false session_affinity: description: @@ -238,19 +244,17 @@ extends_documentation_fragment: gcp EXAMPLES = ''' - name: create a instance group gcp_compute_instance_group: - name: 'instancegroup-backendservice' - zone: 'us-central1-a' + name: "instancegroup-backendservice" + 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: instancegroup - name: create a http health check gcp_compute_http_health_check: - name: 'httphealthcheck-backendservice' + name: "httphealthcheck-backendservice" healthy_threshold: 10 port: 8080 timeout_sec: 2 @@ -258,24 +262,20 @@ EXAMPLES = ''' project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: present register: healthcheck - name: create a backend service gcp_compute_backend_service: - name: testObject + name: "test_object" backends: - - group: "{{ instancegroup }}" + - group: "{{ instancegroup }}" health_checks: - - "{{ healthcheck.selfLink }}" + - "{{ healthcheck.selfLink }}" enable_cdn: true - project: testProject - auth_kind: service_account - service_account_file: /tmp/auth.pem - scopes: - - https://www.googleapis.com/auth/compute + project: "test_project" + auth_kind: "service_account" + service_account_file: "/tmp/auth.pem" state: present ''' @@ -320,7 +320,12 @@ RETURN = ''' type: str group: description: - - A reference to InstanceGroup resource. + - This instance group defines the list of instances that serve traffic. Member virtual + machine instances from each instance group must live in the same zone as the instance + group itself. + - No two backends in a backend service are allowed to use same Instance Group resource. + - When the BackendService has load balancing scheme INTERNAL, the instance group must + be in a zone within the same region as the BackendService. returned: success type: dict max_connections: @@ -478,7 +483,8 @@ RETURN = ''' type: str region: description: - - A reference to Region resource. + - The region where the regional backend service resides. + - This field is not applicable to global backend services. returned: success type: str session_affinity: @@ -554,6 +560,9 @@ def main(): ) ) + if not module.params['scopes']: + module.params['scopes'] = ['https://www.googleapis.com/auth/compute'] + state = module.params['state'] kind = 'compute#backendService' @@ -563,10 +572,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: @@ -586,12 +595,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)) @@ -600,9 +609,9 @@ def resource_to_request(module): request = { u'kind': 'compute#backendService', u'affinityCookieTtlSec': module.params.get('affinity_cookie_ttl_sec'), - u'backends': BackendServiceBackendArray(module.params.get('backends', []), module).to_request(), - u'cdnPolicy': BackeServiCdnPolic(module.params.get('cdn_policy', {}), module).to_request(), - u'connectionDraining': BackeServiConneDrain(module.params.get('connection_draining', {}), module).to_request(), + u'backends': BackendServiceBackendsArray(module.params.get('backends', []), module).to_request(), + u'cdnPolicy': BackendServiceCdnPolicy(module.params.get('cdn_policy', {}), module).to_request(), + u'connectionDraining': BackendServiceConnectionDraining(module.params.get('connection_draining', {}), module).to_request(), u'description': module.params.get('description'), u'enableCDN': module.params.get('enable_cdn'), u'healthChecks': module.params.get('health_checks'), @@ -680,9 +689,9 @@ def is_different(module, response): def response_to_hash(module, response): return { u'affinityCookieTtlSec': response.get(u'affinityCookieTtlSec'), - u'backends': BackendServiceBackendArray(response.get(u'backends', []), module).from_response(), - u'cdnPolicy': BackeServiCdnPolic(response.get(u'cdnPolicy', {}), module).from_response(), - u'connectionDraining': BackeServiConneDrain(response.get(u'connectionDraining', {}), module).from_response(), + u'backends': BackendServiceBackendsArray(response.get(u'backends', []), module).from_response(), + u'cdnPolicy': BackendServiceCdnPolicy(response.get(u'cdnPolicy', {}), module).from_response(), + u'connectionDraining': BackendServiceConnectionDraining(response.get(u'connectionDraining', {}), module).from_response(), u'creationTimestamp': response.get(u'creationTimestamp'), u'description': response.get(u'description'), u'enableCDN': response.get(u'enableCDN'), @@ -718,7 +727,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#backendService') @@ -743,7 +752,7 @@ def raise_if_errors(response, err_path, module): module.fail_json(msg=errors) -class BackendServiceBackendArray(object): +class BackendServiceBackendsArray(object): def __init__(self, request, module): self.module = module if request: @@ -790,7 +799,7 @@ class BackendServiceBackendArray(object): }) -class BackeServiCdnPolic(object): +class BackendServiceCdnPolicy(object): def __init__(self, request, module): self.module = module if request: @@ -800,16 +809,16 @@ class BackeServiCdnPolic(object): def to_request(self): return remove_nones_from_dict({ - u'cacheKeyPolicy': BackServCachKeyPoli(self.request.get('cache_key_policy', {}), self.module).to_request() + u'cacheKeyPolicy': BackendServiceCacheKeyPolicy(self.request.get('cache_key_policy', {}), self.module).to_request() }) def from_response(self): return remove_nones_from_dict({ - u'cacheKeyPolicy': BackServCachKeyPoli(self.request.get(u'cacheKeyPolicy', {}), self.module).from_response() + u'cacheKeyPolicy': BackendServiceCacheKeyPolicy(self.request.get(u'cacheKeyPolicy', {}), self.module).from_response() }) -class BackServCachKeyPoli(object): +class BackendServiceCacheKeyPolicy(object): def __init__(self, request, module): self.module = module if request: @@ -836,7 +845,7 @@ class BackServCachKeyPoli(object): }) -class BackeServiConneDrain(object): +class BackendServiceConnectionDraining(object): def __init__(self, request, module): self.module = module if request: diff --git a/test/integration/targets/gcp_compute_backend_service/tasks/main.yml b/test/integration/targets/gcp_compute_backend_service/tasks/main.yml index 939750174f..c424ff3c3b 100644 --- a/test/integration/targets/gcp_compute_backend_service/tasks/main.yml +++ b/test/integration/targets/gcp_compute_backend_service/tasks/main.yml @@ -15,18 +15,16 @@ # Pre-test setup - name: create a instance group gcp_compute_instance_group: - name: 'instancegroup-backendservice' - zone: 'us-central1-a' + name: "instancegroup-backendservice" + 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: instancegroup - name: create a http health check gcp_compute_http_health_check: - name: 'httphealthcheck-backendservice' + name: "httphealthcheck-backendservice" healthy_threshold: 10 port: 8080 timeout_sec: 2 @@ -34,38 +32,32 @@ project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: present register: healthcheck - name: delete a backend service gcp_compute_backend_service: name: "{{ resource_name }}" backends: - - group: "{{ instancegroup }}" + - group: "{{ instancegroup }}" health_checks: - - "{{ healthcheck.selfLink }}" + - "{{ healthcheck.selfLink }}" enable_cdn: true 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 backend service gcp_compute_backend_service: name: "{{ resource_name }}" backends: - - group: "{{ instancegroup }}" + - group: "{{ instancegroup }}" health_checks: - - "{{ healthcheck.selfLink }}" + - "{{ healthcheck.selfLink }}" enable_cdn: true 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 @@ -74,27 +66,31 @@ - result.changed == true - "result.kind == 'compute#backendService'" - name: verify that backend_service was created - shell: | - gcloud compute backend-services describe --project="{{ gcp_project}}" --global "{{ resource_name }}" - register: results -- name: verify that command succeeded - assert: - that: - - results.rc == 0 -# ---------------------------------------------------------------------------- -- name: create a backend service that already exists - gcp_compute_backend_service: - name: "{{ resource_name }}" - backends: - - group: "{{ instancegroup }}" - health_checks: - - "{{ healthcheck.selfLink }}" - enable_cdn: true + gcp_compute_backend_service_facts: + filters: + - name = {{ resource_name }} 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 backend service that already exists + gcp_compute_backend_service: + name: "{{ resource_name }}" + backends: + - group: "{{ instancegroup }}" + health_checks: + - "{{ healthcheck.selfLink }}" + enable_cdn: true + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file }}" state: present register: result - name: assert changed is false @@ -107,15 +103,13 @@ gcp_compute_backend_service: name: "{{ resource_name }}" backends: - - group: "{{ instancegroup }}" + - group: "{{ instancegroup }}" health_checks: - - "{{ healthcheck.selfLink }}" + - "{{ healthcheck.selfLink }}" enable_cdn: true 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 @@ -124,29 +118,31 @@ - result.changed == true - result.has_key('kind') == False - name: verify that backend_service was deleted - shell: | - gcloud compute backend-services describe --project="{{ gcp_project}}" --global "{{ resource_name }}" - register: results - failed_when: results.rc == 0 -- name: verify that command succeeded - assert: - that: - - results.rc == 1 - - "\"'projects/{{ gcp_project }}/global/backendServices/{{ resource_name }}' was not found\" in results.stderr" -# ---------------------------------------------------------------------------- -- name: delete a backend service that does not exist - gcp_compute_backend_service: - name: "{{ resource_name }}" - backends: - - group: "{{ instancegroup }}" - health_checks: - - "{{ healthcheck.selfLink }}" - enable_cdn: true + gcp_compute_backend_service_facts: + filters: + - name = {{ resource_name }} 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 backend service that does not exist + gcp_compute_backend_service: + name: "{{ resource_name }}" + backends: + - group: "{{ instancegroup }}" + health_checks: + - "{{ healthcheck.selfLink }}" + enable_cdn: true + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file }}" state: absent register: result - name: assert changed is false @@ -158,7 +154,7 @@ # Post-test teardown - name: delete a http health check gcp_compute_http_health_check: - name: 'httphealthcheck-backendservice' + name: "httphealthcheck-backendservice" healthy_threshold: 10 port: 8080 timeout_sec: 2 @@ -166,18 +162,14 @@ project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: absent register: healthcheck - name: delete a instance group gcp_compute_instance_group: - name: 'instancegroup-backendservice' - zone: 'us-central1-a' + name: "instancegroup-backendservice" + 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: instancegroup