| 
									
										
										
										
											2014-08-27 11:17:06 -04:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  | # Ensure 'pytest.raises' context manager support for Python 2.6 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2018-09-21 23:29:04 +08:00
										 |  |  | from moto.ec2.exceptions import EC2ClientError | 
					
						
							|  |  |  | from botocore.exceptions import ClientError | 
					
						
							| 
									
										
										
										
											2014-08-25 15:09:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 23:29:04 +08:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | import boto | 
					
						
							|  |  |  | from boto.exception import EC2ResponseError | 
					
						
							|  |  |  | import sure  # noqa | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 23:29:04 +08:00
										 |  |  | from moto import mock_ec2, mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-08-18 11:54:11 -07:00
										 |  |  | from tests.helpers import requires_boto_gte | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-18 11:54:11 -07:00
										 |  |  | @requires_boto_gte("2.32.0") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | def test_vpc_peering_connections(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     peer_vpc = conn.create_vpc("11.0.0.0/16") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     vpc_pcx = conn.create_vpc_peering_connection(vpc.id, peer_vpc.id) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc_pcx._status.code.should.equal("initiating-request") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return vpc_pcx | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-18 11:54:11 -07:00
										 |  |  | @requires_boto_gte("2.32.0") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | def test_vpc_peering_connections_get_all(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  |     vpc_pcx = test_vpc_peering_connections() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc_pcx._status.code.should.equal("initiating-request") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_vpc_pcxs = conn.get_all_vpc_peering_connections() | 
					
						
							|  |  |  |     all_vpc_pcxs.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     all_vpc_pcxs[0]._status.code.should.equal("pending-acceptance") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-18 11:54:11 -07:00
										 |  |  | @requires_boto_gte("2.32.0") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | def test_vpc_peering_connections_accept(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  |     vpc_pcx = test_vpc_peering_connections() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     vpc_pcx = conn.accept_vpc_peering_connection(vpc_pcx.id) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc_pcx._status.code.should.equal("active") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.reject_vpc_peering_connection(vpc_pcx.id) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidStateTransition") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_vpc_pcxs = conn.get_all_vpc_peering_connections() | 
					
						
							|  |  |  |     all_vpc_pcxs.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     all_vpc_pcxs[0]._status.code.should.equal("active") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-18 11:54:11 -07:00
										 |  |  | @requires_boto_gte("2.32.0") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | def test_vpc_peering_connections_reject(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  |     vpc_pcx = test_vpc_peering_connections() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     verdict = conn.reject_vpc_peering_connection(vpc_pcx.id) | 
					
						
							|  |  |  |     verdict.should.equal(True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.accept_vpc_peering_connection(vpc_pcx.id) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidStateTransition") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_vpc_pcxs = conn.get_all_vpc_peering_connections() | 
					
						
							|  |  |  |     all_vpc_pcxs.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     all_vpc_pcxs[0]._status.code.should.equal("rejected") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-18 11:54:11 -07:00
										 |  |  | @requires_boto_gte("2.32.1") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | def test_vpc_peering_connections_delete(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  |     vpc_pcx = test_vpc_peering_connections() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     verdict = vpc_pcx.delete() | 
					
						
							|  |  |  |     verdict.should.equal(True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_vpc_pcxs = conn.get_all_vpc_peering_connections() | 
					
						
							| 
									
										
										
										
											2018-11-14 12:03:42 +00:00
										 |  |  |     all_vpc_pcxs.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     all_vpc_pcxs[0]._status.code.should.equal("deleted") | 
					
						
							| 
									
										
										
										
											2014-07-31 14:41:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-08-25 10:54:47 -07:00
										 |  |  |         conn.delete_vpc_peering_connection("pcx-1234abcd") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.code.should.equal("InvalidVpcPeeringConnectionId.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2018-09-21 23:29:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_peering_connections_cross_region(): | 
					
						
							|  |  |  |     # create vpc in us-west-1 and ap-northeast-1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_usw1 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc_usw1 = ec2_usw1.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							|  |  |  |     ec2_apn1 = boto3.resource("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     vpc_apn1 = ec2_apn1.create_vpc(CidrBlock="10.20.0.0/16") | 
					
						
							| 
									
										
										
										
											2018-09-21 23:29:04 +08:00
										 |  |  |     # create peering | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc_usw1.id, PeerVpcId=vpc_apn1.id, PeerRegion="ap-northeast-1" | 
					
						
							| 
									
										
										
										
											2018-09-21 23:29:04 +08:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc_pcx_usw1.status["Code"].should.equal("initiating-request") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     vpc_pcx_usw1.requester_vpc.id.should.equal(vpc_usw1.id) | 
					
						
							|  |  |  |     vpc_pcx_usw1.accepter_vpc.id.should.equal(vpc_apn1.id) | 
					
						
							|  |  |  |     # test cross region vpc peering connection exist | 
					
						
							|  |  |  |     vpc_pcx_apn1 = ec2_apn1.VpcPeeringConnection(vpc_pcx_usw1.id) | 
					
						
							|  |  |  |     vpc_pcx_apn1.id.should.equal(vpc_pcx_usw1.id) | 
					
						
							|  |  |  |     vpc_pcx_apn1.requester_vpc.id.should.equal(vpc_usw1.id) | 
					
						
							|  |  |  |     vpc_pcx_apn1.accepter_vpc.id.should.equal(vpc_apn1.id) | 
					
						
							| 
									
										
										
										
											2018-09-21 23:29:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_peering_connections_cross_region_fail(): | 
					
						
							|  |  |  |     # create vpc in us-west-1 and ap-northeast-1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_usw1 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc_usw1 = ec2_usw1.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							|  |  |  |     ec2_apn1 = boto3.resource("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     vpc_apn1 = ec2_apn1.create_vpc(CidrBlock="10.20.0.0/16") | 
					
						
							| 
									
										
										
										
											2018-09-21 23:29:04 +08:00
										 |  |  |     # create peering wrong region with no vpc | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as cm: | 
					
						
							| 
									
										
										
										
											2018-09-21 23:29:04 +08:00
										 |  |  |         ec2_usw1.create_vpc_peering_connection( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             VpcId=vpc_usw1.id, PeerVpcId=vpc_apn1.id, PeerRegion="ap-northeast-2" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.response["Error"]["Code"].should.equal("InvalidVpcID.NotFound") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_peering_connections_cross_region_accept(): | 
					
						
							|  |  |  |     # create vpc in us-west-1 and ap-northeast-1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_usw1 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc_usw1 = ec2_usw1.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							|  |  |  |     ec2_apn1 = boto3.resource("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     vpc_apn1 = ec2_apn1.create_vpc(CidrBlock="10.20.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     # create peering | 
					
						
							|  |  |  |     vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc_usw1.id, PeerVpcId=vpc_apn1.id, PeerRegion="ap-northeast-1" | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  |     # accept peering from ap-northeast-1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_apn1 = boto3.client("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     ec2_usw1 = boto3.client("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     acp_pcx_apn1 = ec2_apn1.accept_vpc_peering_connection( | 
					
						
							|  |  |  |         VpcPeeringConnectionId=vpc_pcx_usw1.id | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     des_pcx_apn1 = ec2_usw1.describe_vpc_peering_connections( | 
					
						
							|  |  |  |         VpcPeeringConnectionIds=[vpc_pcx_usw1.id] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     des_pcx_usw1 = ec2_usw1.describe_vpc_peering_connections( | 
					
						
							|  |  |  |         VpcPeeringConnectionIds=[vpc_pcx_usw1.id] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     acp_pcx_apn1["VpcPeeringConnection"]["Status"]["Code"].should.equal("active") | 
					
						
							| 
									
										
										
										
											2020-07-05 23:04:34 +07:00
										 |  |  |     acp_pcx_apn1["VpcPeeringConnection"]["AccepterVpcInfo"]["Region"].should.equal( | 
					
						
							|  |  |  |         "ap-northeast-1" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     acp_pcx_apn1["VpcPeeringConnection"]["RequesterVpcInfo"]["Region"].should.equal( | 
					
						
							|  |  |  |         "us-west-1" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     des_pcx_apn1["VpcPeeringConnections"][0]["Status"]["Code"].should.equal("active") | 
					
						
							| 
									
										
										
										
											2020-07-05 23:04:34 +07:00
										 |  |  |     des_pcx_apn1["VpcPeeringConnections"][0]["AccepterVpcInfo"]["Region"].should.equal( | 
					
						
							|  |  |  |         "ap-northeast-1" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     des_pcx_apn1["VpcPeeringConnections"][0]["RequesterVpcInfo"]["Region"].should.equal( | 
					
						
							|  |  |  |         "us-west-1" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     des_pcx_usw1["VpcPeeringConnections"][0]["Status"]["Code"].should.equal("active") | 
					
						
							| 
									
										
										
										
											2020-07-05 23:04:34 +07:00
										 |  |  |     des_pcx_usw1["VpcPeeringConnections"][0]["AccepterVpcInfo"]["Region"].should.equal( | 
					
						
							|  |  |  |         "ap-northeast-1" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     des_pcx_usw1["VpcPeeringConnections"][0]["RequesterVpcInfo"]["Region"].should.equal( | 
					
						
							|  |  |  |         "us-west-1" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_peering_connections_cross_region_reject(): | 
					
						
							|  |  |  |     # create vpc in us-west-1 and ap-northeast-1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_usw1 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc_usw1 = ec2_usw1.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							|  |  |  |     ec2_apn1 = boto3.resource("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     vpc_apn1 = ec2_apn1.create_vpc(CidrBlock="10.20.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     # create peering | 
					
						
							|  |  |  |     vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc_usw1.id, PeerVpcId=vpc_apn1.id, PeerRegion="ap-northeast-1" | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  |     # reject peering from ap-northeast-1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_apn1 = boto3.client("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     ec2_usw1 = boto3.client("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     rej_pcx_apn1 = ec2_apn1.reject_vpc_peering_connection( | 
					
						
							|  |  |  |         VpcPeeringConnectionId=vpc_pcx_usw1.id | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     des_pcx_apn1 = ec2_usw1.describe_vpc_peering_connections( | 
					
						
							|  |  |  |         VpcPeeringConnectionIds=[vpc_pcx_usw1.id] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     des_pcx_usw1 = ec2_usw1.describe_vpc_peering_connections( | 
					
						
							|  |  |  |         VpcPeeringConnectionIds=[vpc_pcx_usw1.id] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     rej_pcx_apn1["Return"].should.equal(True) | 
					
						
							|  |  |  |     des_pcx_apn1["VpcPeeringConnections"][0]["Status"]["Code"].should.equal("rejected") | 
					
						
							|  |  |  |     des_pcx_usw1["VpcPeeringConnections"][0]["Status"]["Code"].should.equal("rejected") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_peering_connections_cross_region_delete(): | 
					
						
							|  |  |  |     # create vpc in us-west-1 and ap-northeast-1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_usw1 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc_usw1 = ec2_usw1.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							|  |  |  |     ec2_apn1 = boto3.resource("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     vpc_apn1 = ec2_apn1.create_vpc(CidrBlock="10.20.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     # create peering | 
					
						
							|  |  |  |     vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc_usw1.id, PeerVpcId=vpc_apn1.id, PeerRegion="ap-northeast-1" | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  |     # reject peering from ap-northeast-1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_apn1 = boto3.client("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     ec2_usw1 = boto3.client("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     del_pcx_apn1 = ec2_apn1.delete_vpc_peering_connection( | 
					
						
							|  |  |  |         VpcPeeringConnectionId=vpc_pcx_usw1.id | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     des_pcx_apn1 = ec2_usw1.describe_vpc_peering_connections( | 
					
						
							|  |  |  |         VpcPeeringConnectionIds=[vpc_pcx_usw1.id] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     des_pcx_usw1 = ec2_usw1.describe_vpc_peering_connections( | 
					
						
							|  |  |  |         VpcPeeringConnectionIds=[vpc_pcx_usw1.id] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     del_pcx_apn1["Return"].should.equal(True) | 
					
						
							|  |  |  |     des_pcx_apn1["VpcPeeringConnections"][0]["Status"]["Code"].should.equal("deleted") | 
					
						
							|  |  |  |     des_pcx_usw1["VpcPeeringConnections"][0]["Status"]["Code"].should.equal("deleted") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_peering_connections_cross_region_accept_wrong_region(): | 
					
						
							|  |  |  |     # create vpc in us-west-1 and ap-northeast-1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_usw1 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc_usw1 = ec2_usw1.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							|  |  |  |     ec2_apn1 = boto3.resource("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     vpc_apn1 = ec2_apn1.create_vpc(CidrBlock="10.20.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     # create peering | 
					
						
							|  |  |  |     vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc_usw1.id, PeerVpcId=vpc_apn1.id, PeerRegion="ap-northeast-1" | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # accept wrong peering from us-west-1 which will raise error | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_apn1 = boto3.client("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     ec2_usw1 = boto3.client("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ec2_usw1.accept_vpc_peering_connection(VpcPeeringConnectionId=vpc_pcx_usw1.id) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.response["Error"]["Code"].should.equal("OperationNotPermitted") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     exp_msg = ( | 
					
						
							|  |  |  |         "Incorrect region ({0}) specified for this request.VPC " | 
					
						
							|  |  |  |         "peering connection {1} must be " | 
					
						
							|  |  |  |         "accepted in region {2}".format("us-west-1", vpc_pcx_usw1.id, "ap-northeast-1") | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.response["Error"]["Message"].should.equal(exp_msg) | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_vpc_peering_connections_cross_region_reject_wrong_region(): | 
					
						
							|  |  |  |     # create vpc in us-west-1 and ap-northeast-1 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_usw1 = boto3.resource("ec2", region_name="us-west-1") | 
					
						
							|  |  |  |     vpc_usw1 = ec2_usw1.create_vpc(CidrBlock="10.90.0.0/16") | 
					
						
							|  |  |  |     ec2_apn1 = boto3.resource("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     vpc_apn1 = ec2_apn1.create_vpc(CidrBlock="10.20.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     # create peering | 
					
						
							|  |  |  |     vpc_pcx_usw1 = ec2_usw1.create_vpc_peering_connection( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc_usw1.id, PeerVpcId=vpc_apn1.id, PeerRegion="ap-northeast-1" | 
					
						
							| 
									
										
										
										
											2019-05-25 17:21:57 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  |     # reject wrong peering from us-west-1 which will raise error | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2_apn1 = boto3.client("ec2", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     ec2_usw1 = boto3.client("ec2", region_name="us-west-1") | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as cm: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ec2_usw1.reject_vpc_peering_connection(VpcPeeringConnectionId=vpc_pcx_usw1.id) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.response["Error"]["Code"].should.equal("OperationNotPermitted") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     exp_msg = ( | 
					
						
							|  |  |  |         "Incorrect region ({0}) specified for this request.VPC " | 
					
						
							|  |  |  |         "peering connection {1} must be accepted or " | 
					
						
							|  |  |  |         "rejected in region {2}".format("us-west-1", vpc_pcx_usw1.id, "ap-northeast-1") | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.response["Error"]["Message"].should.equal(exp_msg) |