|
|
|
@ -119,15 +119,21 @@ result:
|
|
|
|
|
type: dictionary
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import json
|
|
|
|
|
import time
|
|
|
|
|
import botocore
|
|
|
|
|
import boto3
|
|
|
|
|
HAS_BOTO3 = True
|
|
|
|
|
except ImportError:
|
|
|
|
|
HAS_BOTO3 = False
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
from ansible.module_utils.ec2 import HAS_BOTO3, boto3_conn, ec2_argument_spec, get_aws_connection_info
|
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_vgw_info(vgws):
|
|
|
|
|
if not isinstance(vgws, list):
|
|
|
|
|
return
|
|
|
|
@ -162,9 +168,8 @@ def wait_for_status(client, module, vpn_gateway_id, status):
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
time.sleep(polling_increment_secs)
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
result = response
|
|
|
|
|
return status_achieved, result
|
|
|
|
@ -176,9 +181,8 @@ def attach_vgw(client, module, vpn_gateway_id):
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
response = client.attach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=params['VpcId'])
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
status_achieved, vgw = wait_for_status(client, module, [vpn_gateway_id], 'attached')
|
|
|
|
|
if not status_achieved:
|
|
|
|
@ -195,15 +199,13 @@ def detach_vgw(client, module, vpn_gateway_id, vpc_id=None):
|
|
|
|
|
if vpc_id:
|
|
|
|
|
try:
|
|
|
|
|
response = client.detach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=vpc_id)
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
response = client.detach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=params['VpcId'])
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
status_achieved, vgw = wait_for_status(client, module, [vpn_gateway_id], 'detached')
|
|
|
|
|
if not status_achieved:
|
|
|
|
@ -219,9 +221,8 @@ def create_vgw(client, module):
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
response = client.create_vpn_gateway(Type=params['Type'])
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
result = response
|
|
|
|
|
return result
|
|
|
|
@ -231,9 +232,8 @@ def delete_vgw(client, module, vpn_gateway_id):
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
response = client.delete_vpn_gateway(VpnGatewayId=vpn_gateway_id)
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
#return the deleted VpnGatewayId as this is not included in the above response
|
|
|
|
|
result = vpn_gateway_id
|
|
|
|
@ -245,9 +245,8 @@ def create_tags(client, module, vpn_gateway_id):
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
response = client.create_tags(Resources=[vpn_gateway_id],Tags=load_tags(module))
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
result = response
|
|
|
|
|
return result
|
|
|
|
@ -259,15 +258,13 @@ def delete_tags(client, module, vpn_gateway_id, tags_to_delete=None):
|
|
|
|
|
if tags_to_delete:
|
|
|
|
|
try:
|
|
|
|
|
response = client.delete_tags(Resources=[vpn_gateway_id], Tags=tags_to_delete)
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
response = client.delete_tags(Resources=[vpn_gateway_id])
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
result = response
|
|
|
|
|
return result
|
|
|
|
@ -292,9 +289,8 @@ def find_tags(client, module, resource_id=None):
|
|
|
|
|
response = client.describe_tags(Filters=[
|
|
|
|
|
{'Name': 'resource-id', 'Values': [resource_id]}
|
|
|
|
|
])
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
result = response
|
|
|
|
|
return result
|
|
|
|
@ -340,9 +336,8 @@ def find_vpc(client, module):
|
|
|
|
|
if params['vpc_id']:
|
|
|
|
|
try:
|
|
|
|
|
response = client.describe_vpcs(VpcIds=[params['vpc_id']])
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
result = response
|
|
|
|
|
return result
|
|
|
|
@ -360,17 +355,15 @@ def find_vgw(client, module, vpn_gateway_id=None):
|
|
|
|
|
{'Name': 'type', 'Values': [params['Type']]},
|
|
|
|
|
{'Name': 'tag:Name', 'Values': [params['Name']]}
|
|
|
|
|
])
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
if vpn_gateway_id:
|
|
|
|
|
try:
|
|
|
|
|
response = client.describe_vpn_gateways(VpnGatewayIds=vpn_gateway_id)
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
@ -378,9 +371,8 @@ def find_vgw(client, module, vpn_gateway_id=None):
|
|
|
|
|
{'Name': 'type', 'Values': [params['Type']]},
|
|
|
|
|
{'Name': 'tag:Name', 'Values': [params['Name']]}
|
|
|
|
|
])
|
|
|
|
|
except botocore.exceptions.ClientError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
result = response['VpnGateways']
|
|
|
|
|
return result
|
|
|
|
@ -583,9 +575,8 @@ def main():
|
|
|
|
|
try:
|
|
|
|
|
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
|
|
|
|
|
client = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_kwargs)
|
|
|
|
|
except botocore.exceptions.NoCredentialsError:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
module.fail_json(msg="Can't authorize connection - "+str(e))
|
|
|
|
|
except botocore.exceptions.NoCredentialsError as e:
|
|
|
|
|
module.fail_json(msg="Can't authorize connection - %s" % to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
|
(changed, results) = ensure_vgw_present(client, module)
|
|
|
|
@ -594,10 +585,5 @@ def main():
|
|
|
|
|
module.exit_json(changed=changed, vgw=results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
from ansible.module_utils.ec2 import *
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|
|
|
|
|
|
|
|
|
|