{% for endpoint in resources -%}
resource "aws_vpc_endpoint" "endpoint_{{ endpoint.name_sanitized }}" {
  vpc_id              = "{{ endpoint.VpcId }}"
  service_name        = "{{ endpoint.ServiceName }}"
  vpc_endpoint_type   = "{{ endpoint.VpcEndpointType }}"

  {%- if endpoint.get('RouteTableIds') and endpoint.is_gateway %}
  route_table_ids = [
    {%- for rt_id in endpoint.route_table_ids_formatted -%}
    "{{ rt_id }}",
    {%- endfor %}
  ]
  {%- endif %}

  {%- if endpoint.get('SubnetIds') and (endpoint.is_interface or endpoint.is_gateway_load_balancer) %}
  subnet_ids = [
    {%- for subnet_id in endpoint.subnet_ids_formatted -%}
    "{{ subnet_id }}",
    {%- endfor %}
  ]
  {%- endif %}

  {%- if endpoint.get('Groups') and (endpoint.is_interface or endpoint.is_gateway_load_balancer) %}
  security_group_ids = [
    {%- for sg_id in endpoint.security_group_ids_formatted -%}
    "{{ sg_id }}",
    {%- endfor %}
  ]
  {%- endif %}

  {%- if endpoint.get('PrivateDnsEnabled') is defined and endpoint.is_interface %}
  private_dns_enabled = {{ endpoint.PrivateDnsEnabled | lower }}
  {%- endif %}

  {%- if endpoint.get('PolicyDocument') %}
  policy = jsonencode({{ endpoint.PolicyDocument | safe }})
  {%- endif %}

  {%- if endpoint.get('State') == 'Available' %}
  auto_accept = true
  {%- endif %}

  tags = {
    {%- for key, value in endpoint.get('tags_formatted', {}).items() -%}
    "{{ key }}" = "{{ value }}"
    {%- endfor %}
  }
}
{% endfor %}