diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 543960da0..9c30cb119 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -2494,6 +2494,14 @@ class VpnGatewayBackend(object): raise InvalidVpnGatewayIdError(vpn_gateway_id) return deleted + def detach_vpn_gateway(self, vpn_gateway_id, vpc_id): + vpn_gateway = self.get_vpn_gateway(vpn_gateway_id) + self.get_vpc(vpc_id) + detached = vpn_gateway.attachments.pop(vpc_id, None) + if not detached: + raise InvalidVPCIdError(vpc_id) + return detached + class EC2Backend(BaseBackend, InstanceBackend, TagBackend, AmiBackend, RegionsAndZonesBackend, SecurityGroupBackend, EBSBackend, diff --git a/moto/ec2/responses/virtual_private_gateways.py b/moto/ec2/responses/virtual_private_gateways.py index 94533943e..50b5704ea 100644 --- a/moto/ec2/responses/virtual_private_gateways.py +++ b/moto/ec2/responses/virtual_private_gateways.py @@ -34,8 +34,14 @@ class VirtualPrivateGateways(BaseResponse): return template.render(vpn_gateways=vpn_gateways) def detach_vpn_gateway(self): - raise NotImplementedError('VirtualPrivateGateways(AmazonVPC).detach_vpn_gateway is not yet implemented') - + vpn_gateway_id = self.querystring.get('VpnGatewayId')[0] + vpc_id = self.querystring.get('VpcId')[0] + attachment = self.ec2_backend.detach_vpn_gateway( + vpn_gateway_id, + vpc_id + ) + template = Template(DETACH_VPN_GATEWAY_RESPONSE) + return template.render(attachment=attachment) CREATE_VPN_GATEWAY_RESPONSE = """ @@ -107,4 +113,11 @@ DELETE_VPN_GATEWAY_RESPONSE = """ 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE true +""" + +DETACH_VPN_GATEWAY_RESPONSE = """ + + 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE + true + """ \ No newline at end of file diff --git a/tests/test_ec2/test_virtual_private_gateways.py b/tests/test_ec2/test_virtual_private_gateways.py index 40a03038e..8050559f1 100644 --- a/tests/test_ec2/test_virtual_private_gateways.py +++ b/tests/test_ec2/test_virtual_private_gateways.py @@ -74,3 +74,31 @@ def test_vpn_gateway_tagging(): vpn_gateway = conn.get_all_vpn_gateways()[0] vpn_gateway.tags.should.have.length_of(1) vpn_gateway.tags["a key"].should.equal("some value") + + +@mock_ec2 +def test_detach_vpn_gateway(): + + conn = boto.connect_vpc('the_key', 'the_secret') + vpc = conn.create_vpc("10.0.0.0/16") + vpn_gateway = conn.create_vpn_gateway('ipsec.1', 'us-east-1a') + + conn.attach_vpn_gateway( + vpn_gateway_id=vpn_gateway.id, + vpc_id=vpc.id + ) + + gateway = conn.get_all_vpn_gateways()[0] + attachments = gateway.attachments + attachments.should.have.length_of(1) + attachments[0].vpc_id.should.equal(vpc.id) + attachments[0].state.should.equal('attached') + + conn.detach_vpn_gateway( + vpn_gateway_id=vpn_gateway.id, + vpc_id=vpc.id + ) + + gateway = conn.get_all_vpn_gateways()[0] + attachments = gateway.attachments + attachments.should.have.length_of(0)