diff --git a/lib/ansible/modules/cloud/google/gcp_compute_health_check.py b/lib/ansible/modules/cloud/google/gcp_compute_health_check.py index de6fb88075d..a07c5d1030b 100644 --- a/lib/ansible/modules/cloud/google/gcp_compute_health_check.py +++ b/lib/ansible/modules/cloud/google/gcp_compute_health_check.py @@ -228,7 +228,7 @@ extends_documentation_fragment: gcp EXAMPLES = ''' - name: create a health check gcp_compute_health_check: - name: testObject + name: "test_object" type: TCP tcp_health_check: port_name: service-health @@ -237,11 +237,9 @@ EXAMPLES = ''' healthy_threshold: 10 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 ''' @@ -512,6 +510,9 @@ def main(): ) ) + if not module.params['scopes']: + module.params['scopes'] = ['https://www.googleapis.com/auth/compute'] + state = module.params['state'] kind = 'compute#healthCheck' @@ -564,10 +565,10 @@ def resource_to_request(module): u'timeoutSec': module.params.get('timeout_sec'), u'unhealthyThreshold': module.params.get('unhealthy_threshold'), u'type': module.params.get('type'), - u'httpHealthCheck': HealChecHttpHealChec(module.params.get('http_health_check', {}), module).to_request(), - u'httpsHealthCheck': HealChecHttpHealChec(module.params.get('https_health_check', {}), module).to_request(), - u'tcpHealthCheck': HealChecTcpHealChec(module.params.get('tcp_health_check', {}), module).to_request(), - u'sslHealthCheck': HealChecSslHealChec(module.params.get('ssl_health_check', {}), module).to_request() + u'httpHealthCheck': HealthCheckHttpHealthCheck(module.params.get('http_health_check', {}), module).to_request(), + u'httpsHealthCheck': HealthCheckHttpsHealthCheck(module.params.get('https_health_check', {}), module).to_request(), + u'tcpHealthCheck': HealthCheckTcpHealthCheck(module.params.get('tcp_health_check', {}), module).to_request(), + u'sslHealthCheck': HealthCheckSslHealthCheck(module.params.get('ssl_health_check', {}), module).to_request() } return_vals = {} for k, v in request.items(): @@ -644,10 +645,10 @@ def response_to_hash(module, response): u'timeoutSec': response.get(u'timeoutSec'), u'unhealthyThreshold': response.get(u'unhealthyThreshold'), u'type': response.get(u'type'), - u'httpHealthCheck': HealChecHttpHealChec(response.get(u'httpHealthCheck', {}), module).from_response(), - u'httpsHealthCheck': HealChecHttpHealChec(response.get(u'httpsHealthCheck', {}), module).from_response(), - u'tcpHealthCheck': HealChecTcpHealChec(response.get(u'tcpHealthCheck', {}), module).from_response(), - u'sslHealthCheck': HealChecSslHealChec(response.get(u'sslHealthCheck', {}), module).from_response() + u'httpHealthCheck': HealthCheckHttpHealthCheck(response.get(u'httpHealthCheck', {}), module).from_response(), + u'httpsHealthCheck': HealthCheckHttpsHealthCheck(response.get(u'httpsHealthCheck', {}), module).from_response(), + u'tcpHealthCheck': HealthCheckTcpHealthCheck(response.get(u'tcpHealthCheck', {}), module).from_response(), + u'sslHealthCheck': HealthCheckSslHealthCheck(response.get(u'sslHealthCheck', {}), module).from_response() } @@ -663,7 +664,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#healthCheck') @@ -688,7 +689,7 @@ def raise_if_errors(response, err_path, module): module.fail_json(msg=errors) -class HealChecHttpHealChec(object): +class HealthCheckHttpHealthCheck(object): def __init__(self, request, module): self.module = module if request: @@ -715,7 +716,7 @@ class HealChecHttpHealChec(object): }) -class HealChecHttpHealChec(object): +class HealthCheckHttpsHealthCheck(object): def __init__(self, request, module): self.module = module if request: @@ -742,7 +743,7 @@ class HealChecHttpHealChec(object): }) -class HealChecTcpHealChec(object): +class HealthCheckTcpHealthCheck(object): def __init__(self, request, module): self.module = module if request: @@ -769,7 +770,7 @@ class HealChecTcpHealChec(object): }) -class HealChecSslHealChec(object): +class HealthCheckSslHealthCheck(object): def __init__(self, request, module): self.module = module if request: diff --git a/test/integration/targets/gcp_compute_health_check/tasks/main.yml b/test/integration/targets/gcp_compute_health_check/tasks/main.yml index eaa77c272b7..91c3977e125 100644 --- a/test/integration/targets/gcp_compute_health_check/tasks/main.yml +++ b/test/integration/targets/gcp_compute_health_check/tasks/main.yml @@ -27,8 +27,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 health check @@ -45,8 +43,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 @@ -55,13 +51,19 @@ - result.changed == true - "result.kind == 'compute#healthCheck'" - name: verify that health_check was created - shell: | - gcloud compute health-checks describe --project="{{gcp_project}}" "{{ resource_name }}" + gcp_compute_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 health check that already exists gcp_compute_health_check: @@ -77,8 +79,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 @@ -101,8 +101,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 @@ -111,15 +109,19 @@ - result.changed == true - result.has_key('kind') == False - name: verify that health_check was deleted - shell: | - gcloud compute health-checks describe --project="{{gcp_project}}" "{{ resource_name }}" + gcp_compute_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/healthChecks/{{ resource_name }}' was not found\" in results.stderr" + - results['items'] | length == 0 # ---------------------------------------------------------------------------- - name: delete a health check that does not exist gcp_compute_health_check: @@ -135,8 +137,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