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_subnetwork (#42822)

This commit is contained in:
Alex Stephen 2018-08-15 11:28:36 -07:00 committed by Ryan Brown
parent 97b3845e89
commit f5f0d16516
2 changed files with 85 additions and 71 deletions

View file

@ -67,18 +67,13 @@ options:
- An optional description of this resource. Provide this property when you create - An optional description of this resource. Provide this property when you create
the resource. This field can be set only at resource creation time. the resource. This field can be set only at resource creation time.
required: false required: false
gateway_address:
description:
- The gateway address for default routes to reach destination addresses outside this
subnetwork. This field can be set only at resource creation time.
required: false
ip_cidr_range: ip_cidr_range:
description: description:
- The range of internal addresses that are owned by this subnetwork. - The range of internal addresses that are owned by this subnetwork.
- Provide this property when you create the subnetwork. For example, 10.0.0.0/8 or - Provide this property when you create the subnetwork. For example, 10.0.0.0/8 or
192.168.0.0/16. Ranges must be unique and non-overlapping within a network. Only 192.168.0.0/16. Ranges must be unique and non-overlapping within a network. Only
IPv4 is supported. IPv4 is supported.
required: false required: true
name: name:
description: description:
- The name of the resource, provided by the client when initially creating the resource. - The name of the resource, provided by the client when initially creating the resource.
@ -87,11 +82,12 @@ options:
which means the first character must be a lowercase letter, and all following characters 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 must be a dash, lowercase letter, or digit, except the last character, which cannot
be a dash. be a dash.
required: false required: true
network: network:
description: description:
- A reference to Network resource. - The network this subnet belongs to.
required: false - Only networks that are in the distributed mode can have subnetworks.
required: true
private_ip_google_access: private_ip_google_access:
description: description:
- Whether the VMs in this subnet can access Google services without assigned external - Whether the VMs in this subnet can access Google services without assigned external
@ -100,35 +96,35 @@ options:
type: bool type: bool
region: region:
description: description:
- A reference to Region resource. - URL of the GCP region for this subnetwork.
required: true required: true
extends_documentation_fragment: gcp extends_documentation_fragment: gcp
notes:
- "API Reference: U(https://cloud.google.com/compute/docs/reference/rest/beta/subnetworks)"
- "Private Google Access: U(https://cloud.google.com/vpc/docs/configure-private-google-access)"
- "Cloud Networking: U(https://cloud.google.com/vpc/docs/using-vpc)"
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: create a network - name: create a network
gcp_compute_network: gcp_compute_network:
name: 'network-subnetwork' name: "network-subnetwork"
auto_create_subnetworks: true auto_create_subnetworks: true
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 subnetwork - name: create a subnetwork
gcp_compute_subnetwork: gcp_compute_subnetwork:
name: 'ansiblenet' name: ansiblenet
region: 'us-west1' region: us-west1
network: "{{ network }}" network: "{{ network }}"
ip_cidr_range: '172.16.0.0/16' ip_cidr_range: 172.16.0.0/16
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
''' '''
@ -147,7 +143,7 @@ RETURN = '''
gateway_address: gateway_address:
description: description:
- The gateway address for default routes to reach destination addresses outside this - The gateway address for default routes to reach destination addresses outside this
subnetwork. This field can be set only at resource creation time. subnetwork.
returned: success returned: success
type: str type: str
id: id:
@ -175,7 +171,8 @@ RETURN = '''
type: str type: str
network: network:
description: description:
- A reference to Network resource. - The network this subnet belongs to.
- Only networks that are in the distributed mode can have subnetworks.
returned: success returned: success
type: dict type: dict
private_ip_google_access: private_ip_google_access:
@ -186,7 +183,7 @@ RETURN = '''
type: bool type: bool
region: region:
description: description:
- A reference to Region resource. - URL of the GCP region for this subnetwork.
returned: success returned: success
type: str type: str
''' '''
@ -211,15 +208,17 @@ def main():
argument_spec=dict( argument_spec=dict(
state=dict(default='present', choices=['present', 'absent'], type='str'), state=dict(default='present', choices=['present', 'absent'], type='str'),
description=dict(type='str'), description=dict(type='str'),
gateway_address=dict(type='str'), ip_cidr_range=dict(required=True, type='str'),
ip_cidr_range=dict(type='str'), name=dict(required=True, type='str'),
name=dict(type='str'), network=dict(required=True, type='dict'),
network=dict(type='dict'),
private_ip_google_access=dict(type='bool'), private_ip_google_access=dict(type='bool'),
region=dict(required=True, type='str') region=dict(required=True, type='str')
) )
) )
if not module.params['scopes']:
module.params['scopes'] = ['https://www.googleapis.com/auth/compute']
state = module.params['state'] state = module.params['state']
kind = 'compute#subnetwork' kind = 'compute#subnetwork'
@ -229,10 +228,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 +251,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))
@ -266,7 +265,6 @@ def resource_to_request(module):
request = { request = {
u'kind': 'compute#subnetwork', u'kind': 'compute#subnetwork',
u'description': module.params.get('description'), u'description': module.params.get('description'),
u'gatewayAddress': module.params.get('gateway_address'),
u'ipCidrRange': module.params.get('ip_cidr_range'), u'ipCidrRange': module.params.get('ip_cidr_range'),
u'name': module.params.get('name'), u'name': module.params.get('name'),
u'network': replace_resource_dict(module.params.get(u'network', {}), 'selfLink'), u'network': replace_resource_dict(module.params.get(u'network', {}), 'selfLink'),
@ -341,9 +339,9 @@ def response_to_hash(module, response):
return { return {
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'gatewayAddress': module.params.get('gateway_address'), u'gatewayAddress': response.get(u'gatewayAddress'),
u'id': response.get(u'id'), u'id': response.get(u'id'),
u'ipCidrRange': module.params.get('ip_cidr_range'), u'ipCidrRange': response.get(u'ipCidrRange'),
u'name': response.get(u'name'), u'name': response.get(u'name'),
u'network': replace_resource_dict(module.params.get(u'network', {}), 'selfLink'), u'network': replace_resource_dict(module.params.get(u'network', {}), 'selfLink'),
u'privateIpGoogleAccess': response.get(u'privateIpGoogleAccess'), u'privateIpGoogleAccess': response.get(u'privateIpGoogleAccess'),
@ -363,7 +361,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#subnetwork') return fetch_resource(module, navigate_hash(wait_done, ['targetLink']), 'compute#subnetwork')

View file

@ -15,39 +15,33 @@
# Pre-test setup # Pre-test setup
- name: create a network - name: create a network
gcp_compute_network: gcp_compute_network:
name: 'network-subnetwork' name: "network-subnetwork"
auto_create_subnetworks: true auto_create_subnetworks: true
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 subnetwork - name: delete a subnetwork
gcp_compute_subnetwork: gcp_compute_subnetwork:
name: 'ansiblenet' name: ansiblenet
region: 'us-west1' region: us-west1
network: "{{ network }}" network: "{{ network }}"
ip_cidr_range: '172.16.0.0/16' ip_cidr_range: 172.16.0.0/16
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 subnetwork - name: create a subnetwork
gcp_compute_subnetwork: gcp_compute_subnetwork:
name: 'ansiblenet' name: ansiblenet
region: 'us-west1' region: us-west1
network: "{{ network }}" network: "{{ network }}"
ip_cidr_range: '172.16.0.0/16' ip_cidr_range: 172.16.0.0/16
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
@ -55,18 +49,31 @@
that: that:
- result.changed == true - result.changed == true
- "result.kind == 'compute#subnetwork'" - "result.kind == 'compute#subnetwork'"
# ---------------------------------------------------------------------------- - name: verify that subnetwork was created
- name: create a subnetwork that already exists gcp_compute_subnetwork_facts:
gcp_compute_subnetwork: filters:
name: 'ansiblenet' - name = ansiblenet
region: 'us-west1' region: us-west1
network: "{{ network }}"
ip_cidr_range: '172.16.0.0/16'
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 subnetwork that already exists
gcp_compute_subnetwork:
name: ansiblenet
region: us-west1
network: "{{ network }}"
ip_cidr_range: 172.16.0.0/16
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
@ -77,15 +84,13 @@
#---------------------------------------------------------- #----------------------------------------------------------
- name: delete a subnetwork - name: delete a subnetwork
gcp_compute_subnetwork: gcp_compute_subnetwork:
name: 'ansiblenet' name: ansiblenet
region: 'us-west1' region: us-west1
network: "{{ network }}" network: "{{ network }}"
ip_cidr_range: '172.16.0.0/16' ip_cidr_range: 172.16.0.0/16
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
@ -93,18 +98,31 @@
that: that:
- result.changed == true - result.changed == true
- result.has_key('kind') == False - result.has_key('kind') == False
# ---------------------------------------------------------------------------- - name: verify that subnetwork was deleted
- name: delete a subnetwork that does not exist gcp_compute_subnetwork_facts:
gcp_compute_subnetwork: filters:
name: 'ansiblenet' - name = ansiblenet
region: 'us-west1' region: us-west1
network: "{{ network }}"
ip_cidr_range: '172.16.0.0/16'
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 subnetwork that does not exist
gcp_compute_subnetwork:
name: ansiblenet
region: us-west1
network: "{{ network }}"
ip_cidr_range: 172.16.0.0/16
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
@ -116,12 +134,10 @@
# Post-test teardown # Post-test teardown
- name: delete a network - name: delete a network
gcp_compute_network: gcp_compute_network:
name: 'network-subnetwork' name: "network-subnetwork"
auto_create_subnetworks: true auto_create_subnetworks: true
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