How to use delete_vpc method in localstack

Best Python code snippet using localstack_python

test_vpcs.py

Source:test_vpcs.py Github

copy

Full Screen

...22 def setUpClass(cls):23 super(VPCTest, cls).setUpClass()24 if not base.TesterStateHolder().get_vpc_enabled():25 raise cls.skipException('VPC is disabled')26 def test_create_delete_vpc(self):27 cidr = '10.1.0.0/16'28 data = self.client.create_vpc(CidrBlock=cidr)29 vpc_id = data['Vpc']['VpcId']30 dv_clean = self.addResourceCleanUp(self.client.delete_vpc,31 VpcId=vpc_id)32 self.assertEqual(cidr, data['Vpc']['CidrBlock'])33 if CONF.aws.run_incompatible_tests:34 # NOTE(andrey-mp): not ready35 self.assertEqual('default', data['Vpc']['InstanceTenancy'])36 self.assertIsNotNone(data['Vpc'].get('DhcpOptionsId'))37 self.get_vpc_waiter().wait_available(vpc_id)38 self.client.delete_vpc(VpcId=vpc_id)39 self.cancelResourceCleanUp(dv_clean)40 self.get_vpc_waiter().wait_delete(vpc_id)41 self.assertRaises('InvalidVpcID.NotFound',42 self.client.describe_vpcs,43 VpcIds=[vpc_id])44 self.assertRaises('InvalidVpcID.NotFound',45 self.client.delete_vpc,46 VpcId=vpc_id)47 def test_create_more_than_one_vpc(self):48 cidr = '10.0.0.0/16'49 data = self.client.create_vpc(CidrBlock=cidr)50 vpc_id1 = data['Vpc']['VpcId']51 rc1 = self.addResourceCleanUp(self.client.delete_vpc, VpcId=vpc_id1)52 self.get_vpc_waiter().wait_available(vpc_id1)53 cidr = '10.1.0.0/16'54 data = self.client.create_vpc(CidrBlock=cidr)55 vpc_id2 = data['Vpc']['VpcId']56 rc2 = self.addResourceCleanUp(self.client.delete_vpc, VpcId=vpc_id2)57 self.get_vpc_waiter().wait_available(vpc_id2)58 self.client.delete_vpc(VpcId=vpc_id1)59 self.cancelResourceCleanUp(rc1)60 self.get_vpc_waiter().wait_delete(vpc_id1)61 self.client.delete_vpc(VpcId=vpc_id2)62 self.cancelResourceCleanUp(rc2)63 self.get_vpc_waiter().wait_delete(vpc_id2)64 def test_describe_vpcs_base(self):65 cidr = '10.1.0.0/16'66 data = self.client.create_vpc(CidrBlock=cidr)67 vpc_id = data['Vpc']['VpcId']68 dv_clean = self.addResourceCleanUp(self.client.delete_vpc,69 VpcId=vpc_id)70 self.get_vpc_waiter().wait_available(vpc_id)71 # NOTE(andrey-mp): by real id72 data = self.client.describe_vpcs(VpcIds=[vpc_id])73 self.assertEqual(1, len(data['Vpcs']))74 # NOTE(andrey-mp): by fake id75 self.assertRaises('InvalidVpcID.NotFound',76 self.client.describe_vpcs,77 VpcIds=['vpc-0'])78 self.client.delete_vpc(VpcId=vpc_id)79 self.cancelResourceCleanUp(dv_clean)80 self.get_vpc_waiter().wait_delete(vpc_id)81 def test_describe_vpcs_filters(self):82 cidr = '10.163.0.0/16'83 data = self.client.create_vpc(CidrBlock=cidr)84 vpc_id = data['Vpc']['VpcId']85 dv_clean = self.addResourceCleanUp(self.client.delete_vpc,86 VpcId=vpc_id)87 self.get_vpc_waiter().wait_available(vpc_id)88 # NOTE(andrey-mp): by filter real cidr89 data = self.client.describe_vpcs(90 Filters=[{'Name': 'cidr', 'Values': [cidr]}])91 self.assertEqual(1, len(data['Vpcs']))92 # NOTE(andrey-mp): by filter fake cidr93 data = self.client.describe_vpcs(94 Filters=[{'Name': 'cidr', 'Values': ['123.0.0.0/16']}])95 self.assertEqual(0, len(data['Vpcs']))96 # NOTE(andrey-mp): by fake filter97 self.assertRaises('InvalidParameterValue',98 self.client.describe_vpcs,99 Filters=[{'Name': 'fake', 'Values': ['fake']}])100 data = self.client.delete_vpc(VpcId=vpc_id)101 self.cancelResourceCleanUp(dv_clean)102 self.get_vpc_waiter().wait_delete(vpc_id)103 @testtools.skipUnless(CONF.aws.run_incompatible_tests,104 "Invalid request on checking vpc atributes.")105 def test_vpc_attributes(self):106 cidr = '10.1.0.0/16'107 data = self.client.create_vpc(CidrBlock=cidr)108 vpc_id = data['Vpc']['VpcId']109 dv_clean = self.addResourceCleanUp(self.client.delete_vpc,110 VpcId=vpc_id)111 self.get_vpc_waiter().wait_available(vpc_id)112 self._check_attribute(vpc_id, 'EnableDnsHostnames')113 self._check_attribute(vpc_id, 'EnableDnsSupport')114 data = self.client.delete_vpc(VpcId=vpc_id)115 self.cancelResourceCleanUp(dv_clean)116 self.get_vpc_waiter().wait_delete(vpc_id)117 def _check_attribute(self, vpc_id, attribute):118 req_attr = attribute[0].lower() + attribute[1:]119 data = self.client.describe_vpc_attribute(VpcId=vpc_id,120 Attribute=req_attr)121 attr = data[attribute].get('Value')122 self.assertIsNotNone(attr)123 kwargs = {'VpcId': vpc_id, attribute: {'Value': not attr}}124 data = self.client.modify_vpc_attribute(*[], **kwargs)125 data = self.client.describe_vpc_attribute(VpcId=vpc_id,126 Attribute=req_attr)127 self.assertNotEqual(attr, data[attribute].get('Value'))128 def test_create_with_invalid_cidr(self):129 def _rollback(fn_data):130 self.client.delete_vpc(VpcId=fn_data['Vpc']['VpcId'])131 # NOTE(andrey-mp): The largest uses a /16 netmask132 self.assertRaises('InvalidVpc.Range',133 self.client.create_vpc, rollback_fn=_rollback,134 CidrBlock='10.0.0.0/15')135 # NOTE(andrey-mp): The smallest VPC you can create uses a /28 netmask136 self.assertRaises('InvalidVpc.Range',137 self.client.create_vpc, rollback_fn=_rollback,138 CidrBlock='10.0.0.0/29')139 def test_describe_non_existing_vpc_by_id(self):140 vpc_id = 'vpc-00000000'141 self.assertRaises('InvalidVpcID.NotFound',142 self.client.describe_vpcs,143 VpcIds=[vpc_id])144 def test_describe_non_existing_vpc_by_cidr(self):145 data = self.client.describe_vpcs(146 Filters=[{'Name': 'cidr', 'Values': ['123.0.0.0/16']}])147 self.assertEqual(0, len(data['Vpcs']))148 def test_describe_with_invalid_filter(self):149 cidr = '10.1.0.0/16'150 data = self.client.create_vpc(CidrBlock=cidr)151 vpc_id = data['Vpc']['VpcId']152 dv_clean = self.addResourceCleanUp(self.client.delete_vpc,153 VpcId=vpc_id)154 self.get_vpc_waiter().wait_available(vpc_id)155 self.assertRaises('InvalidParameterValue',156 self.client.describe_vpcs,157 Filters=[{'Name': 'unknown', 'Values': ['unknown']}])158 data = self.client.delete_vpc(VpcId=vpc_id)159 self.cancelResourceCleanUp(dv_clean)...

