mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Bug fixes for gcp_compute_instance_group (#42816)
This commit is contained in:
parent
f977590a2a
commit
829dcf1d23
2 changed files with 80 additions and 83 deletions
|
@ -80,19 +80,19 @@ options:
|
||||||
required: false
|
required: false
|
||||||
network:
|
network:
|
||||||
description:
|
description:
|
||||||
- A reference to Network resource.
|
- The network to which all instances in the instance group belong.
|
||||||
required: false
|
required: false
|
||||||
region:
|
region:
|
||||||
description:
|
description:
|
||||||
- A reference to Region resource.
|
- The region where the instance group is located (for regional resources).
|
||||||
required: false
|
required: false
|
||||||
subnetwork:
|
subnetwork:
|
||||||
description:
|
description:
|
||||||
- A reference to Subnetwork resource.
|
- The subnetwork to which all instances in the instance group belong.
|
||||||
required: false
|
required: false
|
||||||
zone:
|
zone:
|
||||||
description:
|
description:
|
||||||
- A reference to Zone resource.
|
- A reference to the zone where the instance group resides.
|
||||||
required: true
|
required: true
|
||||||
extends_documentation_fragment: gcp
|
extends_documentation_fragment: gcp
|
||||||
'''
|
'''
|
||||||
|
@ -100,28 +100,24 @@ extends_documentation_fragment: gcp
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
- name: create a network
|
- name: create a network
|
||||||
gcp_compute_network:
|
gcp_compute_network:
|
||||||
name: 'network-instancegroup'
|
name: "network-instancegroup"
|
||||||
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: network
|
register: network
|
||||||
|
|
||||||
- name: create a instance group
|
- name: create a instance group
|
||||||
gcp_compute_instance_group:
|
gcp_compute_instance_group:
|
||||||
name: testObject
|
name: "test_object"
|
||||||
named_ports:
|
named_ports:
|
||||||
- name: ansible
|
- name: ansible
|
||||||
port: 1234
|
port: 1234
|
||||||
network: "{{ network }}"
|
network: "{{ network }}"
|
||||||
zone: 'us-central1-a'
|
zone: us-central1-a
|
||||||
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
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
@ -172,22 +168,22 @@ RETURN = '''
|
||||||
type: int
|
type: int
|
||||||
network:
|
network:
|
||||||
description:
|
description:
|
||||||
- A reference to Network resource.
|
- The network to which all instances in the instance group belong.
|
||||||
returned: success
|
returned: success
|
||||||
type: dict
|
type: dict
|
||||||
region:
|
region:
|
||||||
description:
|
description:
|
||||||
- A reference to Region resource.
|
- The region where the instance group is located (for regional resources).
|
||||||
returned: success
|
returned: success
|
||||||
type: str
|
type: str
|
||||||
subnetwork:
|
subnetwork:
|
||||||
description:
|
description:
|
||||||
- A reference to Subnetwork resource.
|
- The subnetwork to which all instances in the instance group belong.
|
||||||
returned: success
|
returned: success
|
||||||
type: dict
|
type: dict
|
||||||
zone:
|
zone:
|
||||||
description:
|
description:
|
||||||
- A reference to Zone resource.
|
- A reference to the zone where the instance group resides.
|
||||||
returned: success
|
returned: success
|
||||||
type: str
|
type: str
|
||||||
'''
|
'''
|
||||||
|
@ -225,6 +221,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#instanceGroup'
|
kind = 'compute#instanceGroup'
|
||||||
|
|
||||||
|
@ -234,10 +233,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:
|
||||||
|
@ -257,11 +256,11 @@ 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):
|
||||||
module.fail_json(msg="InstanceGroup cannot be edited")
|
module.fail_json(msg="InstanceGroup cannot be edited")
|
||||||
|
|
||||||
|
|
||||||
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))
|
||||||
|
|
||||||
|
@ -271,7 +270,7 @@ def resource_to_request(module):
|
||||||
u'kind': 'compute#instanceGroup',
|
u'kind': 'compute#instanceGroup',
|
||||||
u'description': module.params.get('description'),
|
u'description': module.params.get('description'),
|
||||||
u'name': module.params.get('name'),
|
u'name': module.params.get('name'),
|
||||||
u'namedPorts': InstaGroupNamedPortsArray(module.params.get('named_ports', []), module).to_request(),
|
u'namedPorts': InstanceGroupNamedPortsArray(module.params.get('named_ports', []), module).to_request(),
|
||||||
u'network': replace_resource_dict(module.params.get(u'network', {}), 'selfLink'),
|
u'network': replace_resource_dict(module.params.get(u'network', {}), 'selfLink'),
|
||||||
u'region': region_selflink(module.params.get('region'), module.params),
|
u'region': region_selflink(module.params.get('region'), module.params),
|
||||||
u'subnetwork': replace_resource_dict(module.params.get(u'subnetwork', {}), 'selfLink')
|
u'subnetwork': replace_resource_dict(module.params.get(u'subnetwork', {}), 'selfLink')
|
||||||
|
@ -346,7 +345,7 @@ def response_to_hash(module, response):
|
||||||
u'description': response.get(u'description'),
|
u'description': response.get(u'description'),
|
||||||
u'id': response.get(u'id'),
|
u'id': response.get(u'id'),
|
||||||
u'name': response.get(u'name'),
|
u'name': response.get(u'name'),
|
||||||
u'namedPorts': InstaGroupNamedPortsArray(response.get(u'namedPorts', []), module).from_response(),
|
u'namedPorts': InstanceGroupNamedPortsArray(response.get(u'namedPorts', []), module).from_response(),
|
||||||
u'network': response.get(u'network'),
|
u'network': response.get(u'network'),
|
||||||
u'region': response.get(u'region'),
|
u'region': response.get(u'region'),
|
||||||
u'subnetwork': response.get(u'subnetwork')
|
u'subnetwork': response.get(u'subnetwork')
|
||||||
|
@ -374,7 +373,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#instanceGroup')
|
return fetch_resource(module, navigate_hash(wait_done, ['targetLink']), 'compute#instanceGroup')
|
||||||
|
@ -399,7 +398,7 @@ def raise_if_errors(response, err_path, module):
|
||||||
module.fail_json(msg=errors)
|
module.fail_json(msg=errors)
|
||||||
|
|
||||||
|
|
||||||
class InstaGroupNamedPortsArray(object):
|
class InstanceGroupNamedPortsArray(object):
|
||||||
def __init__(self, request, module):
|
def __init__(self, request, module):
|
||||||
self.module = module
|
self.module = module
|
||||||
if request:
|
if request:
|
||||||
|
|
|
@ -15,42 +15,36 @@
|
||||||
# Pre-test setup
|
# Pre-test setup
|
||||||
- name: create a network
|
- name: create a network
|
||||||
gcp_compute_network:
|
gcp_compute_network:
|
||||||
name: 'network-instancegroup'
|
name: "network-instancegroup"
|
||||||
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: network
|
register: network
|
||||||
- name: delete a instance group
|
- name: delete a instance group
|
||||||
gcp_compute_instance_group:
|
gcp_compute_instance_group:
|
||||||
name: "{{ resource_name }}"
|
name: "{{ resource_name }}"
|
||||||
named_ports:
|
named_ports:
|
||||||
- name: ansible
|
- name: ansible
|
||||||
port: 1234
|
port: 1234
|
||||||
network: "{{ network }}"
|
network: "{{ network }}"
|
||||||
zone: 'us-central1-a'
|
zone: us-central1-a
|
||||||
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 instance group
|
- name: create a instance group
|
||||||
gcp_compute_instance_group:
|
gcp_compute_instance_group:
|
||||||
name: "{{ resource_name }}"
|
name: "{{ resource_name }}"
|
||||||
named_ports:
|
named_ports:
|
||||||
- name: ansible
|
- name: ansible
|
||||||
port: 1234
|
port: 1234
|
||||||
network: "{{ network }}"
|
network: "{{ network }}"
|
||||||
zone: 'us-central1-a'
|
zone: us-central1-a
|
||||||
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
|
||||||
|
@ -59,27 +53,32 @@
|
||||||
- result.changed == true
|
- result.changed == true
|
||||||
- "result.kind == 'compute#instanceGroup'"
|
- "result.kind == 'compute#instanceGroup'"
|
||||||
- name: verify that instance_group was created
|
- name: verify that instance_group was created
|
||||||
shell: |
|
gcp_compute_instance_group_facts:
|
||||||
gcloud compute instance-groups unmanaged describe --project="{{ gcp_project }}" --zone="us-central1-a" "{{ resource_name }}"
|
filters:
|
||||||
register: results
|
- name = {{ resource_name }}
|
||||||
- name: verify that command succeeded
|
zone: us-central1-a
|
||||||
assert:
|
|
||||||
that:
|
|
||||||
- results.rc == 0
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
- name: create a instance group that already exists
|
|
||||||
gcp_compute_instance_group:
|
|
||||||
name: "{{ resource_name }}"
|
|
||||||
named_ports:
|
|
||||||
- name: ansible
|
|
||||||
port: 1234
|
|
||||||
network: "{{ network }}"
|
|
||||||
zone: 'us-central1-a'
|
|
||||||
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 instance group that already exists
|
||||||
|
gcp_compute_instance_group:
|
||||||
|
name: "{{ resource_name }}"
|
||||||
|
named_ports:
|
||||||
|
- name: ansible
|
||||||
|
port: 1234
|
||||||
|
network: "{{ network }}"
|
||||||
|
zone: us-central1-a
|
||||||
|
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
|
||||||
|
@ -92,15 +91,13 @@
|
||||||
gcp_compute_instance_group:
|
gcp_compute_instance_group:
|
||||||
name: "{{ resource_name }}"
|
name: "{{ resource_name }}"
|
||||||
named_ports:
|
named_ports:
|
||||||
- name: ansible
|
- name: ansible
|
||||||
port: 1234
|
port: 1234
|
||||||
network: "{{ network }}"
|
network: "{{ network }}"
|
||||||
zone: 'us-central1-a'
|
zone: us-central1-a
|
||||||
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
|
||||||
|
@ -109,29 +106,32 @@
|
||||||
- result.changed == true
|
- result.changed == true
|
||||||
- result.has_key('kind') == False
|
- result.has_key('kind') == False
|
||||||
- name: verify that instance_group was deleted
|
- name: verify that instance_group was deleted
|
||||||
shell: |
|
gcp_compute_instance_group_facts:
|
||||||
gcloud compute instance-groups unmanaged describe --project="{{ gcp_project }}" --zone="us-central1-a" "{{ resource_name }}"
|
filters:
|
||||||
register: results
|
- name = {{ resource_name }}
|
||||||
failed_when: results.rc == 0
|
zone: us-central1-a
|
||||||
- name: verify that command succeeded
|
|
||||||
assert:
|
|
||||||
that:
|
|
||||||
- results.rc == 1
|
|
||||||
- "\"'projects/{{ gcp_project }}/zones/us-central1-a/instanceGroups/{{ resource_name }}' was not found\" in results.stderr"
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
- name: delete a instance group that does not exist
|
|
||||||
gcp_compute_instance_group:
|
|
||||||
name: "{{ resource_name }}"
|
|
||||||
named_ports:
|
|
||||||
- name: ansible
|
|
||||||
port: 1234
|
|
||||||
network: "{{ network }}"
|
|
||||||
zone: 'us-central1-a'
|
|
||||||
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 instance group that does not exist
|
||||||
|
gcp_compute_instance_group:
|
||||||
|
name: "{{ resource_name }}"
|
||||||
|
named_ports:
|
||||||
|
- name: ansible
|
||||||
|
port: 1234
|
||||||
|
network: "{{ network }}"
|
||||||
|
zone: us-central1-a
|
||||||
|
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
|
||||||
|
@ -143,11 +143,9 @@
|
||||||
# Post-test teardown
|
# Post-test teardown
|
||||||
- name: delete a network
|
- name: delete a network
|
||||||
gcp_compute_network:
|
gcp_compute_network:
|
||||||
name: 'network-instancegroup'
|
name: "network-instancegroup"
|
||||||
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: network
|
register: network
|
||||||
|
|
Loading…
Reference in a new issue