Fix:Ec2-VPC:Added functionality describe-vpc-endpoint (#3524)

* Fix:Ec2:Added fucntionality describe-vpc-endpoint

* Refactor

* Added test cases for errors
This commit is contained in:
usmangani1 2020-12-08 03:09:57 +05:30 committed by GitHub
parent e1fc3a9596
commit af60306371
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 154 additions and 3 deletions

View File

@ -607,3 +607,11 @@ class InvalidAssociationIDIamProfileAssociationError(EC2ClientError):
"InvalidAssociationID.NotFound",
"An invalid association-id of '{0}' was given".format(association_id),
)
class InvalidVpcEndPointIdError(EC2ClientError):
def __init__(self, vpc_end_point_id):
super(InvalidVpcEndPointIdError, self).__init__(
"InvalidVpcEndPointId.NotFound",
"The VpcEndPoint ID '{0}' does not exist".format(vpc_end_point_id),
)

View File

@ -108,6 +108,7 @@ from .exceptions import (
InvalidParameterDependency,
IncorrectStateIamProfileAssociationError,
InvalidAssociationIDIamProfileAssociationError,
InvalidVpcEndPointIdError,
)
from .utils import (
EC2_RESOURCE_TO_PREFIX,
@ -3222,6 +3223,25 @@ class VPCBackend(object):
return vpc_end_point
def get_vpc_end_point(self, vpc_end_point_ids, filters=None):
vpc_end_points = self.vpc_end_points.values()
if vpc_end_point_ids:
vpc_end_points = [
vpc_end_point
for vpc_end_point in vpc_end_points
if vpc_end_point.id in vpc_end_point_ids
]
if len(vpc_end_points) != len(vpc_end_point_ids):
invalid_id = list(
set(vpc_end_point_ids).difference(
set([vpc_end_point.id for vpc_end_point in vpc_end_points])
)
)[0]
raise InvalidVpcEndPointIdError(invalid_id)
return generic_filter(filters, vpc_end_points)
def get_vpc_end_point_services(self):
vpc_end_point_services = self.vpc_end_points.values()

View File

@ -198,9 +198,18 @@ class VPCs(BaseResponse):
def describe_vpc_endpoint_services(self):
vpc_end_point_services = self.ec2_backend.get_vpc_end_point_services()
template = self.response_template(DESCRIBE_VPC_ENDPOINT_RESPONSE)
template = self.response_template(DESCRIBE_VPC_ENDPOINT_SERVICES_RESPONSE)
return template.render(vpc_end_points=vpc_end_point_services)
def describe_vpc_endpoints(self):
vpc_end_points_ids = self._get_multi_param("VpcEndpointId")
filters = filters_from_querystring(self.querystring)
vpc_end_points = self.ec2_backend.get_vpc_end_point(
vpc_end_point_ids=vpc_end_points_ids, filters=filters
)
template = self.response_template(DESCRIBE_VPC_ENDPOINT_RESPONSE)
return template.render(vpc_end_points=vpc_end_points)
CREATE_VPC_RESPONSE = """
<CreateVpcResponse xmlns="http://ec2.amazonaws.com/doc/{{doc_date}}/">
@ -460,7 +469,7 @@ CREATE_VPC_END_POINT = """ <CreateVpcEndpointResponse xmlns="http://monitoring.a
</vpcEndpoint>
</CreateVpcEndpointResponse>"""
DESCRIBE_VPC_ENDPOINT_RESPONSE = """<DescribeVpcEndpointServicesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
DESCRIBE_VPC_ENDPOINT_SERVICES_RESPONSE = """<DescribeVpcEndpointServicesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
<requestId>19a9ff46-7df6-49b8-9726-3df27527089d</requestId>
<serviceNameSet>
{% for serviceName in vpc_end_points.services %}
@ -491,3 +500,69 @@ DESCRIBE_VPC_ENDPOINT_RESPONSE = """<DescribeVpcEndpointServicesResponse xmlns="
</item>
</serviceDetailSet>
</DescribeVpcEndpointServicesResponse>"""
DESCRIBE_VPC_ENDPOINT_RESPONSE = """<DescribeVpcEndpointsResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
<requestId>19a9ff46-7df6-49b8-9726-3df27527089d</requestId>
<vpcEndpointSet>
{% for vpc_end_point in vpc_end_points %}
<item>
{% if vpc_end_point.policy_document %}
<policyDocument>{{ vpc_end_point.policy_document }}</policyDocument>
{% endif %}
<state>available</state>
<privateDnsEnabled>{{ vpc_end_point.private_dns_enabled }}</privateDnsEnabled>
<serviceName>{{ vpc_end_point.service_name }}</serviceName>
<vpcId>{{ vpc_end_point.vpc_id }}</vpcId>
<vpcEndpointId>{{ vpc_end_point.id }}</vpcEndpointId>
<vpcEndpointType>{{ vpc_end_point.type }}</vpcEndpointType>
{% if vpc_end_point.subnet_ids %}
<subnetIdSet>
{% for subnet_id in vpc_end_point.subnet_ids %}
<item>{{ subnet_id }}</item>
{% endfor %}
</subnetIdSet>
{% endif %}
{% if vpc_end_point.route_table_ids %}
<routeTableIdSet>
{% for route_table_id in vpc_end_point.route_table_ids %}
<item>{{ route_table_id }}</item>
{% endfor %}
</routeTableIdSet>
{% endif %}
{% if vpc_end_point.network_interface_ids %}
<networkInterfaceIdSet>
{% for network_interface_id in vpc_end_point.network_interface_ids %}
<item>{{ network_interface_id }}</item>
{% endfor %}
</networkInterfaceIdSet>
{% endif %}
{% if vpc_end_point.dns_entries %}
<dnsEntries>
{% for dns_entry in vpc_end_point.dns_entries %}
<item>{{ dns_entry }}</item>
{% endfor %}
</dnsEntries>
{% endif %}
{% if vpc_end_point.groups %}
<groups>
{% for group in vpc_end_point.groups %}
<item>{{ group }}</item>
{% endfor %}
</groups>
{% endif %}
{% if vpc_end_point.tag_specifications %}
<tagSet>
{% for tag in vpc_end_point.tag_specifications %}
<item>
<key>{{ tag.key }}</key>
<value>{{ tag.value }}</value>
</item>
{% endfor %}
</tagSet>
{% endif %}
<ownerId>123456789012</ownerId>
<creationTimestamp>{{ vpc_end_point.created_at }}</creationTimestamp>
</item>
{% endfor %}
</vpcEndpointSet>
</DescribeVpcEndpointsResponse>"""

View File

@ -8,7 +8,8 @@ from botocore.exceptions import ClientError
import boto3
import boto
from boto.exception import EC2ResponseError
import sure # noqa
# import sure # noqa
from moto import mock_ec2, mock_ec2_deprecated
@ -868,3 +869,50 @@ def test_describe_vpc_end_point_services():
"us-west-1a",
"us-west-1b",
]
@mock_ec2
def test_describe_vpc_end_points():
ec2 = boto3.client("ec2", region_name="us-west-1")
vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
route_table = ec2.create_route_table(VpcId=vpc["Vpc"]["VpcId"])
vpc_end_point = ec2.create_vpc_endpoint(
VpcId=vpc["Vpc"]["VpcId"],
ServiceName="com.amazonaws.us-east-1.s3",
RouteTableIds=[route_table["RouteTable"]["RouteTableId"]],
VpcEndpointType="gateway",
)
vpc_endpoints = ec2.describe_vpc_endpoints()
assert vpc_endpoints.get("VpcEndpoints")[0].get(
"VpcEndpointId"
) == vpc_end_point.get("VpcEndpoint").get("VpcEndpointId")
assert vpc_endpoints.get("VpcEndpoints")[0].get("VpcId") == vpc["Vpc"]["VpcId"]
assert vpc_endpoints.get("VpcEndpoints")[0].get("RouteTableIds") == [
route_table.get("RouteTable").get("RouteTableId")
]
assert "VpcEndpointType" in vpc_endpoints.get("VpcEndpoints")[0]
assert "ServiceName" in vpc_endpoints.get("VpcEndpoints")[0]
assert "State" in vpc_endpoints.get("VpcEndpoints")[0]
vpc_endpoints = ec2.describe_vpc_endpoints(
VpcEndpointIds=[vpc_end_point.get("VpcEndpoint").get("VpcEndpointId")]
)
assert vpc_endpoints.get("VpcEndpoints")[0].get(
"VpcEndpointId"
) == vpc_end_point.get("VpcEndpoint").get("VpcEndpointId")
assert vpc_endpoints.get("VpcEndpoints")[0].get("VpcId") == vpc["Vpc"]["VpcId"]
assert vpc_endpoints.get("VpcEndpoints")[0].get("RouteTableIds") == [
route_table.get("RouteTable").get("RouteTableId")
]
assert "VpcEndpointType" in vpc_endpoints.get("VpcEndpoints")[0]
assert "ServiceName" in vpc_endpoints.get("VpcEndpoints")[0]
assert "State" in vpc_endpoints.get("VpcEndpoints")[0]
try:
ec2.describe_vpc_endpoints(
VpcEndpointIds=[route_table.get("RouteTable").get("RouteTableId")]
)
except ClientError as err:
assert err.response["Error"]["Code"] == "InvalidVpcEndPointId.NotFound"