Best Python code snippet using localstack_python
vpc_egress_only_internet_gateway.py
Source:vpc_egress_only_internet_gateway.py
...58 if vpc_id.startswith("res-"):59 res = self.depl.get_typed_resource(vpc_id[4:].split(".")[0], "vpc")60 vpc_id = res._state['vpcId']61 self.log("creating egress only internet gateway in region {0}, vpc {1}".format(self._state['region'], vpc_id))62 response = self.get_client().create_egress_only_internet_gateway(VpcId=vpc_id)63 igw_id = response['EgressOnlyInternetGateway']['EgressOnlyInternetGatewayId']64 with self.depl._db:65 self.state = self.UP66 self._state['region'] = config['region']67 self._state['vpcId'] = vpc_id68 self._state['egressOnlyInternetGatewayId'] = igw_id69 def _destroy(self):70 if self.state != self.UP: return71 self.log("deleting egress only internet gateway {0}".format(self._state['egressOnlyInternetGatewayId']))72 self.get_client().delete_egress_only_internet_gateway(EgressOnlyInternetGatewayId=self._state['egressOnlyInternetGatewayId'])73 with self.depl._db:74 self.state = self.MISSING75 self._state['region'] = None76 self._state['vpcId'] = None...
egress_only_internet_gateways.py
Source:egress_only_internet_gateways.py
1from moto.core.responses import BaseResponse2from moto.ec2.utils import filters_from_querystring, add_tag_specification3class EgressOnlyInternetGateway(BaseResponse):4 def create_egress_only_internet_gateway(self):5 vpc_id = self._get_param("VpcId")6 tags = self._get_multi_param("TagSpecification")7 tags = add_tag_specification(tags)8 egress_only_igw = self.ec2_backend.create_egress_only_internet_gateway(9 vpc_id=vpc_id, tags=tags10 )11 template = self.response_template(CREATE_EGRESS_ONLY_IGW_RESPONSE)12 return template.render(egress_only_igw=egress_only_igw)13 def describe_egress_only_internet_gateways(self):14 egress_only_igw_ids = self._get_multi_param("EgressOnlyInternetGatewayId")15 filters = filters_from_querystring(self.querystring)16 egress_only_igws = self.ec2_backend.describe_egress_only_internet_gateways(17 egress_only_igw_ids, filters,18 )19 template = self.response_template(DESCRIBE_EGRESS_ONLY_IGW_RESPONSE)20 return template.render(egress_only_igws=egress_only_igws)21 def delete_egress_only_internet_gateway(self):22 egress_only_igw_id = self._get_param("EgressOnlyInternetGatewayId")...
test_egress_only_igw.py
Source:test_egress_only_igw.py
...7def test_create():8 ec2 = boto3.resource("ec2", region_name="us-west-1")9 client = boto3.client("ec2", region_name="us-west-1")10 vpc = list(ec2.vpcs.all())[0]11 res = client.create_egress_only_internet_gateway(VpcId=vpc.id)12 gateway = res["EgressOnlyInternetGateway"]13 gateway.should.have.key("EgressOnlyInternetGatewayId").match("eigw-[a-z0-9]+")14 gateway.should.have.key("Tags").equal([])15 gateway.should.have.key("Attachments")16 gateway["Attachments"].should.have.length_of(1)17 gateway["Attachments"][0].should.equal({"State": "attached", "VpcId": vpc.id})18@mock_ec219def test_create_with_unknown_vpc():20 client = boto3.client("ec2", region_name="us-west-1")21 with pytest.raises(ClientError) as ex:22 client.create_egress_only_internet_gateway(VpcId="vpc-says-what")23 err = ex.value.response["Error"]24 err["Code"].should.equal("InvalidVpcID.NotFound")25 err["Message"].should.equal("VpcID vpc-says-what does not exist.")26@mock_ec227def test_describe_all():28 ec2 = boto3.resource("ec2", region_name="us-west-1")29 client = boto3.client("ec2", region_name="us-west-1")30 vpc = list(ec2.vpcs.all())[0]31 gw1 = client.create_egress_only_internet_gateway(VpcId=vpc.id)[32 "EgressOnlyInternetGateway"33 ]34 gw2 = client.create_egress_only_internet_gateway(VpcId=vpc.id)[35 "EgressOnlyInternetGateway"36 ]37 gateways = client.describe_egress_only_internet_gateways()[38 "EgressOnlyInternetGateways"39 ]40 assert len(gateways) >= 2, "Should have two recently created gateways"41 gateways.should.contain(gw1)42 gateways.should.contain(gw2)43@mock_ec244def test_describe_one():45 ec2 = boto3.resource("ec2", region_name="us-west-1")46 client = boto3.client("ec2", region_name="us-west-1")47 vpc = list(ec2.vpcs.all())[0]48 gw1 = client.create_egress_only_internet_gateway(VpcId=vpc.id)[49 "EgressOnlyInternetGateway"50 ]51 gw1_id = gw1["EgressOnlyInternetGatewayId"]52 client.create_egress_only_internet_gateway(VpcId=vpc.id)[53 "EgressOnlyInternetGateway"54 ]55 gw3 = client.create_egress_only_internet_gateway(VpcId=vpc.id)[56 "EgressOnlyInternetGateway"57 ]58 gw3_id = gw3["EgressOnlyInternetGatewayId"]59 gateways = client.describe_egress_only_internet_gateways(60 EgressOnlyInternetGatewayIds=[gw1_id, gw3_id]61 )["EgressOnlyInternetGateways"]62 gateways.should.have.length_of(2)63 gateways.should.contain(gw1)64 gateways.should.contain(gw3)65@mock_ec266def test_create_and_delete():67 ec2 = boto3.resource("ec2", region_name="us-west-1")68 client = boto3.client("ec2", region_name="us-west-1")69 vpc = list(ec2.vpcs.all())[0]70 gw1 = client.create_egress_only_internet_gateway(VpcId=vpc.id)[71 "EgressOnlyInternetGateway"72 ]73 gw1_id = gw1["EgressOnlyInternetGatewayId"]74 client.describe_egress_only_internet_gateways(75 EgressOnlyInternetGatewayIds=[gw1_id]76 )["EgressOnlyInternetGateways"].should.have.length_of(1)77 client.delete_egress_only_internet_gateway(EgressOnlyInternetGatewayId=gw1_id)78 client.describe_egress_only_internet_gateways(79 EgressOnlyInternetGatewayIds=[gw1_id]...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!