#!/bin/bash
# provisioning {{provisioning_vers}} upctl server creation: {{now}}
{%- if debug and debug == "yes" %} set -x {% endif %}
  [ -z "$UPCLOUD_USERNAME" ] && [ ! -r "$HOME/.config/upctl.yaml" ] && echo "UpCloud credentials not found"  && exit 1
{#
 {%- for server in servers %}
  {%- if server.hostname and match_server != "" and match_server != server.hostname %} {% continue %} {%- endif %}
#}
  {%- 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.storages and server.storages[0] and server.storages[0].parts and server.storages[0].parts[0] and server.storages[0].parts[0].encrypted %} 
    --os-storage-encrypt 
    {%- endif -%}
    {%- if server.storages and server.storages[0] and server.storages[0].parts and server.storages[0].parts[0] and server.storages[0].parts[0].size and server.storages[0].parts[0].size > 0  %} 
    --os-storage-size {{server.storages[0].parts[0].size}} \
    {%- elif server.storages and server.storages[0].size and server.storage[0].size > 0 %} 
    --os-storage-size {{server.storages[0].size}} \
    {%- elif defaults.storage and defaults.storages[0].size and defaults.storage[0].size > 0 %} 
    --os-storage-size {{defaults.storages[0].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 wait %}
    --wait \
    {%- endif -%}
    {%- if runset.output_format and runset.output_format != '' %}
    -o {{runset.output_format}} \
    {%- endif %}
    --enable-metadata \
    >> {{wk_file}}
  fi
  {%- endif -%}
{# %- endfor % #}
