Best Python code snippet using localstack_python
ec2_starter.py
Source:ec2_starter.py  
...181        }182        result = xmltodict.unparse(result)183        return result184    vpcs.VPCs.modify_vpc_endpoint = modify_vpc_endpoint185    def create_vpc_endpoint_service_configuration(self):186        configs = getattr(self.ec2_backend, "vpc_endpoint_service_configurations", {})187        self.ec2_backend.vpc_endpoint_service_configurations = configs188        dns_name = self._get_param("PrivateDnsName")189        lb_arns = self._get_multi_param("NetworkLoadBalancerArn")190        gw_arns = self._get_multi_param("GatewayLoadBalancerArn")191        tags = self._get_multi_param("TagSpecification")192        tags = (tags or {}).get("Tags")193        service_id = short_uid()194        entry = {195            "serviceId": service_id,196            "privateDnsName": dns_name,197            "networkLoadBalancerArns": lb_arns,198            "gatewayLoadBalancerArns": gw_arns,199            "Tags": tags,...test_vpc_service_configuration.py
Source:test_vpc_service_configuration.py  
...9@mock_ec210def test_create_vpc_endpoint_service_configuration_without_params():11    client = boto3.client("ec2", region_name="us-west-2")12    with pytest.raises(ClientError) as exc:13        client.create_vpc_endpoint_service_configuration()14    err = exc.value.response["Error"]15    err["Code"].should.equal("InvalidParameter")16    err["Message"].should.equal(17        "exactly one of network_load_balancer_arn or gateway_load_balancer_arn is a required member"18    )19@mock_ec220@mock_elbv221def test_create_vpc_endpoint_service_configuration_with_network_load_balancer():22    region_name = "eu-west-3"23    client = boto3.client("ec2", region_name=region_name)24    lb_arn = create_load_balancer(25        region_name=region_name, lb_type="network", zone="eu-west-3b"26    )27    resp = client.create_vpc_endpoint_service_configuration(28        NetworkLoadBalancerArns=[lb_arn]29    )30    resp.should.have.key("ServiceConfiguration")31    config = resp["ServiceConfiguration"]32    config.should.have.key("ServiceType").equals([{"ServiceType": "Interface"}])33    config.should.have.key("ServiceId").match("^vpce-svc-")34    config.should.have.key("ServiceName").equals(35        f"com.amazonaws.vpce.eu-west-3.{config['ServiceId']}"36    )37    config.should.have.key("ServiceState").equals("Available")38    config.should.have.key("AvailabilityZones").equals(["eu-west-3b"])39    config.should.have.key("AcceptanceRequired").equals(True)40    config.should.have.key("ManagesVpcEndpoints").equals(False)41    config.should.have.key("NetworkLoadBalancerArns").equals([lb_arn])42    config.should.have.key("BaseEndpointDnsNames").equals(43        [f"{config['ServiceId']}.eu-west-3.vpce.amazonaws.com"]44    )45    config.should.have.key("PrivateDnsNameConfiguration").equals({})46    config.shouldnt.have.key("GatewayLoadBalancerArns")47@mock_ec248@mock_elbv249def test_create_vpc_endpoint_service_configuration_with_gateway_load_balancer():50    region = "us-east-2"51    client = boto3.client("ec2", region_name=region)52    lb_arn = create_load_balancer(53        region_name=region, lb_type="gateway", zone="us-east-1c"54    )55    resp = client.create_vpc_endpoint_service_configuration(56        GatewayLoadBalancerArns=[lb_arn]57    )58    resp.should.have.key("ServiceConfiguration")59    config = resp["ServiceConfiguration"]60    config.should.have.key("ServiceType").equals([{"ServiceType": "Gateway"}])61    config.should.have.key("ServiceId").match("^vpce-svc-")62    config.should.have.key("ServiceName").equals(63        f"com.amazonaws.vpce.us-east-2.{config['ServiceId']}"64    )65    config.should.have.key("ServiceState").equals("Available")66    config.should.have.key("AvailabilityZones").equals(["us-east-1c"])67    config.should.have.key("AcceptanceRequired").equals(True)68    config.should.have.key("ManagesVpcEndpoints").equals(False)69    config.should.have.key("GatewayLoadBalancerArns").equals([lb_arn])70    config.should.have.key("BaseEndpointDnsNames").equals(71        [f"{config['ServiceId']}.us-east-2.vpce.amazonaws.com"]72    )73    config.should.have.key("PrivateDnsNameConfiguration").equals({})74    config.shouldnt.have.key("NetworkLoadBalancerArns")75@mock_ec276@mock_elbv277def test_create_vpc_endpoint_service_configuration_with_options():78    client = boto3.client("ec2", region_name="us-east-2")79    lb_arn = create_load_balancer(80        region_name="us-east-2", lb_type="gateway", zone="us-east-1c"81    )82    resp = client.create_vpc_endpoint_service_configuration(83        GatewayLoadBalancerArns=[lb_arn],84        AcceptanceRequired=False,85        PrivateDnsName="example.com",86    )87    resp.should.have.key("ServiceConfiguration")88    config = resp["ServiceConfiguration"]89    config.should.have.key("AcceptanceRequired").equals(False)90    config.should.have.key("PrivateDnsName").equals("example.com")91    config.should.have.key("PrivateDnsNameConfiguration").equals(92        {"Name": "n", "State": "verified", "Type": "TXT", "Value": "val"}93    )94@mock_ec295@mock_elbv296def test_describe_vpc_endpoint_service_configurations():97    region = "us-east-2"98    client = boto3.client("ec2", region_name=region)99    lb_arn = create_load_balancer(100        region_name=region, lb_type="gateway", zone="us-east-1c"101    )102    config1 = client.create_vpc_endpoint_service_configuration(103        GatewayLoadBalancerArns=[lb_arn]104    )["ServiceConfiguration"]["ServiceId"]105    config2 = client.create_vpc_endpoint_service_configuration(106        GatewayLoadBalancerArns=[lb_arn]107    )["ServiceConfiguration"]["ServiceId"]108    resp = client.describe_vpc_endpoint_service_configurations()109    resp.should.have.key("ServiceConfigurations")110    service_ids = [s["ServiceId"] for s in resp["ServiceConfigurations"]]111    service_ids.should.contain(config1)112    service_ids.should.contain(config2)113    resp = client.describe_vpc_endpoint_service_configurations(ServiceIds=[config2])114    resp.should.have.key("ServiceConfigurations").length_of(1)115    result = resp["ServiceConfigurations"][0]116    result.should.have.key("ServiceId").equals(config2)117    result.should.have.key("ServiceName")118    result.should.have.key("ServiceState")119    result.should.have.key("GatewayLoadBalancerArns").equals([lb_arn])120@mock_ec2121@mock_elbv2122@pytest.mark.parametrize(123    "tags",124    [125        [{"Key": "k1", "Value": "v1"}],126        [{"Key": "k1", "Value": "v1"}, {"Key": "k2", "Value": "v2"}],127    ],128)129def test_describe_vpc_endpoint_service_configurations_with_tags(tags):130    region = "us-east-2"131    client = boto3.client("ec2", region_name=region)132    lb_arn = create_load_balancer(133        region_name=region, lb_type="gateway", zone="us-east-1c"134    )135    service_id = client.create_vpc_endpoint_service_configuration(136        GatewayLoadBalancerArns=[lb_arn],137        TagSpecifications=[{"ResourceType": "vpc-endpoint-service", "Tags": tags}],138    )["ServiceConfiguration"]["ServiceId"]139    resp = client.describe_vpc_endpoint_service_configurations(ServiceIds=[service_id])140    resp.should.have.key("ServiceConfigurations").length_of(1)141    result = resp["ServiceConfigurations"][0]142    result.should.have.key("Tags").length_of(len(tags))143    for tag in tags:144        result["Tags"].should.contain(tag)145@mock_ec2146@mock_elbv2147def test_describe_vpc_endpoint_service_configurations_and_add_tags():148    tags = [{"Key": "k1", "Value": "v1"}]149    region = "us-east-2"150    client = boto3.client("ec2", region_name=region)151    lb_arn = create_load_balancer(152        region_name=region, lb_type="gateway", zone="us-east-1c"153    )154    service_id = client.create_vpc_endpoint_service_configuration(155        GatewayLoadBalancerArns=[lb_arn]156    )["ServiceConfiguration"]["ServiceId"]157    client.create_tags(Resources=[service_id], Tags=tags)158    resp = client.describe_vpc_endpoint_service_configurations(ServiceIds=[service_id])159    resp.should.have.key("ServiceConfigurations").length_of(1)160    result = resp["ServiceConfigurations"][0]161    result.should.have.key("Tags").length_of(len(tags))162    for tag in tags:163        result["Tags"].should.contain(tag)164@mock_ec2165def test_describe_vpc_endpoint_service_configurations_unknown():166    client = boto3.client("ec2", region_name="eu-west-3")167    with pytest.raises(ClientError) as exc:168        # Will always fail if at least one ServiceId is unknown169        client.describe_vpc_endpoint_service_configurations(170            ServiceIds=["vpce-svc-unknown"]171        )172    err = exc.value.response["Error"]173    err["Code"].should.equal("InvalidVpcEndpointServiceId.NotFound")174    err["Message"].should.equal(175        "The VpcEndpointService Id 'vpce-svc-unknown' does not exist"176    )177@mock_ec2178@mock_elbv2179def test_delete_vpc_endpoint_service_configurations():180    region = "us-east-2"181    client = boto3.client("ec2", region_name=region)182    lb_arn = create_load_balancer(183        region_name=region, lb_type="gateway", zone="us-east-1c"184    )185    service_id = client.create_vpc_endpoint_service_configuration(186        GatewayLoadBalancerArns=[lb_arn]187    )["ServiceConfiguration"]["ServiceId"]188    resp = client.delete_vpc_endpoint_service_configurations(ServiceIds=[service_id])189    resp.should.have.key("Unsuccessful").equals([])190@mock_ec2191def test_delete_vpc_endpoint_service_configurations_already_deleted():192    client = boto3.client("ec2", region_name="eu-west-3")193    resp = client.delete_vpc_endpoint_service_configurations(194        ServiceIds=["vpce-svc-03cf101d15c3bff53"]195    )196    resp.should.have.key("Unsuccessful").length_of(1)197    u = resp["Unsuccessful"][0]198    u.should.have.key("ResourceId").equals("vpce-svc-03cf101d15c3bff53")199    u.should.have.key("Error")200    u["Error"].should.have.key("Code").equals("InvalidVpcEndpointService.NotFound")201    u["Error"].should.have.key("Message").equals(202        "The VpcEndpointService Id 'vpce-svc-03cf101d15c3bff53' does not exist"203    )204@mock_ec2205@mock_elbv2206def test_describe_vpc_endpoint_service_permissions():207    region = "us-east-2"208    client = boto3.client("ec2", region_name=region)209    lb_arn = create_load_balancer(210        region_name=region, lb_type="gateway", zone="us-east-1c"211    )212    service_id = client.create_vpc_endpoint_service_configuration(213        GatewayLoadBalancerArns=[lb_arn]214    )["ServiceConfiguration"]["ServiceId"]215    resp = client.describe_vpc_endpoint_service_permissions(ServiceId=service_id)216    resp.should.have.key("AllowedPrincipals").equals([])217@mock_ec2218@mock_elbv2219def test_modify_vpc_endpoint_service_permissions():220    region = "us-east-2"221    client = boto3.client("ec2", region_name=region)222    lb_arn = create_load_balancer(223        region_name=region, lb_type="gateway", zone="us-east-1c"224    )225    service_id = client.create_vpc_endpoint_service_configuration(226        GatewayLoadBalancerArns=[lb_arn]227    )["ServiceConfiguration"]["ServiceId"]228    client.modify_vpc_endpoint_service_permissions(229        ServiceId=service_id, AddAllowedPrincipals=["prin1", "cipal2"]230    )231    resp = client.describe_vpc_endpoint_service_permissions(ServiceId=service_id)232    resp.should.have.key("AllowedPrincipals").length_of(2)233    resp["AllowedPrincipals"].should.contain({"Principal": "prin1"})234    resp["AllowedPrincipals"].should.contain({"Principal": "cipal2"})235    client.modify_vpc_endpoint_service_permissions(236        ServiceId=service_id, RemoveAllowedPrincipals=["prin1"]237    )238    resp = client.describe_vpc_endpoint_service_permissions(ServiceId=service_id)239    resp.should.have.key("AllowedPrincipals").length_of(1)240    resp["AllowedPrincipals"].should.contain({"Principal": "cipal2"})241@mock_ec2242@mock_elbv2243def test_modify_vpc_endpoint_service_configuration():244    region = "us-east-2"245    client = boto3.client("ec2", region_name=region)246    lb_arn = create_load_balancer(247        region_name=region, lb_type="gateway", zone="us-east-1c"248    )249    service_id = client.create_vpc_endpoint_service_configuration(250        GatewayLoadBalancerArns=[lb_arn]251    )["ServiceConfiguration"]["ServiceId"]252    client.modify_vpc_endpoint_service_configuration(253        ServiceId=service_id, PrivateDnsName="dnsname", AcceptanceRequired=False254    )255    config = client.describe_vpc_endpoint_service_configurations(256        ServiceIds=[service_id]257    )["ServiceConfigurations"][0]258    config.should.have.key("AcceptanceRequired").equals(False)259    config.should.have.key("PrivateDnsName").equals("dnsname")260@mock_ec2261@mock_elbv2262def test_modify_vpc_endpoint_service_configuration_with_new_loadbalancers():263    region = "us-east-2"264    client = boto3.client("ec2", region_name=region)265    lb_arn = create_load_balancer(266        region_name=region, lb_type="gateway", zone="us-east-1c"267    )268    lb_arn2 = create_load_balancer(269        region_name=region, lb_type="gateway", zone="us-east-1c"270    )271    lb_arn3 = create_load_balancer(272        region_name=region, lb_type="network", zone="us-east-1c"273    )274    service_id = client.create_vpc_endpoint_service_configuration(275        GatewayLoadBalancerArns=[lb_arn]276    )["ServiceConfiguration"]["ServiceId"]277    client.modify_vpc_endpoint_service_configuration(278        ServiceId=service_id,279        AddNetworkLoadBalancerArns=[lb_arn3],280        AddGatewayLoadBalancerArns=[lb_arn2],281    )282    config = client.describe_vpc_endpoint_service_configurations(283        ServiceIds=[service_id]284    )["ServiceConfigurations"][0]285    config["GatewayLoadBalancerArns"].should.equal([lb_arn, lb_arn2])286    config["NetworkLoadBalancerArns"].should.equal([lb_arn3])287    client.modify_vpc_endpoint_service_configuration(288        ServiceId=service_id,...vpc_service_configuration.py
Source:vpc_service_configuration.py  
1from moto.core.responses import BaseResponse2from ..exceptions import NoLoadBalancersProvided3class VPCEndpointServiceConfiguration(BaseResponse):4    def create_vpc_endpoint_service_configuration(self):5        gateway_lbs = self._get_multi_param("GatewayLoadBalancerArn")6        network_lbs = self._get_multi_param("NetworkLoadBalancerArn")7        if not gateway_lbs and not network_lbs:8            raise NoLoadBalancersProvided9        tags = self._get_multi_param("TagSpecification")10        if tags:11            tags = tags[0].get("Tag")12        acceptance_required = (13            str(self._get_param("AcceptanceRequired", "true")).lower() == "true"14        )15        private_dns_name = self._get_param("PrivateDnsName")16        config = self.ec2_backend.create_vpc_endpoint_service_configuration(17            gateway_lbs or network_lbs,18            acceptance_required=acceptance_required,19            private_dns_name=private_dns_name,20            tags=tags,21        )22        template = self.response_template(CREATE_VPC_ENDPOINT_SERVICE_CONFIGURATION)23        return template.render(config=config)24    def describe_vpc_endpoint_service_configurations(self):25        service_ids = self._get_multi_param("ServiceId")26        configs = self.ec2_backend.describe_vpc_endpoint_service_configurations(27            service_ids28        )29        template = self.response_template(DESCRIBE_VPC_ENDPOINT_SERVICE_CONFIGURATION)30        return template.render(configs=configs)...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!!
