From 63e87a3a0a2af57523417a5bec49a541768b1cac Mon Sep 17 00:00:00 2001 From: Alex Stephen Date: Wed, 15 Aug 2018 11:28:49 -0700 Subject: [PATCH] Bug fixes for gcp_compute_route (#42820) --- .../modules/cloud/google/gcp_compute_route.py | 87 +++++++++++-------- .../targets/gcp_compute_route/tasks/main.yml | 84 +++++++++--------- 2 files changed, 93 insertions(+), 78 deletions(-) diff --git a/lib/ansible/modules/cloud/google/gcp_compute_route.py b/lib/ansible/modules/cloud/google/gcp_compute_route.py index 15559356573..1a61e098b6b 100644 --- a/lib/ansible/modules/cloud/google/gcp_compute_route.py +++ b/lib/ansible/modules/cloud/google/gcp_compute_route.py @@ -46,8 +46,8 @@ description: to another virtual machine destination, a virtual machine gateway or a Compute Engine-operated gateway. Packets that do not match any route in the sending virtual machine's routing table will be dropped. - - A Routes resources must have exactly one specification of either nextHopGateway, - nextHopInstance, nextHopIp, or nextHopVpnTunnel. + - A Route resource must have exactly one specification of either nextHopGateway, nextHopInstance, + nextHopIp, or nextHopVpnTunnel. short_description: Creates a GCP Route version_added: 2.6 author: Google Inc. (@googlecloudplatform) @@ -65,7 +65,13 @@ options: description: - The destination range of outgoing packets that this route applies to. - Only IPv4 is supported. + required: true + description: + description: + - An optional description of this resource. Provide this property when you create + the resource. required: false + version_added: 2.7 name: description: - Name of the resource. Provided by the client when the resource is created. The name @@ -74,11 +80,11 @@ options: 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 be a dash. - required: false + required: true network: description: - - A reference to Network resource. - required: false + - The network that this route applies to. + required: true priority: description: - The priority of this route. Priority is used to break ties in cases where there @@ -102,11 +108,9 @@ options: next_hop_instance: description: - URL to an instance that should handle matching packets. - - 'You can specify this as a full or partial URL. For example: * - U(https://www.googleapis.com/compute/v1/projects/project/zones/zone/) - instances/instance * - projects/project/zones/zone/instances/instance * - zones/zone/instances/instance .' + - 'You can specify this as a full or partial URL. For example: * U(https://www.googleapis.com/compute/v1/projects/project/zones/zone/) + instances/instance * projects/project/zones/zone/instances/instance * zones/zone/instances/instance + .' required: false next_hop_ip: description: @@ -117,34 +121,33 @@ options: - URL to a VpnTunnel that should handle matching packets. required: false extends_documentation_fragment: gcp +notes: + - "API Reference: U(https://cloud.google.com/compute/docs/reference/rest/v1/routes)" + - "Using Routes: U(https://cloud.google.com/vpc/docs/using-routes)" ''' EXAMPLES = ''' - name: create a network gcp_compute_network: - name: 'network-route' + name: "network-route" project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: present register: network - name: create a route gcp_compute_route: - name: testObject - dest_range: '192.168.6.0/24' - next_hop_gateway: 'global/gateways/default-internet-gateway' + name: "test_object" + dest_range: 192.168.6.0/24 + next_hop_gateway: global/gateways/default-internet-gateway network: "{{ network }}" tags: - - backends - - databases - project: testProject - auth_kind: service_account - service_account_file: /tmp/auth.pem - scopes: - - https://www.googleapis.com/auth/compute + - backends + - databases + project: "test_project" + auth_kind: "service_account" + service_account_file: "/tmp/auth.pem" state: present ''' @@ -155,6 +158,12 @@ RETURN = ''' - Only IPv4 is supported. returned: success type: str + description: + description: + - An optional description of this resource. Provide this property when you create + the resource. + returned: success + type: str name: description: - Name of the resource. Provided by the client when the resource is created. The name @@ -167,7 +176,7 @@ RETURN = ''' type: str network: description: - - A reference to Network resource. + - The network that this route applies to. returned: success type: dict priority: @@ -196,11 +205,9 @@ RETURN = ''' next_hop_instance: description: - URL to an instance that should handle matching packets. - - 'You can specify this as a full or partial URL. For example: * - U(https://www.googleapis.com/compute/v1/projects/project/zones/zone/) - instances/instance * - projects/project/zones/zone/instances/instance * - zones/zone/instances/instance .' + - 'You can specify this as a full or partial URL. For example: * U(https://www.googleapis.com/compute/v1/projects/project/zones/zone/) + instances/instance * projects/project/zones/zone/instances/instance * zones/zone/instances/instance + .' returned: success type: str next_hop_ip: @@ -213,6 +220,11 @@ RETURN = ''' - URL to a VpnTunnel that should handle matching packets. returned: success type: str + next_hop_network: + description: + - URL to a Network that should handle matching packets. + returned: success + type: str ''' ################################################################################ @@ -234,9 +246,10 @@ def main(): module = GcpModule( argument_spec=dict( state=dict(default='present', choices=['present', 'absent'], type='str'), - dest_range=dict(type='str'), - name=dict(type='str'), - network=dict(type='dict'), + dest_range=dict(required=True, type='str'), + description=dict(type='str'), + name=dict(required=True, type='str'), + network=dict(required=True, type='dict'), priority=dict(type='int'), tags=dict(type='list', elements='str'), next_hop_gateway=dict(type='str'), @@ -246,6 +259,9 @@ def main(): ) ) + if not module.params['scopes']: + module.params['scopes'] = ['https://www.googleapis.com/auth/compute'] + state = module.params['state'] kind = 'compute#route' @@ -292,6 +308,7 @@ def resource_to_request(module): request = { u'kind': 'compute#route', u'destRange': module.params.get('dest_range'), + u'description': module.params.get('description'), u'name': module.params.get('name'), u'network': replace_resource_dict(module.params.get(u'network', {}), 'selfLink'), u'priority': module.params.get('priority'), @@ -368,6 +385,7 @@ def is_different(module, response): def response_to_hash(module, response): return { u'destRange': response.get(u'destRange'), + u'description': response.get(u'description'), u'name': response.get(u'name'), u'network': replace_resource_dict(module.params.get(u'network', {}), 'selfLink'), u'priority': module.params.get('priority'), @@ -375,7 +393,8 @@ def response_to_hash(module, response): u'nextHopGateway': module.params.get('next_hop_gateway'), u'nextHopInstance': module.params.get('next_hop_instance'), u'nextHopIp': module.params.get('next_hop_ip'), - u'nextHopVpnTunnel': module.params.get('next_hop_vpn_tunnel') + u'nextHopVpnTunnel': module.params.get('next_hop_vpn_tunnel'), + u'nextHopNetwork': response.get(u'nextHopNetwork') } @@ -391,7 +410,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#route') diff --git a/test/integration/targets/gcp_compute_route/tasks/main.yml b/test/integration/targets/gcp_compute_route/tasks/main.yml index 6852d1cc185..2004ecc7b47 100644 --- a/test/integration/targets/gcp_compute_route/tasks/main.yml +++ b/test/integration/targets/gcp_compute_route/tasks/main.yml @@ -15,44 +15,38 @@ # Pre-test setup - name: create a network gcp_compute_network: - name: 'network-route' + name: "network-route" project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: present register: network - name: delete a route gcp_compute_route: name: "{{ resource_name }}" - dest_range: '192.168.6.0/24' - next_hop_gateway: 'global/gateways/default-internet-gateway' + dest_range: 192.168.6.0/24 + next_hop_gateway: global/gateways/default-internet-gateway network: "{{ network }}" tags: - - backends - - databases + - backends + - databases 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 route gcp_compute_route: name: "{{ resource_name }}" - dest_range: '192.168.6.0/24' - next_hop_gateway: 'global/gateways/default-internet-gateway' + dest_range: 192.168.6.0/24 + next_hop_gateway: global/gateways/default-internet-gateway network: "{{ network }}" tags: - - backends - - databases + - backends + - databases 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 @@ -61,28 +55,32 @@ - result.changed == true - "result.kind == 'compute#route'" - name: verify that route was created - shell: | - gcloud compute routes describe --project="{{ gcp_project}}" "{{ resource_name }}" + gcp_compute_route_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 route that already exists gcp_compute_route: name: "{{ resource_name }}" - dest_range: '192.168.6.0/24' - next_hop_gateway: 'global/gateways/default-internet-gateway' + dest_range: 192.168.6.0/24 + next_hop_gateway: global/gateways/default-internet-gateway network: "{{ network }}" tags: - - backends - - databases + - backends + - databases 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 @@ -94,17 +92,15 @@ - name: delete a route gcp_compute_route: name: "{{ resource_name }}" - dest_range: '192.168.6.0/24' - next_hop_gateway: 'global/gateways/default-internet-gateway' + dest_range: 192.168.6.0/24 + next_hop_gateway: global/gateways/default-internet-gateway network: "{{ network }}" tags: - - backends - - databases + - backends + - databases 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 @@ -113,30 +109,32 @@ - result.changed == true - result.has_key('kind') == False - name: verify that route was deleted - shell: | - gcloud compute routes describe --project="{{ gcp_project}}" "{{ resource_name }}" + gcp_compute_route_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/routes/{{ resource_name }}' was not found\" in results.stderr" + - results['items'] | length == 0 # ---------------------------------------------------------------------------- - name: delete a route that does not exist gcp_compute_route: name: "{{ resource_name }}" - dest_range: '192.168.6.0/24' - next_hop_gateway: 'global/gateways/default-internet-gateway' + dest_range: 192.168.6.0/24 + next_hop_gateway: global/gateways/default-internet-gateway network: "{{ network }}" tags: - - backends - - databases + - backends + - databases 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 @@ -148,11 +146,9 @@ # Post-test teardown - name: delete a network gcp_compute_network: - name: 'network-route' + name: "network-route" project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: absent register: network