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

ec2_vpc_vpn: Add delay and wait_timeout parameter (#53940)

* ec2_vpc_vpn: Add delay and wait_timeout parameter

Fixes: #53481

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>

* Fix unit tests

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2019-03-18 21:35:51 +05:30 committed by Sloane Hertel
parent 231f3e138a
commit 3fa39ac818
2 changed files with 40 additions and 10 deletions

View file

@ -110,6 +110,20 @@ options:
description: description:
- Whether or not to delete VPN connections routes that are not specified in the task. - Whether or not to delete VPN connections routes that are not specified in the task.
type: bool type: bool
wait_timeout:
description:
- How long before wait gives up, in seconds.
default: 600
type: int
required: false
version_added: "2.8"
delay:
description:
- The time to wait before checking operation again. in seconds.
required: false
type: int
default: 15
version_added: "2.8"
""" """
EXAMPLES = """ EXAMPLES = """
@ -434,11 +448,10 @@ def find_connection_response(connections=None):
return connections['VpnConnections'][0] return connections['VpnConnections'][0]
def create_connection(connection, customer_gateway_id, static_only, vpn_gateway_id, connection_type, tunnel_options=None): def create_connection(connection, customer_gateway_id, static_only, vpn_gateway_id, connection_type, max_attempts, delay, tunnel_options=None):
""" Creates a VPN connection """ """ Creates a VPN connection """
options = {'StaticRoutesOnly': static_only} options = {'StaticRoutesOnly': static_only}
if tunnel_options and len(tunnel_options) <= 2: if tunnel_options and len(tunnel_options) <= 2:
t_opt = [] t_opt = []
for m in tunnel_options: for m in tunnel_options:
@ -458,7 +471,10 @@ def create_connection(connection, customer_gateway_id, static_only, vpn_gateway_
CustomerGatewayId=customer_gateway_id, CustomerGatewayId=customer_gateway_id,
VpnGatewayId=vpn_gateway_id, VpnGatewayId=vpn_gateway_id,
Options=options) Options=options)
connection.get_waiter('vpn_connection_available').wait(VpnConnectionIds=[vpn['VpnConnection']['VpnConnectionId']]) connection.get_waiter('vpn_connection_available').wait(
VpnConnectionIds=[vpn['VpnConnection']['VpnConnectionId']],
WaiterConfig={'Delay': delay, 'MaxAttempts': max_attempts}
)
except WaiterError as e: except WaiterError as e:
raise VPNConnectionException(msg="Failed to wait for VPN connection {0} to be available".format(vpn['VpnConnection']['VpnConnectionId']), raise VPNConnectionException(msg="Failed to wait for VPN connection {0} to be available".format(vpn['VpnConnection']['VpnConnectionId']),
exception=e) exception=e)
@ -469,11 +485,14 @@ def create_connection(connection, customer_gateway_id, static_only, vpn_gateway_
return vpn['VpnConnection'] return vpn['VpnConnection']
def delete_connection(connection, vpn_connection_id): def delete_connection(connection, vpn_connection_id, delay, max_attempts):
""" Deletes a VPN connection """ """ Deletes a VPN connection """
try: try:
connection.delete_vpn_connection(VpnConnectionId=vpn_connection_id) connection.delete_vpn_connection(VpnConnectionId=vpn_connection_id)
connection.get_waiter('vpn_connection_deleted').wait(VpnConnectionIds=[vpn_connection_id]) connection.get_waiter('vpn_connection_deleted').wait(
VpnConnectionIds=[vpn_connection_id],
WaiterConfig={'Delay': delay, 'MaxAttempts': max_attempts}
)
except WaiterError as e: except WaiterError as e:
raise VPNConnectionException(msg="Failed to wait for VPN connection {0} to be removed".format(vpn_connection_id), raise VPNConnectionException(msg="Failed to wait for VPN connection {0} to be removed".format(vpn_connection_id),
exception=e) exception=e)
@ -641,6 +660,8 @@ def ensure_present(connection, module_params, check_mode=False):
""" Creates and adds tags to a VPN connection. If the connection already exists update tags. """ """ Creates and adds tags to a VPN connection. If the connection already exists update tags. """
vpn_connection = find_connection(connection, module_params) vpn_connection = find_connection(connection, module_params)
changed = False changed = False
delay = module_params.get('delay')
max_attempts = module_params.get('wait_timeout') // delay
# No match but vpn_connection_id was specified. # No match but vpn_connection_id was specified.
if not vpn_connection and module_params.get('vpn_connection_id'): if not vpn_connection and module_params.get('vpn_connection_id'):
@ -665,7 +686,9 @@ def ensure_present(connection, module_params, check_mode=False):
static_only=module_params.get('static_only'), static_only=module_params.get('static_only'),
vpn_gateway_id=module_params.get('vpn_gateway_id'), vpn_gateway_id=module_params.get('vpn_gateway_id'),
connection_type=module_params.get('connection_type'), connection_type=module_params.get('connection_type'),
tunnel_options=module_params.get('tunnel_options')) tunnel_options=module_params.get('tunnel_options'),
max_attempts=max_attempts,
delay=delay)
changes = check_for_update(connection, module_params, vpn_connection['VpnConnectionId']) changes = check_for_update(connection, module_params, vpn_connection['VpnConnectionId'])
_ = make_changes(connection, vpn_connection['VpnConnectionId'], changes) _ = make_changes(connection, vpn_connection['VpnConnectionId'], changes)
@ -685,8 +708,11 @@ def ensure_absent(connection, module_params, check_mode=False):
if check_mode: if check_mode:
return get_check_mode_results(connection, module_params, vpn_connection['VpnConnectionId'] if vpn_connection else None) return get_check_mode_results(connection, module_params, vpn_connection['VpnConnectionId'] if vpn_connection else None)
delay = module_params.get('delay')
max_attempts = module_params.get('wait_timeout') // delay
if vpn_connection: if vpn_connection:
delete_connection(connection, vpn_connection['VpnConnectionId']) delete_connection(connection, vpn_connection['VpnConnectionId'], delay=delay, max_attempts=max_attempts)
changed = True changed = True
else: else:
changed = False changed = False
@ -708,6 +734,8 @@ def main():
purge_tags=dict(type='bool', default=False), purge_tags=dict(type='bool', default=False),
routes=dict(type='list', default=[]), routes=dict(type='list', default=[]),
purge_routes=dict(type='bool', default=False), purge_routes=dict(type='bool', default=False),
wait_timeout=dict(type='int', default=600),
delay=dict(type='int', default=15),
) )
module = AnsibleAWSModule(argument_spec=argument_spec, module = AnsibleAWSModule(argument_spec=argument_spec,
supports_check_mode=True) supports_check_mode=True)