Full Screen

Full Screen

clear_ch2.py

Source:clear_ch2.py Github

copy

Full Screen

...30def delete_subnet(ec2_client, subnet_id):31 # https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.delete_subnet32 response = ec2_client.delete_subnet(SubnetId=subnet_id)33 print(response)34def delete_vpc(ec2_client, vpc_id):35 # https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.delete_vpc36 response = ec2_client.delete_vpc(VpcId=vpc_id)37 print(response)38def delete_each_vpc_items(ec2_client, aws):39 # 逆順に解放していく40 # デフォルトゲートウェイからインターネットゲートウェイを削除41 delete_route_from_route_table(ec2_client, aws['public_route_table_id'])42 # ルートテーブルとサブネットの関連付けを削除43 disassociate_route_table(ec2_client, aws['public_route_table_association_id'])44 # ルートテーブルの削除45 delete_route_table(ec2_client, aws['public_route_table_id'])46 # インターネットゲートウェイの削除47 # インターネットゲートウェイをVPC領域から削除48 detach_internet_gateway_from_vpc(ec2_client, aws['internet_gateway_id'], aws['vpc_id'])49 # インターネットゲートウェイの削除50 delete_internet_gateway(ec2_client, aws['internet_gateway_id'])51 # サブネットの削除52 delete_subnet(ec2_client, aws['public_subnet_id'])53 # VPC領域の削除54 delete_vpc(ec2_client, aws_keys['vpc_id'])55if __name__ == '__main__':56 # Profileをロード57 session = boto3.Session(profile_name='my-profile')58 # クライアントとリソースを作っておく59 client = create_ec2_client(session)60 resource = create_ec2_resource(session)61 # AWSの各種IDをロード62 with open('aws.json', mode='r') as f:63 aws_keys = json.load(f)64 # それぞれのオブジェクトを、作成したのとは逆順に削除する場合65 delete_each_vpc_items(client, aws_keys)66 # GUIではVPCを削除するとそれぞれのオブジェクトも自動的に削除されるが、boto3だとエラーで削除できない67 # botocore.exceptions.ClientError: An error occurred (DependencyViolation) when calling the DeleteVpc operation:68 # The vpc 'vpc-a781dac3' has dependencies and cannot be deleted....

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful