#!/bin/bash
# on_local_server creation: {{now}}

{%- for server in servers %}
  {%- if server.hostname %}
{#
  if upctl server show {{server.hostname}} >/dev/null 2>/dev/null ; then 
    echo "Server {{server.hostname}} already created."
  else
    {% if use_time and use_time == 'true'  %} time {%- endif -%}
    upctl server create \
    --hostname {{server.hostname}} \
    {%- if server.title and server.title != '' %}
    --title "{{server.title}}" \
    {%- endif -%}
    {%- if server.plan %}
    --plan {{server.plan}} \
    {%- elif defaults.plan and defaults.plan != '' %}
    --plan {{defaults.plan}} \
    {%- endif -%}
    {%- if server.zone %}
    --zone {{server.zone}} \
    {%- elif defaults.zone and defaults.zone != '' %}
    --zone {{defaults.zone}} \
    {%- endif -%}
    {%- if server.ssh_key_path %}
    --ssh-keys {{server.ssh_key_path}} \
    {%- elif defaults.ssh_key_path and defaults.ssh_key_path != '' %}
    --ssh-keys {{defaults.ssh_key_path}} \
    {%- endif -%}
    {%- if server.storage_os %}
    --os "{{server.storage_os}}" \
    {%- elif defaults.storage_os and defaults.storage_os != '' %}
    --os "{{defaults.storage_os}}" \
    {%- endif -%}
    {%- if server.storage_size %} 
    --os-storage-size {{server.storage_size}} \
    {%- elif defaults.storage_size and defaults.storage_size > 0 %} 
    --os-storage-size {{defaults.storage_size}} \
    {%- endif -%}
    {%- if server.network_public_ipv4 %}
    --network family=IPv4,type=public \
    {%- elif defaults.network_public_ipv4 and defaults.network_public_ipv4 != '' %} 
    --network family=IPv4,type=public \
    {%- endif -%}
    {%- if server.network_public_ipv6 %}
    --network family=IPv6,type=public \
    {%- elif defaults.network_public_ipv6 and defaults.network_public_ipv6 != '' %} 
    --network family=IPv6,type=public \
    {%- endif -%}
    {%- if server.network_utility_ipv4 %}
    --network family=IPv4,type=utility \
    {%- elif defaults.network_utility_ipv4 and defaults.network_utility_ipv4 != '' %} 
    --network family=IPv4,type=utility \
    {%- endif -%}
    {%- if server.network_utility_ipv6 %}
    --network family=IPv6,type=utility \
    {%- elif defaults.network_utility_ipv6 and defaults.network_utility_ipv6 != '' %} 
    --network family=IPv6,type=utility \
    {%- endif -%}
    {%- if server.network_private_ip %}
      {%- if server.network_private_id %} 
    --network type=private,network={{server.network_private_id}},ip-address={{server.network_private_ip}} \
      {%- elif defaults.network_private_id %} 
    --network type=private,network={{defaults.network_private_id}},ip-address={{server.network_private_ip}} \
      {%- endif -%}
    {%- endif -%}
    {%- if server.time_zone %}
    --time-zone {{server.time_zone}} \
    {%- elif defaults.time_zone and defaults.time_zone != '' %} 
    --time-zone {{defaults.time_zone}} \
    {%- endif -%}
    {%- if server.labels %}
    --label {{server.labels}} \
    {%- endif -%}
    {%- if defaults.labels and defaults.labels != '' %}
    --labels {{defaults.labels}} \
    {%- endif -%}
    {%- if wait %}
    --wait \
    {%- endif -%}
    {%- if runset.output_format and runset.output_format != '' %}
    -o {{runset.output_format}} \
    {%- endif %}
    --enable-metadata >> {{wk_file}}
  fi
#}
  {%- endif -%}
{%- endfor %}
