#!/usr/bin/env bash
# Configure the dynect backend

# Requirements:
# An active DynECT account / contract will be requied to use this DevStack
# plugin.

# Enable with:
# DESIGNATE_BACKEND_DRIVER=dynect

# Dependencies:
# ``functions`` file
# ``designate`` configuration

# install_designate_backend - install any external requirements
# configure_designate_backend - make configuration changes, including those to other services
# init_designate_backend - initialize databases, etc.
# start_designate_backend - start any external services
# stop_designate_backend - stop any external services
# cleanup_designate_backend - remove transient data and cache

# Save trace setting
DP_DYNECT_XTRACE=$(set +o | grep xtrace)
set +o xtrace

# Defaults
# --------

DESIGNATE_DYNECT_CUSTOMER=${DESIGNATE_DYNECT_CUSTOMER:-customer}
DESIGNATE_DYNECT_USERNAME=${DESIGNATE_DYNECT_USERNAME:-username}
DESIGNATE_DYNECT_PASSWORD=${DESIGNATE_DYNECT_PASSWORD:-password}
DESIGNATE_DYNECT_CONTACT_NICKNAME=${DESIGNATE_DYNECT_CONTACT_NICKNAME:-}
DESIGNATE_DYNECT_JOB_TIMEOUT=${DESIGNATE_DYNECT_JOB_TIMEOUT:-}
DESIGNATE_DYNECT_TIMEOUT=${DESIGNATE_DYNECT_TIMEOUT:-}
DESIGNATE_DYNECT_MASTERS=${DESIGNATE_DYNECT_MASTERS:-"$DESIGNATE_SERVICE_HOST:$DESIGNATE_SERVICE_PORT_MDNS"}
DESIGNATE_DYNECT_NAMESERVERS=${DESIGNATE_DYNECT_NAMESERVERS:-""}
DESIGNATE_DYNECT_ALSO_NOTIFIES=${DESIGNATE_DYNECT_ALSO_NOTIFIES:-"204.13.249.65,208.78.68.65"}

# Pull in DESIGNATE_3RDPARTY_CREDS user/pass if set
if [ -n "$DESIGNATE_3RDPARTY_CREDS" ]; then
    DESIGNATE_DYNECT_CUSTOMER=`echo $DESIGNATE_3RDPARTY_CREDS | cut -f1 -d:`
    DESIGNATE_DYNECT_USERNAME=`echo $DESIGNATE_3RDPARTY_CREDS | cut -f2 -d:`
    DESIGNATE_DYNECT_PASSWORD=`echo $DESIGNATE_3RDPARTY_CREDS | cut -f3- -d:`
fi

# Sanity Checks
# -------------
if [ -z "$DESIGNATE_DYNECT_NAMESERVERS" ]; then
    die $LINENO "You must configure DESIGNATE_DYNECT_NAMESERVERS"
fi

if [ "$DESIGNATE_SERVICE_PORT_MDNS" != "53" ]; then
    die $LINENO "DynECT requires DESIGNATE_SERVICE_PORT_MDNS is set to '53'"
fi

# Entry Points
# ------------

# install_designate_backend - install any external requirements
function install_designate_backend {
    :
}

# configure_designate_backend - make configuration changes, including those to other services
function configure_designate_backend {
    # Generate Designate pool.yaml file
    sudo tee $DESIGNATE_CONF_DIR/pools.yaml > /dev/null <<EOF
---
- name: default
  description: DevStack DynECT Pool
  attributes: {}

  targets:
    - type: dynect
      description: DynECT API

      options:
        customer_name: $DESIGNATE_DYNECT_CUSTOMER
        username: $DESIGNATE_DYNECT_USERNAME
        password: $DESIGNATE_DYNECT_PASSWORD

      masters:
EOF

    # Create a Pool Master for each of the DynECT Masters
    IFS=',' read -a masters <<< "$DESIGNATE_DYNECT_MASTERS"

    for master in "${masters[@]}"; do
        sudo tee -a $DESIGNATE_CONF_DIR/pools.yaml > /dev/null <<EOF
        - host: $master
          port: 53
EOF
    done

    # Create a Pool NS Record for each of the DynECT Nameservers
    IFS=',' read -a nameservers <<< "$DESIGNATE_DYNECT_NAMESERVERS"

    sudo tee -a $DESIGNATE_CONF_DIR/pools.yaml > /dev/null <<EOF
  ns_records:
EOF

    for nameserver in "${nameservers[@]}"; do
        sudo tee -a $DESIGNATE_CONF_DIR/pools.yaml > /dev/null <<EOF
    - hostname: $nameserver
      priority: 1
EOF
    done

    # Create a Pool Nameserver for each of the DynECT Nameservers
    sudo tee -a $DESIGNATE_CONF_DIR/pools.yaml > /dev/null <<EOF
  nameservers:
EOF

    for nameserver in "${nameservers[@]}"; do
        sudo tee -a $DESIGNATE_CONF_DIR/pools.yaml > /dev/null <<EOF
    - host: `dig +short A $nameserver | head -n 1`
      port: 53
EOF
    done

    # Create a Pool Also Notifies for each of the DynECT Also Notifies
    IFS=',' read -a also_notifies <<< "$DESIGNATE_DYNECT_ALSO_NOTIFIES"

    sudo tee -a $DESIGNATE_CONF_DIR/pools.yaml > /dev/null <<EOF
  also_notifies:
EOF

    for also_notify in "${also_notifies[@]}"; do
        sudo tee -a $DESIGNATE_CONF_DIR/pools.yaml > /dev/null <<EOF
    - host: $also_notify
      port: 53
EOF
    done
}

# init_designate_backend - initialize databases, etc.
function init_designate_backend {
    :
}

# start_designate_backend - start any external services
function start_designate_backend {
    :
}

# stop_designate_backend - stop any external services
function stop_designate_backend {
    :
}

# cleanup_designate_backend - remove transient data and cache
function cleanup_designate_backend {
    :
}

# Restore xtrace
$DP_DYNECT_XTRACE
