from moto.ec2.utils import add_tag_specification from ._base_response import EC2BaseResponse class TransitGatewayAttachment(EC2BaseResponse): def create_transit_gateway_vpc_attachment(self): options = self._get_multi_param_dict("Options") subnet_ids = self._get_multi_param("SubnetIds") transit_gateway_id = self._get_param("TransitGatewayId") vpc_id = self._get_param("VpcId") tags = self._get_multi_param("TagSpecifications") tags = tags[0] if isinstance(tags, list) and len(tags) == 1 else tags tags = (tags or {}).get("Tag", []) tags = {t["Key"]: t["Value"] for t in tags} transit_gateway_attachment = ( self.ec2_backend.create_transit_gateway_vpc_attachment( transit_gateway_id=transit_gateway_id, tags=tags, vpc_id=vpc_id, subnet_ids=subnet_ids, options=options, ) ) template = self.response_template(CREATE_TRANSIT_GATEWAY_VPC_ATTACHMENT) return template.render(transit_gateway_attachment=transit_gateway_attachment) def describe_transit_gateway_vpc_attachments(self): transit_gateways_attachment_ids = self._get_multi_param( "TransitGatewayAttachmentIds" ) filters = self._filters_from_querystring() transit_gateway_vpc_attachments = ( self.ec2_backend.describe_transit_gateway_vpc_attachments( transit_gateways_attachment_ids=transit_gateways_attachment_ids, filters=filters, ) ) template = self.response_template(DESCRIBE_TRANSIT_GATEWAY_VPC_ATTACHMENTS) return template.render( transit_gateway_vpc_attachments=transit_gateway_vpc_attachments ) def modify_transit_gateway_vpc_attachment(self): add_subnet_ids = self._get_multi_param("AddSubnetIds") options = self._get_multi_param_dict("Options") remove_subnet_ids = self._get_multi_param("RemoveSubnetIds") transit_gateway_attachment_id = self._get_param("TransitGatewayAttachmentId") transit_gateway_attachment = ( self.ec2_backend.modify_transit_gateway_vpc_attachment( add_subnet_ids=add_subnet_ids, options=options, remove_subnet_ids=remove_subnet_ids, transit_gateway_attachment_id=transit_gateway_attachment_id, ) ) template = self.response_template(MODIFY_TRANSIT_GATEWAY_VPC_ATTACHMENTS) return template.render(transit_gateway_attachment=transit_gateway_attachment) def describe_transit_gateway_attachments(self): transit_gateways_attachment_ids = self._get_multi_param( "TransitGatewayAttachmentIds" ) filters = self._filters_from_querystring() transit_gateway_attachments = ( self.ec2_backend.describe_transit_gateway_attachments( transit_gateways_attachment_ids=transit_gateways_attachment_ids, filters=filters, ) ) template = self.response_template(DESCRIBE_TRANSIT_GATEWAY_ATTACHMENTS) return template.render(transit_gateway_attachments=transit_gateway_attachments) def delete_transit_gateway_vpc_attachment(self): transit_gateway_attachment_id = self._get_param("TransitGatewayAttachmentId") transit_gateway_attachment = ( self.ec2_backend.delete_transit_gateway_vpc_attachment( transit_gateway_attachment_id=transit_gateway_attachment_id ) ) template = self.response_template(DELETE_TRANSIT_GATEWAY_VPC_ATTACHMENTS) return template.render(transit_gateway_attachment=transit_gateway_attachment) def associate_transit_gateway_route_table(self): transit_gateway_attachment_id = self._get_param("TransitGatewayAttachmentId") transit_gateway_route_table_id = self._get_param("TransitGatewayRouteTableId") transit_gateway_association = ( self.ec2_backend.associate_transit_gateway_route_table( transit_gateway_attachment_id=transit_gateway_attachment_id, transit_gateway_route_table_id=transit_gateway_route_table_id, ) ) template = self.response_template(TRANSIT_GATEWAY_ASSOCIATION) return template.render(transit_gateway_association=transit_gateway_association) def disassociate_transit_gateway_route_table(self): tgw_attach_id = self._get_param("TransitGatewayAttachmentId") tgw_rt_id = self._get_param("TransitGatewayRouteTableId") tgw_association = self.ec2_backend.disassociate_transit_gateway_route_table( tgw_attach_id, tgw_rt_id ) template = self.response_template(TRANSIT_GATEWAY_DISASSOCIATION) return template.render(tgw_association=tgw_association) def enable_transit_gateway_route_table_propagation(self): transit_gateway_attachment_id = self._get_param("TransitGatewayAttachmentId") transit_gateway_route_table_id = self._get_param("TransitGatewayRouteTableId") transit_gateway_propagation = ( self.ec2_backend.enable_transit_gateway_route_table_propagation( transit_gateway_attachment_id=transit_gateway_attachment_id, transit_gateway_route_table_id=transit_gateway_route_table_id, ) ) template = self.response_template(TRANSIT_GATEWAY_PROPAGATION) return template.render(transit_gateway_propagation=transit_gateway_propagation) def disable_transit_gateway_route_table_propagation(self): transit_gateway_attachment_id = self._get_param("TransitGatewayAttachmentId") transit_gateway_route_table_id = self._get_param("TransitGatewayRouteTableId") transit_gateway_propagation = ( self.ec2_backend.disable_transit_gateway_route_table_propagation( transit_gateway_attachment_id=transit_gateway_attachment_id, transit_gateway_route_table_id=transit_gateway_route_table_id, ) ) template = self.response_template(TRANSIT_GATEWAY_PROPAGATION) return template.render(transit_gateway_propagation=transit_gateway_propagation) def create_transit_gateway_peering_attachment(self): peer_account_id = self._get_param("PeerAccountId") peer_region = self._get_param("PeerRegion") peer_transit_gateway_id = self._get_param("PeerTransitGatewayId") transit_gateway_id = self._get_param("TransitGatewayId") tags = add_tag_specification(self._get_multi_param("TagSpecification")) transit_gateway_peering_attachment = ( self.ec2_backend.create_transit_gateway_peering_attachment( transit_gateway_id, peer_transit_gateway_id, peer_region, peer_account_id, tags, ) ) template = self.response_template(TRANSIT_GATEWAY_PEERING_ATTACHMENT) return template.render( method_name="CreateTransitGatewayPeeringAttachment", transit_gateway_peering_attachment=transit_gateway_peering_attachment, ) def describe_transit_gateway_peering_attachments(self): transit_gateways_attachment_ids = self._get_multi_param( "TransitGatewayAttachmentIds" ) filters = self._filters_from_querystring() transit_gateway_peering_attachments = ( self.ec2_backend.describe_transit_gateway_peering_attachments( transit_gateways_attachment_ids=transit_gateways_attachment_ids, filters=filters, ) ) template = self.response_template(DESCRIBE_TRANSIT_GATEWAY_PEERING_ATTACHMENTS) return template.render( transit_gateway_peering_attachments=transit_gateway_peering_attachments ) def accept_transit_gateway_peering_attachment(self): transit_gateway_attachment_id = self._get_param("TransitGatewayAttachmentId") transit_gateway_peering_attachment = ( self.ec2_backend.accept_transit_gateway_peering_attachment( transit_gateway_attachment_id=transit_gateway_attachment_id ) ) template = self.response_template(TRANSIT_GATEWAY_PEERING_ATTACHMENT) return template.render( method_name="AcceptTransitGatewayPeeringAttachment", transit_gateway_peering_attachment=transit_gateway_peering_attachment, ) def delete_transit_gateway_peering_attachment(self): transit_gateway_attachment_id = self._get_param("TransitGatewayAttachmentId") transit_gateway_peering_attachment = ( self.ec2_backend.delete_transit_gateway_peering_attachment( transit_gateway_attachment_id=transit_gateway_attachment_id ) ) template = self.response_template(TRANSIT_GATEWAY_PEERING_ATTACHMENT) return template.render( method_name="DeleteTransitGatewayPeeringAttachment", transit_gateway_peering_attachment=transit_gateway_peering_attachment, ) def reject_transit_gateway_peering_attachment(self): transit_gateway_attachment_id = self._get_param("TransitGatewayAttachmentId") transit_gateway_peering_attachment = ( self.ec2_backend.reject_transit_gateway_peering_attachment( transit_gateway_attachment_id=transit_gateway_attachment_id ) ) template = self.response_template(TRANSIT_GATEWAY_PEERING_ATTACHMENT) return template.render( method_name="RejectTransitGatewayPeeringAttachment", transit_gateway_peering_attachment=transit_gateway_peering_attachment, ) CREATE_TRANSIT_GATEWAY_VPC_ATTACHMENT = """ 9b5766ac-2af6-4b92-9a8a-4d74ae46ae79 {{ transit_gateway_attachment.create_time }} {{ transit_gateway_attachment.options.ApplianceModeSupport }} {{ transit_gateway_attachment.options.DnsSupport }} {{ transit_gateway_attachment.options.Ipv6Support }} {{ transit_gateway_attachment.state }} {% for subnet_id in transit_gateway_attachment.subnet_ids %} {{ subnet_id }} {% endfor %} {% for tag in transit_gateway_attachment.get_tags() %} {{ tag.key }} {{ tag.value }} {% endfor %} {{ transit_gateway_attachment.id }} {{ transit_gateway_attachment.transit_gateway_id }} {{ transit_gateway_attachment.vpc_id }} {{ transit_gateway_attachment.resource_owner_id }} """ DESCRIBE_TRANSIT_GATEWAY_ATTACHMENTS = """ 92aa7885-74c0-42d1-a846-e59bd07488a7 {% for transit_gateway_attachment in transit_gateway_attachments %} associated tgw-rtb-0b36edb9b88f0d5e3 2021-07-18T08:57:21.000Z {{ transit_gateway_attachment.resource_id }} {{ transit_gateway_attachment.resource_owner_id }} {{ transit_gateway_attachment.resource_type }} {{ transit_gateway_attachment.state }} {% for tag in transit_gateway_attachment.get_tags() %} {{ tag.key }} {{ tag.value }} {% endfor %} {{ transit_gateway_attachment.id }} {{ transit_gateway_attachment.transit_gateway_id }} {{ transit_gateway_attachment.resource_owner_id }} {% endfor %} """ DESCRIBE_TRANSIT_GATEWAY_VPC_ATTACHMENTS = """ bebc9670-0205-4f28-ad89-049c97e46633 {% for transit_gateway_vpc_attachment in transit_gateway_vpc_attachments %} 2021-07-18T08:57:21.000Z {% if transit_gateway_vpc_attachment.options %} {{ transit_gateway_vpc_attachment.options.ApplianceModeSupport }} {{ transit_gateway_vpc_attachment.options.DnsSupport }} {{ transit_gateway_vpc_attachment.options.Ipv6Support }} {% endif %} {{ transit_gateway_vpc_attachment.state }} {% for id in transit_gateway_vpc_attachment.subnet_ids %} {{ id }} {% endfor %} {% for tag in transit_gateway_vpc_attachment.get_tags() %} {{ tag.key }} {{ tag.value }} {% endfor %} {{ transit_gateway_vpc_attachment.id }} {{ transit_gateway_vpc_attachment.transit_gateway_id }} {{ transit_gateway_vpc_attachment.vpc_id }} {{ transit_gateway_vpc_attachment.resource_owner_id }} {% endfor %} """ MODIFY_TRANSIT_GATEWAY_VPC_ATTACHMENTS = """ 9b5766ac-2af6-4b92-9a8a-4d74ae46ae79 {{ transit_gateway_attachment.create_time }} {{ transit_gateway_attachment.options.ApplianceModeSupport }} {{ transit_gateway_attachment.options.DnsSupport }} {{ transit_gateway_attachment.options.Ipv6Support }} {{ transit_gateway_attachment.state }} {% for subnet_id in transit_gateway_attachment.subnet_ids %} {{ subnet_id }} {% endfor %} {% for tag in transit_gateway_attachment.get_tags() %} {{ tag.key }} {{ tag.value }} {% endfor %} {{ transit_gateway_attachment.id }} {{ transit_gateway_attachment.transit_gateway_id }} {{ transit_gateway_attachment.vpc_id }} {{ transit_gateway_attachment.resource_owner_id }} """ DELETE_TRANSIT_GATEWAY_VPC_ATTACHMENTS = """ 9b5766ac-2af6-4b92-9a8a-4d74ae46ae79 {{ transit_gateway_attachment.create_time }} {{ transit_gateway_attachment.options.ApplianceModeSupport }} {{ transit_gateway_attachment.options.DnsSupport }} {{ transit_gateway_attachment.options.Ipv6Support }} {{ transit_gateway_attachment.state }} {% for subnet_id in transit_gateway_attachment.subnet_ids %} {{ subnet_id }} {% endfor %} {% for tag in transit_gateway_attachment.get_tags() %} {{ tag.key }} {{ tag.value }} {% endfor %} {{ transit_gateway_attachment.id }} {{ transit_gateway_attachment.transit_gateway_id }} {{ transit_gateway_attachment.vpc_id }} {{ transit_gateway_attachment.resource_owner_id }} """ TRANSIT_GATEWAY_ASSOCIATION = """ 86a597cf-93ec-44a3-9559-4641863642a5 {{ transit_gateway_association.resource_id }} {{ transit_gateway_association.resource_type }} {{ transit_gateway_association.state }} {{ transit_gateway_association.transit_gateway_attachment_id }} {{ transit_gateway_association.transit_gateway_route_table_id }} """ TRANSIT_GATEWAY_DISASSOCIATION = """ 86a597cf-93ec-44a3-9559-4641863642a5 {{ tgw_association.resource_id }} {{ tgw_association.resource_type }} {{ tgw_association.state }} {{ tgw_association.transit_gateway_attachment_id }} {{ tgw_association.transit_gateway_route_table_id }} """ TRANSIT_GATEWAY_PROPAGATION = """ c78427d4-e498-46ae-bc14-32841b16bff4 {{ transit_gateway_propagation.resource_id }} {{ transit_gateway_propagation.resource_type }} {{ transit_gateway_propagation.state }} {{ transit_gateway_propagation.transit_gateway_attachment_id }} {{ transit_gateway_propagation.transit_gateway_route_table_id }} """ TRANSIT_GATEWAY_PEERING_ATTACHMENT = """<{{ method_name }} xmlns="http://ec2.amazonaws.com/doc/2016-11-15/"> 9b5766ac-2af6-4b92-9a8a-4d74ae46ae79 {{ transit_gateway_peering_attachment.create_time }} {{ transit_gateway_peering_attachment.state }} {{ transit_gateway_peering_attachment.accepter_tgw_info.ownerId or '' }} {{ transit_gateway_peering_attachment.accepter_tgw_info.region or '' }} {{ transit_gateway_peering_attachment.accepter_tgw_info.transitGatewayId or '' }} {{ transit_gateway_peering_attachment.requester_tgw_info.ownerId or '' }} {{ transit_gateway_peering_attachment.requester_tgw_info.region or '' }} {{ transit_gateway_peering_attachment.requester_tgw_info.transitGatewayId or '' }} {{ transit_gateway_peering_attachment.status.code }} {% for tag in transit_gateway_peering_attachment.get_tags() %} {{ tag.key }} {{ tag.value }} {% endfor %} {{ transit_gateway_peering_attachment.id }} """ DESCRIBE_TRANSIT_GATEWAY_PEERING_ATTACHMENTS = """ bebc9670-0205-4f28-ad89-049c97e46633 {% for transit_gateway_peering_attachment in transit_gateway_peering_attachments %} {{ transit_gateway_peering_attachment.create_time }} {{ transit_gateway_peering_attachment.state }} {% if transit_gateway_peering_attachment.accepter_tgw_info %} {{ transit_gateway_peering_attachment.accepter_tgw_info.ownerId or '' }} {{ transit_gateway_peering_attachment.accepter_tgw_info.region or '' }} {{ transit_gateway_peering_attachment.accepter_tgw_info.transitGatewayId or '' }} {% endif %} {% if transit_gateway_peering_attachment.requester_tgw_info %} {{ transit_gateway_peering_attachment.requester_tgw_info.ownerId or '' }} {{ transit_gateway_peering_attachment.requester_tgw_info.region or '' }} {{ transit_gateway_peering_attachment.requester_tgw_info.transitGatewayId or '' }} {% endif %} {% if transit_gateway_peering_attachment.status %} {{ transit_gateway_peering_attachment.status.code }} {% endif %} {% for tag in transit_gateway_peering_attachment.get_tags() %} {{ tag.key }} {{ tag.value }} {% endfor %} {{ transit_gateway_peering_attachment.id }} {% endfor %} """