Fix VPN gateway (#4279)
This commit is contained in:
parent
e061d371c9
commit
99c661781e
@ -124,11 +124,19 @@ class InvalidNetworkAclIdError(EC2ClientError):
|
|||||||
|
|
||||||
|
|
||||||
class InvalidVpnGatewayIdError(EC2ClientError):
|
class InvalidVpnGatewayIdError(EC2ClientError):
|
||||||
def __init__(self, network_acl_id):
|
def __init__(self, vpn_gw):
|
||||||
super(InvalidVpnGatewayIdError, self).__init__(
|
super(InvalidVpnGatewayIdError, self).__init__(
|
||||||
"InvalidVpnGatewayID.NotFound",
|
"InvalidVpnGatewayID.NotFound",
|
||||||
"The virtual private gateway ID '{0}' does not exist".format(
|
"The virtual private gateway ID '{0}' does not exist".format(vpn_gw),
|
||||||
network_acl_id
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidVpnGatewayAttachmentError(EC2ClientError):
|
||||||
|
def __init__(self, vpn_gw, vpc_id):
|
||||||
|
super(InvalidVpnGatewayAttachmentError, self).__init__(
|
||||||
|
"InvalidVpnGatewayAttachment.NotFound",
|
||||||
|
"The attachment with vpn gateway ID '{}' and vpc ID '{}' does not exist".format(
|
||||||
|
vpn_gw, vpc_id
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -95,6 +95,7 @@ from .exceptions import (
|
|||||||
InvalidVPCIdError,
|
InvalidVPCIdError,
|
||||||
InvalidVPCRangeError,
|
InvalidVPCRangeError,
|
||||||
InvalidVpnGatewayIdError,
|
InvalidVpnGatewayIdError,
|
||||||
|
InvalidVpnGatewayAttachmentError,
|
||||||
InvalidVpnConnectionIdError,
|
InvalidVpnConnectionIdError,
|
||||||
InvalidSubnetCidrBlockAssociationID,
|
InvalidSubnetCidrBlockAssociationID,
|
||||||
MalformedAMIIdError,
|
MalformedAMIIdError,
|
||||||
@ -3576,6 +3577,18 @@ class VPCBackend(object):
|
|||||||
return matches
|
return matches
|
||||||
|
|
||||||
def delete_vpc(self, vpc_id):
|
def delete_vpc(self, vpc_id):
|
||||||
|
# Do not delete if any VPN Gateway is attached
|
||||||
|
vpn_gateways = self.describe_vpn_gateways(filters={"attachment.vpc-id": vpc_id})
|
||||||
|
vpn_gateways = [
|
||||||
|
item
|
||||||
|
for item in vpn_gateways
|
||||||
|
if item.attachments.get(vpc_id).state == "attached"
|
||||||
|
]
|
||||||
|
if vpn_gateways:
|
||||||
|
raise DependencyViolationError(
|
||||||
|
"The vpc {0} has dependencies and cannot be deleted.".format(vpc_id)
|
||||||
|
)
|
||||||
|
|
||||||
# Delete route table if only main route table remains.
|
# Delete route table if only main route table remains.
|
||||||
route_tables = self.describe_route_tables(filters={"vpc-id": vpc_id})
|
route_tables = self.describe_route_tables(filters={"vpc-id": vpc_id})
|
||||||
if len(route_tables) > 1:
|
if len(route_tables) > 1:
|
||||||
@ -6676,8 +6689,10 @@ class VpnGatewayBackend(object):
|
|||||||
self.vpn_gateways[vpn_gateway_id] = vpn_gateway
|
self.vpn_gateways[vpn_gateway_id] = vpn_gateway
|
||||||
return vpn_gateway
|
return vpn_gateway
|
||||||
|
|
||||||
def describe_vpn_gateways(self, filters=None):
|
def describe_vpn_gateways(self, filters=None, vpn_gw_ids=None):
|
||||||
vpn_gateways = self.vpn_gateways.values()
|
vpn_gateways = list(self.vpn_gateways.values() or [])
|
||||||
|
if vpn_gw_ids:
|
||||||
|
vpn_gateways = [item for item in vpn_gateways if item.id in vpn_gw_ids]
|
||||||
return generic_filter(filters, vpn_gateways)
|
return generic_filter(filters, vpn_gateways)
|
||||||
|
|
||||||
def get_vpn_gateway(self, vpn_gateway_id):
|
def get_vpn_gateway(self, vpn_gateway_id):
|
||||||
@ -6690,21 +6705,25 @@ class VpnGatewayBackend(object):
|
|||||||
vpn_gateway = self.get_vpn_gateway(vpn_gateway_id)
|
vpn_gateway = self.get_vpn_gateway(vpn_gateway_id)
|
||||||
self.get_vpc(vpc_id)
|
self.get_vpc(vpc_id)
|
||||||
attachment = VpnGatewayAttachment(vpc_id, state="attached")
|
attachment = VpnGatewayAttachment(vpc_id, state="attached")
|
||||||
|
for key in vpn_gateway.attachments.copy():
|
||||||
|
if key.startswith("vpc-"):
|
||||||
|
vpn_gateway.attachments.pop(key)
|
||||||
vpn_gateway.attachments[vpc_id] = attachment
|
vpn_gateway.attachments[vpc_id] = attachment
|
||||||
return attachment
|
return attachment
|
||||||
|
|
||||||
def delete_vpn_gateway(self, vpn_gateway_id):
|
def delete_vpn_gateway(self, vpn_gateway_id):
|
||||||
deleted = self.vpn_gateways.pop(vpn_gateway_id, None)
|
deleted = self.vpn_gateways.get(vpn_gateway_id, None)
|
||||||
if not deleted:
|
if not deleted:
|
||||||
raise InvalidVpnGatewayIdError(vpn_gateway_id)
|
raise InvalidVpnGatewayIdError(vpn_gateway_id)
|
||||||
|
deleted.state = "deleted"
|
||||||
return deleted
|
return deleted
|
||||||
|
|
||||||
def detach_vpn_gateway(self, vpn_gateway_id, vpc_id):
|
def detach_vpn_gateway(self, vpn_gateway_id, vpc_id):
|
||||||
vpn_gateway = self.get_vpn_gateway(vpn_gateway_id)
|
vpn_gateway = self.get_vpn_gateway(vpn_gateway_id)
|
||||||
self.get_vpc(vpc_id)
|
|
||||||
detached = vpn_gateway.attachments.get(vpc_id, None)
|
detached = vpn_gateway.attachments.get(vpc_id, None)
|
||||||
if not detached:
|
if not detached:
|
||||||
raise InvalidVPCIdError(vpc_id)
|
|
||||||
|
raise InvalidVpnGatewayAttachmentError(vpn_gateway.id, vpc_id)
|
||||||
detached.state = "detached"
|
detached.state = "detached"
|
||||||
return detached
|
return detached
|
||||||
|
|
||||||
|
@ -36,7 +36,8 @@ class VirtualPrivateGateways(BaseResponse):
|
|||||||
|
|
||||||
def describe_vpn_gateways(self):
|
def describe_vpn_gateways(self):
|
||||||
filters = filters_from_querystring(self.querystring)
|
filters = filters_from_querystring(self.querystring)
|
||||||
vpn_gateways = self.ec2_backend.describe_vpn_gateways(filters)
|
vpn_gw_ids = self._get_multi_param("VpnGatewayId")
|
||||||
|
vpn_gateways = self.ec2_backend.describe_vpn_gateways(filters, vpn_gw_ids)
|
||||||
template = self.response_template(DESCRIBE_VPN_GATEWAYS_RESPONSE)
|
template = self.response_template(DESCRIBE_VPN_GATEWAYS_RESPONSE)
|
||||||
return template.render(vpn_gateways=vpn_gateways)
|
return template.render(vpn_gateways=vpn_gateways)
|
||||||
|
|
||||||
@ -53,6 +54,9 @@ CREATE_VPN_GATEWAY_RESPONSE = """
|
|||||||
<requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>
|
<requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>
|
||||||
<vpnGateway>
|
<vpnGateway>
|
||||||
<vpnGatewayId>{{ vpn_gateway.id }}</vpnGatewayId>
|
<vpnGatewayId>{{ vpn_gateway.id }}</vpnGatewayId>
|
||||||
|
{% if vpn_gateway.amazon_side_asn %}
|
||||||
|
<amazonSideAsn>{{ vpn_gateway.amazon_side_asn }}</amazonSideAsn>
|
||||||
|
{% endif %}
|
||||||
<state>{{ vpn_gateway.state }}</state>
|
<state>{{ vpn_gateway.state }}</state>
|
||||||
<type>{{ vpn_gateway.type }}</type>
|
<type>{{ vpn_gateway.type }}</type>
|
||||||
<availabilityZone>{{ vpn_gateway.availability_zone }}</availabilityZone>
|
<availabilityZone>{{ vpn_gateway.availability_zone }}</availabilityZone>
|
||||||
@ -75,6 +79,9 @@ DESCRIBE_VPN_GATEWAYS_RESPONSE = """
|
|||||||
{% for vpn_gateway in vpn_gateways %}
|
{% for vpn_gateway in vpn_gateways %}
|
||||||
<item>
|
<item>
|
||||||
<vpnGatewayId>{{ vpn_gateway.id }}</vpnGatewayId>
|
<vpnGatewayId>{{ vpn_gateway.id }}</vpnGatewayId>
|
||||||
|
{% if vpn_gateway.amazon_side_asn %}
|
||||||
|
<amazonSideAsn>{{ vpn_gateway.amazon_side_asn }}</amazonSideAsn>
|
||||||
|
{% endif %}
|
||||||
<state>{{ vpn_gateway.state }}</state>
|
<state>{{ vpn_gateway.state }}</state>
|
||||||
<type>{{ vpn_gateway.id }}</type>
|
<type>{{ vpn_gateway.id }}</type>
|
||||||
<availabilityZone>{{ vpn_gateway.availability_zone }}</availabilityZone>
|
<availabilityZone>{{ vpn_gateway.availability_zone }}</availabilityZone>
|
||||||
|
@ -522,6 +522,10 @@ def is_filter_matching(obj, filter, filter_value):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if isinstance(value, type({}.keys())):
|
||||||
|
if isinstance(filter_value, str) and filter_value in value:
|
||||||
|
return True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
value = set(value)
|
value = set(value)
|
||||||
return (value and value.issubset(filter_value)) or value.issuperset(
|
return (value and value.issubset(filter_value)) or value.issuperset(
|
||||||
|
@ -120,7 +120,6 @@ TestAccAwsEc2ManagedPrefixList
|
|||||||
TestAccAWSEgressOnlyInternetGateway
|
TestAccAWSEgressOnlyInternetGateway
|
||||||
TestAccAWSSecurityGroup_
|
TestAccAWSSecurityGroup_
|
||||||
TestAccAWSInternetGateway
|
TestAccAWSInternetGateway
|
||||||
TestAccAWSVpnGateway_basic
|
|
||||||
TestAccAWSVpnGateway_delete
|
|
||||||
TestAccAWSVpnGateway_tags
|
|
||||||
TestAccAWSSecurityGroupRule_
|
TestAccAWSSecurityGroupRule_
|
||||||
|
TestAccAWSVpnGateway
|
||||||
|
TestAccAWSVpnGatewayAttachment
|
||||||
|
@ -233,7 +233,8 @@ def test_delete_vpn_gateway():
|
|||||||
|
|
||||||
conn.delete_vpn_gateway(vpn_gateway.id)
|
conn.delete_vpn_gateway(vpn_gateway.id)
|
||||||
vgws = conn.get_all_vpn_gateways()
|
vgws = conn.get_all_vpn_gateways()
|
||||||
vgws.should.have.length_of(0)
|
vgws.should.have.length_of(1)
|
||||||
|
vgws[0].state.should.equal("deleted")
|
||||||
|
|
||||||
|
|
||||||
@mock_ec2_deprecated
|
@mock_ec2_deprecated
|
||||||
|
Loading…
Reference in New Issue
Block a user