{% extends "aws/base_resource.tf.j2" %}
{% block resources %}
{% for subnet in resources %}
{{ resource_spacer() }}
resource "aws_subnet" "{{ subnet.SubnetId | strip_id_prefix | tf_resource_name }}" {
  vpc_id                  = "{{ subnet.VpcId }}"
  cidr_block              = "{{ subnet.CidrBlock }}"
  
  {% if subnet.get('AvailabilityZone') and not subnet.get('AvailabilityZoneId') %}
  availability_zone       = "{{ subnet.AvailabilityZone }}"
  {%  endif %}
  
  {% if subnet.get('AvailabilityZoneId') %}
  availability_zone_id    = "{{ subnet.AvailabilityZoneId }}"
  {%  endif %}
  
  {% if subnet.get('MapPublicIpOnLaunch') is not none %}
  map_public_ip_on_launch = {{ subnet.MapPublicIpOnLaunch | lower }}
  {%  endif %}

  {% if subnet.get('AssignIpv6AddressOnCreation') is not none %}
  assign_ipv6_address_on_creation = {{ subnet.AssignIpv6AddressOnCreation | lower }}
  {%  endif %}

  {% if subnet.get('Ipv6CidrBlockAssociationSet') %}
  ipv6_cidr_block         = "{{ subnet.Ipv6CidrBlockAssociationSet[0].Ipv6CidrBlock }}"
  {%  endif %}

  {% if subnet.get('OutpostArn') %}
  outpost_arn             = "{{ subnet.OutpostArn }}"
  {%  endif %}

  # Customer-owned IP configuration - only set if all required values are present
  {% if subnet.get('CustomerOwnedIpv4Pool') and subnet.get('OutpostArn') and subnet.get('MapCustomerOwnedIpOnLaunch') is not none %}
  customer_owned_ipv4_pool = "{{ subnet.CustomerOwnedIpv4Pool }}"
  map_customer_owned_ip_on_launch = {{ subnet.MapCustomerOwnedIpOnLaunch | lower }}
  {%  endif %}

  {% if subnet.get('EnableDns64') is not none %}
  enable_dns64 = {{ subnet.EnableDns64 | lower }}
  {%  endif %}

  {% if subnet.get('EnableResourceNameDnsARecordOnLaunch') is not none %}
  enable_resource_name_dns_a_record_on_launch = {{ subnet.EnableResourceNameDnsARecordOnLaunch | lower }}
  {%  endif %}

  {% if subnet.get('EnableResourceNameDnsAAAARecordOnLaunch') is not none %}
  enable_resource_name_dns_aaaa_record_on_launch = {{ subnet.EnableResourceNameDnsAAAARecordOnLaunch | lower }}
  {%  endif %}

  {% if subnet.get('Ipv6Native') is not none %}
  ipv6_native = {{ subnet.Ipv6Native | lower }}
  {%  endif %}

  {% if subnet.get('PrivateDnsNameOptionsOnLaunch') and subnet.PrivateDnsNameOptionsOnLaunch.get('HostnameType') %}
  private_dns_hostname_type_on_launch = "{{ subnet.PrivateDnsNameOptionsOnLaunch.get('HostnameType', 'ip-name') }}"
  {%  endif %}

  {{ render_common_tags(subnet, indent=2) }}
}

{% endfor %}
{% endblock %}