View file

@ -96,7 +96,9 @@ def make_params(cgw, vgw, tags=None, filters=None, routes=None):
'purge_tags': True, 'purge_tags': True,
'tags': tags, 'tags': tags,
'filters': filters, 'filters': filters,
'routes': routes} 'routes': routes,
'delay': 15,
'wait_timeout': 600}
def make_conn(placeboify, module, connection): def make_conn(placeboify, module, connection):
@ -111,7 +113,7 @@ def make_conn(placeboify, module, connection):
def tear_down_conn(placeboify, connection, vpn_connection_id): def tear_down_conn(placeboify, connection, vpn_connection_id):
ec2_vpc_vpn.delete_connection(connection, vpn_connection_id) ec2_vpc_vpn.delete_connection(connection, vpn_connection_id, delay=15, max_attempts=40)
def test_find_connection_vpc_conn_id(placeboify, maybe_sleep): def test_find_connection_vpc_conn_id(placeboify, maybe_sleep):
@ -251,7 +253,7 @@ def test_delete_connection(placeboify, maybe_sleep):
def test_delete_nonexistent_connection(placeboify, maybe_sleep): def test_delete_nonexistent_connection(placeboify, maybe_sleep):
# create parameters and ensure any connection matching (None) is deleted # create parameters and ensure any connection matching (None) is deleted
params = {'filters': {'tags': {'ThisConnection': 'DoesntExist'}}} params = {'filters': {'tags': {'ThisConnection': 'DoesntExist'}}, 'delay': 15, 'wait_timeout': 600}
m, conn = setup_mod_conn(placeboify, params) m, conn = setup_mod_conn(placeboify, params)
changed, vpn = ec2_vpc_vpn.ensure_absent(conn, m.params) changed, vpn = ec2_vpc_vpn.ensure_absent(conn, m.params)