From 67b93ecd44997d00103bd934b66944b99d7b8729 Mon Sep 17 00:00:00 2001 From: Alex Stephen Date: Mon, 13 Aug 2018 09:08:42 -0700 Subject: [PATCH] Bug fixes for gcp_compute_https_health_check (#42813) --- .../modules/gcp_compute_https_health_check.py | 112 +++++++++--------- .../tasks/main.yml | 36 +++--- 2 files changed, 73 insertions(+), 75 deletions(-) diff --git a/plugins/modules/gcp_compute_https_health_check.py b/plugins/modules/gcp_compute_https_health_check.py index 6ee3685..2cf4962 100644 --- a/plugins/modules/gcp_compute_https_health_check.py +++ b/plugins/modules/gcp_compute_https_health_check.py @@ -32,8 +32,8 @@ DOCUMENTATION = ''' --- module: gcp_compute_https_health_check description: - - An HttpsHealthCheck resource. This resource defines a template for how - individual VMs should be checked for health, via HTTPS. + - An HttpsHealthCheck resource. This resource defines a template for how individual + VMs should be checked for health, via HTTPS. short_description: Creates a GCP HttpsHealthCheck version_added: 2.6 author: Google Inc. (@googlecloudplatform) @@ -45,39 +45,36 @@ options: state: description: - Whether the given object should exist in GCP - required: true choices: ['present', 'absent'] default: 'present' check_interval_sec: description: - - How often (in seconds) to send a health check. The default value - is 5 seconds. + - How often (in seconds) to send a health check. The default value is 5 seconds. required: false description: description: - - An optional description of this resource. Provide this property - when you create the resource. + - An optional description of this resource. Provide this property when you create + the resource. required: false healthy_threshold: description: - - A so-far unhealthy instance will be marked healthy after this many - consecutive successes. The default value is 2. + - A so-far unhealthy instance will be marked healthy after this many consecutive successes. + The default value is 2. required: false host: description: - - The value of the host header in the HTTPS health check request. If - left empty (default value), the public IP on behalf of which this - health check is performed will be used. + - The value of the host header in the HTTPS health check request. If left empty (default + value), the public IP on behalf of which this health check is performed will be + used. required: false name: description: - - Name of the resource. Provided by the client when the resource is - created. The name must be 1-63 characters long, and comply with - RFC1035. Specifically, the name must be 1-63 characters long and - match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` which - means the first character must be a lowercase letter, and all - following characters must be a dash, lowercase letter, or digit, - except the last character, which cannot be a dash. + - Name of the resource. Provided by the client when the resource is created. The name + must be 1-63 characters long, and comply with RFC1035. Specifically, the name must + be 1-63 characters long and match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` + which means the first character must be a lowercase letter, and all following characters + must be a dash, lowercase letter, or digit, except the last character, which cannot + be a dash. required: true port: description: @@ -92,39 +89,39 @@ options: timeout_sec: description: - How long (in seconds) to wait before claiming failure. - - The default value is 5 seconds. It is invalid for timeoutSec to - have greater value than checkIntervalSec. + - The default value is 5 seconds. It is invalid for timeoutSec to have greater value + than checkIntervalSec. required: false aliases: [timeout_seconds] unhealthy_threshold: description: - - A so-far healthy instance will be marked unhealthy after this many - consecutive failures. The default value is 2. + - A so-far healthy instance will be marked unhealthy after this many consecutive failures. + The default value is 2. required: false extends_documentation_fragment: gcp +notes: + - "API Reference: U(https://cloud.google.com/compute/docs/reference/latest/httpsHealthChecks)" + - "Adding Health Checks: U(https://cloud.google.com/compute/docs/load-balancing/health-checks#legacy_health_checks)" ''' EXAMPLES = ''' - name: create a https health check gcp_compute_https_health_check: - name: testObject + name: "test_object" healthy_threshold: 10 port: 8080 timeout_sec: 2 unhealthy_threshold: 5 - 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 ''' RETURN = ''' check_interval_sec: description: - - How often (in seconds) to send a health check. The default value - is 5 seconds. + - How often (in seconds) to send a health check. The default value is 5 seconds. returned: success type: int creation_timestamp: @@ -134,38 +131,36 @@ RETURN = ''' type: str description: description: - - An optional description of this resource. Provide this property - when you create the resource. + - An optional description of this resource. Provide this property when you create + the resource. returned: success type: str healthy_threshold: description: - - A so-far unhealthy instance will be marked healthy after this many - consecutive successes. The default value is 2. + - A so-far unhealthy instance will be marked healthy after this many consecutive successes. + The default value is 2. returned: success type: int host: description: - - The value of the host header in the HTTPS health check request. If - left empty (default value), the public IP on behalf of which this - health check is performed will be used. + - The value of the host header in the HTTPS health check request. If left empty (default + value), the public IP on behalf of which this health check is performed will be + used. returned: success type: str id: description: - - The unique identifier for the resource. This identifier is defined - by the server. + - The unique identifier for the resource. This identifier is defined by the server. returned: success type: int name: description: - - Name of the resource. Provided by the client when the resource is - created. The name must be 1-63 characters long, and comply with - RFC1035. Specifically, the name must be 1-63 characters long and - match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` which - means the first character must be a lowercase letter, and all - following characters must be a dash, lowercase letter, or digit, - except the last character, which cannot be a dash. + - Name of the resource. Provided by the client when the resource is created. The name + must be 1-63 characters long, and comply with RFC1035. Specifically, the name must + be 1-63 characters long and match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` + which means the first character must be a lowercase letter, and all following characters + must be a dash, lowercase letter, or digit, except the last character, which cannot + be a dash. returned: success type: str port: @@ -183,14 +178,14 @@ RETURN = ''' timeout_sec: description: - How long (in seconds) to wait before claiming failure. - - The default value is 5 seconds. It is invalid for timeoutSec to - have greater value than checkIntervalSec. + - The default value is 5 seconds. It is invalid for timeoutSec to have greater value + than checkIntervalSec. returned: success type: int unhealthy_threshold: description: - - A so-far healthy instance will be marked unhealthy after this many - consecutive failures. The default value is 2. + - A so-far healthy instance will be marked unhealthy after this many consecutive failures. + The default value is 2. returned: success type: int ''' @@ -226,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#httpsHealthCheck' @@ -235,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: @@ -258,12 +256,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)) @@ -353,7 +351,7 @@ def response_to_hash(module, response): u'healthyThreshold': response.get(u'healthyThreshold'), u'host': response.get(u'host'), u'id': response.get(u'id'), - u'name': response.get(u'name'), + u'name': module.params.get('name'), u'port': response.get(u'port'), u'requestPath': response.get(u'requestPath'), u'timeoutSec': response.get(u'timeoutSec'), @@ -373,7 +371,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#httpsHealthCheck') diff --git a/tests/integration/gcp_compute_https_health_check/tasks/main.yml b/tests/integration/gcp_compute_https_health_check/tasks/main.yml index 3fa9c95..dbe3cf7 100644 --- a/tests/integration/gcp_compute_https_health_check/tasks/main.yml +++ b/tests/integration/gcp_compute_https_health_check/tasks/main.yml @@ -23,8 +23,6 @@ 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 https health check @@ -37,8 +35,6 @@ 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 @@ -47,13 +43,19 @@ - result.changed == true - "result.kind == 'compute#httpsHealthCheck'" - name: verify that https_health_check was created - shell: | - gcloud compute https-health-checks describe --project="{{gcp_project}}" "{{ resource_name }}" + gcp_compute_https_health_check_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.rc == 0 + - results['items'] | length == 1 # ---------------------------------------------------------------------------- - name: create a https health check that already exists gcp_compute_https_health_check: @@ -65,8 +67,6 @@ 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 @@ -85,8 +85,6 @@ 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 @@ -95,15 +93,19 @@ - result.changed == true - result.has_key('kind') == False - name: verify that https_health_check was deleted - shell: | - gcloud compute https-health-checks describe --project="{{gcp_project}}" "{{ resource_name }}" + gcp_compute_https_health_check_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 - failed_when: results.rc == 0 - name: verify that command succeeded assert: that: - - results.rc == 1 - - "\"'projects/{{ gcp_project }}/global/httpsHealthChecks/{{ resource_name }}' was not found\" in results.stderr" + - results['items'] | length == 0 # ---------------------------------------------------------------------------- - name: delete a https health check that does not exist gcp_compute_https_health_check: @@ -115,8 +117,6 @@ 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