Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
ec2: add DescribeVpcEndpointServices (#569)
Nahuel Nucerad20f7c8de716
diff --git a/ministack/services/ec2.py b/ministack/services/ec2.pyindex 78ad0e95..6e63bd5d 100644--- a/ministack/services/ec2.py+++ b/ministack/services/ec2.py@@ -24,7 +24,7 @@ Supports:ENI: CreateNetworkInterface, DeleteNetworkInterface, DescribeNetworkInterfaces,AttachNetworkInterface, DetachNetworkInterfaceVPC Endpoints: CreateVpcEndpoint, DeleteVpcEndpoints, DescribeVpcEndpoints,- ModifyVpcEndpoint, DescribePrefixLists+ DescribeVpcEndpointServices, ModifyVpcEndpoint, DescribePrefixListsEBS Volumes: CreateVolume, DeleteVolume, DescribeVolumes, DescribeVolumeStatus,AttachVolume, DetachVolume, ModifyVolume, DescribeVolumesModifications,EnableVolumeIO, ModifyVolumeAttribute, DescribeVolumeAttribute@@ -1470,6 +1470,72 @@ def _describe_vpc_endpoints(p):f"<vpcEndpointSet>{items}</vpcEndpointSet>")+# Services available as VPC endpoints. s3 and dynamodb are Gateway endpoints+# (free, route-table-based); the rest are Interface (PrivateLink).+_VPCE_GATEWAY_SERVICES = ("s3", "dynamodb")+_VPCE_INTERFACE_SERVICES = (+ "ec2", "sts", "logs", "sqs", "sns", "kinesis", "lambda",+ "ssm", "ssmmessages", "ec2messages", "secretsmanager", "kms",+ "monitoring", "events", "ecr.api", "ecr.dkr", "execute-api",+)+++def _describe_vpc_endpoint_services(p):+ region = get_region()+ azs = [f"{region}a", f"{region}b", f"{region}c"]+ requested = _parse_member_list(p, "ServiceName")+ filters = _parse_filters(p)+ name_filters = filters.get("service-name", [])+ type_filters = filters.get("service-type", [])++ catalog = []+ for svc in _VPCE_GATEWAY_SERVICES:+ catalog.append((f"com.amazonaws.{region}.{svc}", "Gateway"))+ for svc in _VPCE_INTERFACE_SERVICES:+ catalog.append((f"com.amazonaws.{region}.{svc}", "Interface"))++ selected = []+ for name, stype in catalog:+ if requested and name not in requested:+ continue+ if name_filters and name not in name_filters:+ continue+ if type_filters and stype not in type_filters:+ continue+ selected.append((name, stype))++ name_items = "".join(f"<item>{n}</item>" for n, _ in selected)+ az_items = "".join(f"<item>{az}</item>" for az in azs)++ detail_items = []+ for name, stype in selected:+ short = name.rsplit(".", 1)[-1]+ if stype == "Gateway":+ base_dns = f"<item>{short}.{region}.amazonaws.com</item>"+ private_dns = ""+ else:+ base_dns = f"<item>{short}.{region}.vpce.amazonaws.com</item>"+ private_dns = f"<privateDnsName>{short}.{region}.amazonaws.com</privateDnsName>"+ detail_items.append(f"""<item>+ <serviceName>{name}</serviceName>+ <serviceId>vpce-svc-{abs(hash(name)) & 0xffffffffffff:012x}</serviceId>+ <serviceType><item><serviceType>{stype}</serviceType></item></serviceType>+ <availabilityZoneSet>{az_items}</availabilityZoneSet>+ <owner>amazon</owner>+ <baseEndpointDnsNameSet>{base_dns}</baseEndpointDnsNameSet>+ {private_dns}+ <vpcEndpointPolicySupported>{'false' if stype == 'Gateway' else 'true'}</vpcEndpointPolicySupported>+ <acceptanceRequired>false</acceptanceRequired>+ <managesVpcEndpoints>false</managesVpcEndpoints>+ <supportedIpAddressTypeSet><item>ipv4</item></supportedIpAddressTypeSet>+ </item>""")++ return _xml(200, "DescribeVpcEndpointServicesResponse", f"""+ <serviceNameSet>{name_items}</serviceNameSet>+ <serviceDetailSet>{''.join(detail_items)}</serviceDetailSet>+ """)++# ---------------------------------------------------------------------------# Availability Zones# ---------------------------------------------------------------------------@@ -4235,6 +4301,7 @@ _ACTION_MAP = {"CreateVpcEndpoint": _create_vpc_endpoint,"DeleteVpcEndpoints": _delete_vpc_endpoints,"DescribeVpcEndpoints": _describe_vpc_endpoints,+ "DescribeVpcEndpointServices": _describe_vpc_endpoint_services,"ReplaceRouteTableAssociation": _replace_route_table_association,"ModifyVpcEndpoint": _modify_vpc_endpoint,"DescribePrefixLists": _describe_prefix_lists,diff --git a/tests/test_ec2.py b/tests/test_ec2.pyindex 3d6fa3a9..395affed 100644--- a/tests/test_ec2.py+++ b/tests/test_ec2.py@@ -546,6 +546,38 @@ def test_ec2_vpc_endpoint_crud(ec2):ec2.delete_vpc(VpcId=vpc_id)+def test_ec2_describe_vpc_endpoint_services(ec2):+ resp = ec2.describe_vpc_endpoint_services()+ names = resp["ServiceNames"]+ assert "com.amazonaws.us-east-1.s3" in names+ assert "com.amazonaws.us-east-1.dynamodb" in names+ assert "com.amazonaws.us-east-1.sts" in names++ by_name = {d["ServiceName"]: d for d in resp["ServiceDetails"]}+ s3 = by_name["com.amazonaws.us-east-1.s3"]+ assert s3["ServiceType"][0]["ServiceType"] == "Gateway"+ assert s3["Owner"] == "amazon"+ assert "us-east-1a" in s3["AvailabilityZones"]+ assert s3["BaseEndpointDnsNames"] == ["s3.us-east-1.amazonaws.com"]+ assert "PrivateDnsName" not in s3++ sts = by_name["com.amazonaws.us-east-1.sts"]+ assert sts["ServiceType"][0]["ServiceType"] == "Interface"+ assert sts["PrivateDnsName"] == "sts.us-east-1.amazonaws.com"++ filtered = ec2.describe_vpc_endpoint_services(+ ServiceNames=["com.amazonaws.us-east-1.s3"],+ )+ assert filtered["ServiceNames"] == ["com.amazonaws.us-east-1.s3"]++ gw_only = ec2.describe_vpc_endpoint_services(+ Filters=[{"Name": "service-type", "Values": ["Gateway"]}],+ )+ assert all(+ d["ServiceType"][0]["ServiceType"] == "Gateway"+ for d in gw_only["ServiceDetails"]+ )+def test_ec2_describe_route_tables_default(ec2):desc = ec2.describe_route_tables()assert any(rt["VpcId"] == "vpc-00000001" for rt in desc["RouteTables"])