1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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'
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')

View file

@ -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 }}"
register: results
- name: verify that command succeeded
assert:
that:
- results.rc == 0
# ----------------------------------------------------------------------------
- name: create a target pool that already exists
gcp_compute_target_pool:
name: "{{ resource_name }}"
region: 'us-west1'
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['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
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 }}"
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"
# ----------------------------------------------------------------------------
- name: delete a target pool that does not exist
gcp_compute_target_pool:
name: "{{ resource_name }}"
region: 'us-west1'
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['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
register: result
- name: assert changed is false