Bug fixes for gcp_compute_target_pool (#42825)

This commit is contained in:
Alex Stephen 2018-08-14 06:52:40 -07:00 committed by Ryan Brown
parent eaacda42f5
commit b88442064f
2 changed files with 88 additions and 56 deletions

View file

@ -48,7 +48,17 @@ options:
default: 'present' default: 'present'
backup_pool: backup_pool:
description: 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 required: false
description: description:
description: description:
@ -70,7 +80,10 @@ options:
required: false required: false
health_check: health_check:
description: 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 required: false
instances: instances:
description: description:
@ -98,28 +111,39 @@ options:
choices: ['NONE', 'CLIENT_IP', 'CLIENT_IP_PROTO'] choices: ['NONE', 'CLIENT_IP', 'CLIENT_IP_PROTO']
region: region:
description: description:
- A reference to Region resource. - The region where the target pool resides.
required: true required: true
extends_documentation_fragment: gcp 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 = ''' EXAMPLES = '''
- name: create a target pool - name: create a target pool
gcp_compute_target_pool: gcp_compute_target_pool:
name: testObject name: "test_object"
region: 'us-west1' region: us-west1
project: testProject project: "test_project"
auth_kind: service_account auth_kind: "service_account"
service_account_file: /tmp/auth.pem service_account_file: "/tmp/auth.pem"
scopes:
- https://www.googleapis.com/auth/compute
state: present state: present
''' '''
RETURN = ''' RETURN = '''
backup_pool: backup_pool:
description: 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 returned: success
type: dict type: dict
creation_timestamp: creation_timestamp:
@ -149,7 +173,10 @@ RETURN = '''
type: str type: str
health_check: health_check:
description: 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 returned: success
type: dict type: dict
id: id:
@ -185,7 +212,7 @@ RETURN = '''
type: str type: str
region: region:
description: description:
- A reference to Region resource. - The region where the target pool resides.
returned: success returned: success
type: str 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'] state = module.params['state']
kind = 'compute#targetPool' kind = 'compute#targetPool'
@ -229,10 +259,10 @@ def main():
if fetch: if fetch:
if state == 'present': if state == 'present':
if is_different(module, fetch): if is_different(module, fetch):
fetch = update(module, self_link(module), kind, fetch) fetch = update(module, self_link(module), kind)
changed = True changed = True
else: else:
delete(module, self_link(module), kind, fetch) delete(module, self_link(module), kind)
fetch = {} fetch = {}
changed = True changed = True
else: else:
@ -252,12 +282,12 @@ def create(module, link, kind):
return wait_for_operation(module, auth.post(link, resource_to_request(module))) 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') auth = GcpSession(module, 'compute')
return wait_for_operation(module, auth.put(link, resource_to_request(module))) 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') auth = GcpSession(module, 'compute')
return wait_for_operation(module, auth.delete(link)) 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. # This is for doing comparisons with Ansible's current parameters.
def response_to_hash(module, response): def response_to_hash(module, response):
return { 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'creationTimestamp': response.get(u'creationTimestamp'),
u'description': response.get(u'description'), u'description': response.get(u'description'),
u'failoverRatio': response.get(u'failoverRatio'), u'failoverRatio': response.get(u'failoverRatio'),
u'healthCheck': response.get(u'healthCheck'), u'healthCheck': response.get(u'healthCheck'),
u'id': response.get(u'id'), u'id': response.get(u'id'),
u'instances': response.get(u'instances'), u'instances': response.get(u'instances'),
u'name': response.get(u'name'), u'name': module.params.get('name'),
u'sessionAffinity': response.get(u'sessionAffinity') 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): def wait_for_operation(module, response):
op_result = return_if_object(module, response, 'compute#operation') op_result = return_if_object(module, response, 'compute#operation')
if op_result is None: if op_result is None:
return None return {}
status = navigate_hash(op_result, ['status']) status = navigate_hash(op_result, ['status'])
wait_done = wait_for_completion(status, op_result, module) wait_done = wait_for_completion(status, op_result, module)
return fetch_resource(module, navigate_hash(wait_done, ['targetLink']), 'compute#targetPool') return fetch_resource(module, navigate_hash(wait_done, ['targetLink']), 'compute#targetPool')

View file

@ -16,23 +16,19 @@
- name: delete a target pool - name: delete a target pool
gcp_compute_target_pool: gcp_compute_target_pool:
name: "{{ resource_name }}" name: "{{ resource_name }}"
region: 'us-west1' region: us-west1
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
state: absent state: absent
#---------------------------------------------------------- #----------------------------------------------------------
- name: create a target pool - name: create a target pool
gcp_compute_target_pool: gcp_compute_target_pool:
name: "{{ resource_name }}" name: "{{ resource_name }}"
region: 'us-west1' region: us-west1
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
state: present state: present
register: result register: result
- name: assert changed is true - name: assert changed is true
@ -41,23 +37,28 @@
- result.changed == true - result.changed == true
- "result.kind == 'compute#targetPool'" - "result.kind == 'compute#targetPool'"
- name: verify that target_pool was created - name: verify that target_pool was created
shell: | gcp_compute_target_pool_facts:
gcloud compute target-pools describe --project="{{ gcp_project}}" --region=us-west1 "{{ resource_name }}" filters:
register: results - name = {{ resource_name }}
- name: verify that command succeeded region: us-west1
assert:
that:
- results.rc == 0
# ----------------------------------------------------------------------------
- name: create a target pool that already exists
gcp_compute_target_pool:
name: "{{ resource_name }}"
region: 'us-west1'
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes: scopes:
- https://www.googleapis.com/auth/compute - https://www.googleapis.com/auth/compute
register: results
- name: verify that command succeeded
assert:
that:
- results['items'] | length == 1
# ----------------------------------------------------------------------------
- name: create a target pool that already exists
gcp_compute_target_pool:
name: "{{ resource_name }}"
region: us-west1
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
state: present state: present
register: result register: result
- name: assert changed is false - name: assert changed is false
@ -69,12 +70,10 @@
- name: delete a target pool - name: delete a target pool
gcp_compute_target_pool: gcp_compute_target_pool:
name: "{{ resource_name }}" name: "{{ resource_name }}"
region: 'us-west1' region: us-west1
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
state: absent state: absent
register: result register: result
- name: assert changed is true - name: assert changed is true
@ -83,25 +82,28 @@
- result.changed == true - result.changed == true
- result.has_key('kind') == False - result.has_key('kind') == False
- name: verify that target_pool was deleted - name: verify that target_pool was deleted
shell: | gcp_compute_target_pool_facts:
gcloud compute target-pools describe --project="{{ gcp_project}}" --region=us-west1 "{{ resource_name }}" filters:
register: results - name = {{ resource_name }}
failed_when: results.rc == 0 region: us-west1
- 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"
# ----------------------------------------------------------------------------
- name: delete a target pool that does not exist
gcp_compute_target_pool:
name: "{{ resource_name }}"
region: 'us-west1'
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes: scopes:
- https://www.googleapis.com/auth/compute - https://www.googleapis.com/auth/compute
register: results
- name: verify that command succeeded
assert:
that:
- results['items'] | length == 0
# ----------------------------------------------------------------------------
- name: delete a target pool that does not exist
gcp_compute_target_pool:
name: "{{ resource_name }}"
region: us-west1
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
state: absent state: absent
register: result register: result
- name: assert changed is false - name: assert changed is false