| 
									
										
										
										
											2014-08-27 11:17:06 -04:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2017-02-23 19:43:48 -05:00
										 |  |  | from botocore.exceptions import ClientError | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | import boto | 
					
						
							| 
									
										
										
										
											2014-11-15 13:50:58 -05:00
										 |  |  | import boto.ec2 | 
					
						
							| 
									
										
										
										
											2017-02-23 19:43:48 -05:00
										 |  |  | from boto.exception import EC2ResponseError | 
					
						
							| 
									
										
										
										
											2013-08-03 17:21:25 -04:00
										 |  |  | import sure  # noqa | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-13 16:08:23 +01:00
										 |  |  | from moto import mock_ec2, mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-08 23:05:40 -07:00
										 |  |  | from tests.helpers import requires_boto_gte | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2013-02-21 23:13:01 -05:00
										 |  |  | def test_elastic_network_interfaces(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         eni = conn.create_network_interface(subnet.id, dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the CreateNetworkInterface operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-26 16:21:17 +02:00
										 |  |  |     conn.create_network_interface(subnet.id) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_enis = conn.get_all_network_interfaces() | 
					
						
							|  |  |  |     all_enis.should.have.length_of(1) | 
					
						
							|  |  |  |     eni = all_enis[0] | 
					
						
							|  |  |  |     eni.groups.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2019-03-27 16:23:49 -04:00
										 |  |  |     eni.private_ip_addresses.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     eni.private_ip_addresses[0].private_ip_address.startswith("10.").should.be.true | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  |         conn.delete_network_interface(eni.id, dry_run=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the DeleteNetworkInterface operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     conn.delete_network_interface(eni.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_enis = conn.get_all_network_interfaces() | 
					
						
							|  |  |  |     all_enis.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |         conn.delete_network_interface(eni.id) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("InvalidNetworkInterfaceID.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | def test_elastic_network_interfaces_subnet_validation(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as cm: | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |         conn.create_network_interface("subnet-abcd1234") | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     cm.value.error_code.should.equal("InvalidSubnetID.NotFound") | 
					
						
							|  |  |  |     cm.value.status.should.equal(400) | 
					
						
							|  |  |  |     cm.value.request_id.should_not.be.none | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | def test_elastic_network_interfaces_with_private_ip(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") | 
					
						
							|  |  |  |     private_ip = "54.0.0.1" | 
					
						
							|  |  |  |     eni = conn.create_network_interface(subnet.id, private_ip) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     all_enis = conn.get_all_network_interfaces() | 
					
						
							|  |  |  |     all_enis.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eni = all_enis[0] | 
					
						
							|  |  |  |     eni.groups.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eni.private_ip_addresses.should.have.length_of(1) | 
					
						
							|  |  |  |     eni.private_ip_addresses[0].private_ip_address.should.equal(private_ip) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | def test_elastic_network_interfaces_with_groups(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group1 = conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "test security group #1", "this is a test security group" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group2 = conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "test security group #2", "this is a test security group" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     conn.create_network_interface( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         subnet.id, groups=[security_group1.id, security_group2.id] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_enis = conn.get_all_network_interfaces() | 
					
						
							|  |  |  |     all_enis.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eni = all_enis[0] | 
					
						
							|  |  |  |     eni.groups.should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     set([group.id for group in eni.groups]).should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         set([security_group1.id, security_group2.id]) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 23:05:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | @requires_boto_gte("2.12.0") | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-08 23:05:40 -07:00
										 |  |  | def test_elastic_network_interfaces_modify_attribute(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-09-08 23:05:40 -07:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group1 = conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "test security group #1", "this is a test security group" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group2 = conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "test security group #2", "this is a test security group" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-08 23:05:40 -07:00
										 |  |  |     conn.create_network_interface(subnet.id, groups=[security_group1.id]) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_enis = conn.get_all_network_interfaces() | 
					
						
							|  |  |  |     all_enis.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eni = all_enis[0] | 
					
						
							|  |  |  |     eni.groups.should.have.length_of(1) | 
					
						
							|  |  |  |     eni.groups[0].id.should.equal(security_group1.id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(EC2ResponseError) as ex: | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         conn.modify_network_interface_attribute( | 
					
						
							| 
									
										
										
										
											2021-05-07 13:50:26 +01:00
										 |  |  |             eni.id, "groupset", [security_group1.id, security_group2.id], dry_run=True | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.error_code.should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.status.should.equal(400) | 
					
						
							|  |  |  |     ex.value.message.should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the ModifyNetworkInterface operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-07 13:50:26 +01:00
										 |  |  |     conn.modify_network_interface_attribute( | 
					
						
							|  |  |  |         eni.id, "groupset", [security_group1.id, security_group2.id] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-08 23:05:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_enis = conn.get_all_network_interfaces() | 
					
						
							|  |  |  |     all_enis.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eni = all_enis[0] | 
					
						
							| 
									
										
										
										
											2021-05-07 13:50:26 +01:00
										 |  |  |     eni.groups.should.have.length_of(2) | 
					
						
							|  |  |  |     eni.groups[0].id.should.equal(security_group1.id) | 
					
						
							|  |  |  |     eni.groups[1].id.should.equal(security_group2.id) | 
					
						
							| 
									
										
										
										
											2014-09-08 23:05:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_ec2_deprecated | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | def test_elastic_network_interfaces_filtering(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto.connect_vpc("the_key", "the_secret") | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     vpc = conn.create_vpc("10.0.0.0/16") | 
					
						
							|  |  |  |     subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group1 = conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "test security group #1", "this is a test security group" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     security_group2 = conn.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "test security group #2", "this is a test security group" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     eni1 = conn.create_network_interface( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         subnet.id, groups=[security_group1.id, security_group2.id] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     eni2 = conn.create_network_interface(subnet.id, groups=[security_group1.id]) | 
					
						
							|  |  |  |     eni3 = conn.create_network_interface(subnet.id, description="test description") | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_enis = conn.get_all_network_interfaces() | 
					
						
							|  |  |  |     all_enis.should.have.length_of(3) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-14 14:42:20 -05:00
										 |  |  |     # Filter by NetworkInterfaceId | 
					
						
							|  |  |  |     enis_by_id = conn.get_all_network_interfaces([eni1.id]) | 
					
						
							|  |  |  |     enis_by_id.should.have.length_of(1) | 
					
						
							|  |  |  |     set([eni.id for eni in enis_by_id]).should.equal(set([eni1.id])) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     # Filter by ENI ID | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     enis_by_id = conn.get_all_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"network-interface-id": eni1.id} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     enis_by_id.should.have.length_of(1) | 
					
						
							|  |  |  |     set([eni.id for eni in enis_by_id]).should.equal(set([eni1.id])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Filter by Security Group | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     enis_by_group = conn.get_all_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"group-id": security_group1.id} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     enis_by_group.should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2015-02-14 14:42:20 -05:00
										 |  |  |     set([eni.id for eni in enis_by_group]).should.equal(set([eni1.id, eni2.id])) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Filter by ENI ID and Security Group | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     enis_by_group = conn.get_all_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"network-interface-id": eni1.id, "group-id": security_group1.id} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     enis_by_group.should.have.length_of(1) | 
					
						
							|  |  |  |     set([eni.id for eni in enis_by_group]).should.equal(set([eni1.id])) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  |     # Filter by Description | 
					
						
							|  |  |  |     enis_by_description = conn.get_all_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"description": eni3.description} | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  |     enis_by_description.should.have.length_of(1) | 
					
						
							|  |  |  |     enis_by_description[0].description.should.equal(eni3.description) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  |     # Unsupported filter | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     conn.get_all_network_interfaces.when.called_with( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         filters={"not-implemented-filter": "foobar"} | 
					
						
							|  |  |  |     ).should.throw(NotImplementedError) | 
					
						
							| 
									
										
										
										
											2014-09-08 16:50:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-03 14:24:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_elastic_network_interfaces_get_by_tag_name(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-2") | 
					
						
							|  |  |  |     ec2_client = boto3.client("ec2", region_name="us-west-2") | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     subnet = ec2.create_subnet( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-2a" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     eni1 = ec2.create_network_interface( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         SubnetId=subnet.id, PrivateIpAddress="10.0.10.5" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as ex: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         eni1.create_tags(Tags=[{"Key": "Name", "Value": "eni1"}], DryRun=True) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     ex.value.response["Error"]["Code"].should.equal("DryRunOperation") | 
					
						
							|  |  |  |     ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) | 
					
						
							|  |  |  |     ex.value.response["Error"]["Message"].should.equal( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-10-15 23:08:44 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     eni1.create_tags(Tags=[{"Key": "Name", "Value": "eni1"}]) | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # The status of the new interface should be 'available' | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     waiter = ec2_client.get_waiter("network_interface_available") | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     waiter.wait(NetworkInterfaceIds=[eni1.id]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "tag:Name", "Values": ["eni1"]}] | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "tag:Name", "Values": ["wrong-name"]}] | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-22 14:33:19 +01:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_elastic_network_interfaces_get_by_availability_zone(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-2") | 
					
						
							|  |  |  |     ec2_client = boto3.client("ec2", region_name="us-west-2") | 
					
						
							| 
									
										
										
										
											2017-03-22 14:33:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2017-03-22 14:33:19 +01:00
										 |  |  |     subnet1 = ec2.create_subnet( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-2a" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-03-22 14:33:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     subnet2 = ec2.create_subnet( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc.id, CidrBlock="10.0.1.0/24", AvailabilityZone="us-west-2b" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-03-22 14:33:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     eni1 = ec2.create_network_interface( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         SubnetId=subnet1.id, PrivateIpAddress="10.0.0.15" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-03-22 14:33:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     eni2 = ec2.create_network_interface( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         SubnetId=subnet2.id, PrivateIpAddress="10.0.1.15" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-03-22 14:33:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # The status of the new interface should be 'available' | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     waiter = ec2_client.get_waiter("network_interface_available") | 
					
						
							| 
									
										
										
										
											2017-03-22 14:33:19 +01:00
										 |  |  |     waiter.wait(NetworkInterfaceIds=[eni1.id, eni2.id]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "availability-zone", "Values": ["us-west-2a"]}] | 
					
						
							| 
									
										
										
										
											2017-03-22 14:33:19 +01:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "availability-zone", "Values": ["us-west-2c"]}] | 
					
						
							| 
									
										
										
										
											2017-03-22 14:33:19 +01:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_elastic_network_interfaces_get_by_private_ip(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-2") | 
					
						
							|  |  |  |     ec2_client = boto3.client("ec2", region_name="us-west-2") | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     subnet = ec2.create_subnet( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-2a" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     eni1 = ec2.create_network_interface( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         SubnetId=subnet.id, PrivateIpAddress="10.0.10.5" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # The status of the new interface should be 'available' | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     waiter = ec2_client.get_waiter("network_interface_available") | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     waiter.wait(NetworkInterfaceIds=[eni1.id]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "private-ip-address", "Values": ["10.0.10.5"]}] | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "private-ip-address", "Values": ["10.0.10.10"]}] | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "addresses.private-ip-address", "Values": ["10.0.10.5"]}] | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "addresses.private-ip-address", "Values": ["10.0.10.10"]}] | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_elastic_network_interfaces_get_by_vpc_id(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-2") | 
					
						
							|  |  |  |     ec2_client = boto3.client("ec2", region_name="us-west-2") | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     subnet = ec2.create_subnet( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-2a" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     eni1 = ec2.create_network_interface( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         SubnetId=subnet.id, PrivateIpAddress="10.0.10.5" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # The status of the new interface should be 'available' | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     waiter = ec2_client.get_waiter("network_interface_available") | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     waiter.wait(NetworkInterfaceIds=[eni1.id]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "vpc-id", "Values": [subnet.vpc_id]}] | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "vpc-id", "Values": ["vpc-aaaa1111"]}] | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_elastic_network_interfaces_get_by_subnet_id(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-2") | 
					
						
							|  |  |  |     ec2_client = boto3.client("ec2", region_name="us-west-2") | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     subnet = ec2.create_subnet( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-2a" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     eni1 = ec2.create_network_interface( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         SubnetId=subnet.id, PrivateIpAddress="10.0.10.5" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # The status of the new interface should be 'available' | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     waiter = ec2_client.get_waiter("network_interface_available") | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     waiter.wait(NetworkInterfaceIds=[eni1.id]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "subnet-id", "Values": [subnet.id]}] | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "subnet-id", "Values": ["subnet-aaaa1111"]}] | 
					
						
							| 
									
										
										
										
											2016-05-08 00:19:47 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_elastic_network_interfaces_get_by_description(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-2") | 
					
						
							|  |  |  |     ec2_client = boto3.client("ec2", region_name="us-west-2") | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  |     subnet = ec2.create_subnet( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-2a" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     eni1 = ec2.create_network_interface( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         SubnetId=subnet.id, PrivateIpAddress="10.0.10.5", Description="test interface" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # The status of the new interface should be 'available' | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     waiter = ec2_client.get_waiter("network_interface_available") | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  |     waiter.wait(NetworkInterfaceIds=[eni1.id]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "description", "Values": [eni1.description]}] | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filters = [{"Name": "description", "Values": ["bad description"]}] | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  |     enis = list(ec2.network_interfaces.filter(Filters=filters)) | 
					
						
							|  |  |  |     enis.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_elastic_network_interfaces_describe_network_interfaces_with_filter(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-2") | 
					
						
							|  |  |  |     ec2_client = boto3.client("ec2", region_name="us-west-2") | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  |     subnet = ec2.create_subnet( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-2a" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-11 18:50:15 +01:00
										 |  |  |     sg = ec2_client.create_security_group(Description="test", GroupName="test_sg") | 
					
						
							|  |  |  |     sg_id = sg["GroupId"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  |     eni1 = ec2.create_network_interface( | 
					
						
							| 
									
										
										
										
											2021-08-11 18:50:15 +01:00
										 |  |  |         SubnetId=subnet.id, | 
					
						
							|  |  |  |         PrivateIpAddress="10.0.10.5", | 
					
						
							|  |  |  |         Description="test interface", | 
					
						
							|  |  |  |         Groups=[sg_id], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # The status of the new interface should be 'available' | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     waiter = ec2_client.get_waiter("network_interface_available") | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  |     waiter.wait(NetworkInterfaceIds=[eni1.id]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Filter by network-interface-id | 
					
						
							|  |  |  |     response = ec2_client.describe_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Filters=[{"Name": "network-interface-id", "Values": [eni1.id]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"].should.have.length_of(1) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(eni1.id) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal( | 
					
						
							|  |  |  |         eni1.private_ip_address | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["Description"].should.equal(eni1.description) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-11 18:50:15 +01:00
										 |  |  |     # Filter by network-interface-id | 
					
						
							|  |  |  |     response = ec2_client.describe_network_interfaces( | 
					
						
							|  |  |  |         Filters=[{"Name": "group-id", "Values": [sg_id]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"].should.have.length_of(1) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(eni1.id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  |     response = ec2_client.describe_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Filters=[{"Name": "network-interface-id", "Values": ["bad-id"]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"].should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Filter by private-ip-address | 
					
						
							|  |  |  |     response = ec2_client.describe_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Filters=[{"Name": "private-ip-address", "Values": [eni1.private_ip_address]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"].should.have.length_of(1) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(eni1.id) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal( | 
					
						
							|  |  |  |         eni1.private_ip_address | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["Description"].should.equal(eni1.description) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     response = ec2_client.describe_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Filters=[{"Name": "private-ip-address", "Values": ["11.11.11.11"]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"].should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Filter by sunet-id | 
					
						
							|  |  |  |     response = ec2_client.describe_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Filters=[{"Name": "subnet-id", "Values": [eni1.subnet.id]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"].should.have.length_of(1) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(eni1.id) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal( | 
					
						
							|  |  |  |         eni1.private_ip_address | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["Description"].should.equal(eni1.description) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     response = ec2_client.describe_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Filters=[{"Name": "subnet-id", "Values": ["sn-bad-id"]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"].should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Filter by description | 
					
						
							|  |  |  |     response = ec2_client.describe_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Filters=[{"Name": "description", "Values": [eni1.description]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"].should.have.length_of(1) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(eni1.id) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal( | 
					
						
							|  |  |  |         eni1.private_ip_address | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["Description"].should.equal(eni1.description) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     response = ec2_client.describe_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Filters=[{"Name": "description", "Values": ["bad description"]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"].should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2019-07-15 00:01:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Filter by multiple filters | 
					
						
							|  |  |  |     response = ec2_client.describe_network_interfaces( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Filters=[ | 
					
						
							|  |  |  |             {"Name": "private-ip-address", "Values": [eni1.private_ip_address]}, | 
					
						
							|  |  |  |             {"Name": "network-interface-id", "Values": [eni1.id]}, | 
					
						
							|  |  |  |             {"Name": "subnet-id", "Values": [eni1.subnet.id]}, | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"].should.have.length_of(1) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(eni1.id) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal( | 
					
						
							|  |  |  |         eni1.private_ip_address | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     response["NetworkInterfaces"][0]["Description"].should.equal(eni1.description) | 
					
						
							| 
									
										
										
										
											2021-07-03 23:44:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_elastic_network_interfaces_filter_by_tag(): | 
					
						
							|  |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-2") | 
					
						
							|  |  |  |     ec2_client = boto3.client("ec2", region_name="us-west-2") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							|  |  |  |     subnet = ec2.create_subnet( | 
					
						
							|  |  |  |         VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-2a" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eni_dev = ec2.create_network_interface( | 
					
						
							|  |  |  |         SubnetId=subnet.id, | 
					
						
							|  |  |  |         PrivateIpAddress="10.0.10.5", | 
					
						
							|  |  |  |         Description="dev interface", | 
					
						
							|  |  |  |         TagSpecifications=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "ResourceType": "network-interface", | 
					
						
							|  |  |  |                 "Tags": [{"Key": "environment", "Value": "dev"}], | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eni_prod = ec2.create_network_interface( | 
					
						
							|  |  |  |         SubnetId=subnet.id, | 
					
						
							|  |  |  |         PrivateIpAddress="10.0.10.6", | 
					
						
							|  |  |  |         Description="prod interface", | 
					
						
							|  |  |  |         TagSpecifications=[ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "ResourceType": "network-interface", | 
					
						
							|  |  |  |                 "Tags": [{"Key": "environment", "Value": "prod"}], | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for eni in [eni_dev, eni_prod]: | 
					
						
							|  |  |  |         waiter = ec2_client.get_waiter("network_interface_available") | 
					
						
							|  |  |  |         waiter.wait(NetworkInterfaceIds=[eni.id]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = ec2_client.describe_network_interfaces( | 
					
						
							|  |  |  |         Filters=[{"Name": "tag:environment", "Values": ["staging"]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     resp["NetworkInterfaces"].should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = ec2_client.describe_network_interfaces( | 
					
						
							|  |  |  |         Filters=[{"Name": "tag:environment", "Values": ["dev"]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     resp["NetworkInterfaces"].should.have.length_of(1) | 
					
						
							|  |  |  |     resp["NetworkInterfaces"][0]["Description"].should.equal("dev interface") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = ec2_client.describe_network_interfaces( | 
					
						
							|  |  |  |         Filters=[{"Name": "tag:environment", "Values": ["prod"]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     resp["NetworkInterfaces"].should.have.length_of(1) | 
					
						
							|  |  |  |     resp["NetworkInterfaces"][0]["Description"].should.equal("prod interface") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = ec2_client.describe_network_interfaces( | 
					
						
							|  |  |  |         Filters=[{"Name": "tag:environment", "Values": ["dev", "prod"]}] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     resp["NetworkInterfaces"].should.have.length_of(2) | 
					
						
							| 
									
										
										
										
											2021-08-11 18:50:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | def test_elastic_network_interfaces_auto_create_securitygroup(): | 
					
						
							|  |  |  |     ec2 = boto3.resource("ec2", region_name="us-west-2") | 
					
						
							|  |  |  |     ec2_client = boto3.client("ec2", region_name="us-west-2") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") | 
					
						
							|  |  |  |     subnet = ec2.create_subnet( | 
					
						
							|  |  |  |         VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-2a" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eni1 = ec2.create_network_interface( | 
					
						
							|  |  |  |         SubnetId=subnet.id, PrivateIpAddress="10.0.10.5", Groups=["testgroup"] | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # The status of the new interface should be 'available' | 
					
						
							|  |  |  |     waiter = ec2_client.get_waiter("network_interface_available") | 
					
						
							|  |  |  |     waiter.wait(NetworkInterfaceIds=[eni1.id]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sgs = ec2_client.describe_security_groups()["SecurityGroups"] | 
					
						
							|  |  |  |     found_sg = [sg for sg in sgs if sg["GroupId"] == "testgroup"] | 
					
						
							|  |  |  |     found_sg.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     found_sg[0]["GroupName"].should.equal("testgroup") | 
					
						
							|  |  |  |     found_sg[0]["Description"].should.equal("testgroup") |