# Configure the agent backend

# Enable this pluging by adding these line to local.conf:
#
# DESIGNATE_BACKEND_DRIVER=agent
# DESIGNATE_AGENT_BACKEND_DRIVER=msdns

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

# install_designate_agent_backend - install any external requirements
# configure_designate_agent_backend - make configuration changes, including those to other services
# init_designate_agent_backend - initialize databases, etc.
# start_designate_agent_backend - start any external services
# stop_designate_agent_backend - stop any external services
# cleanup_designate_agent_backend - remove transient data and cache

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

# Defaults
# --------

DESIGNATE_MSDNS_MASTERS=${DESIGNATE_MSDNS_MASTERS:-"$DESIGNATE_SERVICE_HOST:$DESIGNATE_SERVICE_PORT_MDNS"}
DESIGNATE_MSDNS_HOST_IP=${DESIGNATE_MSDNS_HOST_IP:-}
DESIGNATE_MSDNS_HOST_PORT=${DESIGNATE_MSDNS_HOST_PORT:-}

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

if [ -z "$DESIGNATE_MSDNS_HOST_IP" ]; then
    die $LINENO "You must configure DESIGNATE_MSDNS_HOST_IP with the IP of the MS DNS host"
fi

if [ -z "$DESIGNATE_MSDNS_HOST_PORT" ]; then
    die $LINENO "You must configure DESIGNATE_MSDNS_HOST_PORT with the PORT of the MS DNS host"
fi

if [ "$DESIGNATE_SERVICE_PORT_MDNS" != "53" ]; then
    die $LINENO "Microsoft DNS requires DESIGNATE_SERVICE_PORT_MDNS to be set to '53'"
fi

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

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

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

  ns_records:
    - hostname: $DESIGNATE_DEFAULT_NS_RECORD
      priority: 1

  nameservers:
    - host: $DESIGNATE_MSDNS_HOST_IP
      port: $DESIGNATE_MSDNS_HOST_PORT

  targets:
    - type: agent
      description: MSDNS Agent Instance

      masters:
        - host: $(ipv6_unquote $DESIGNATE_SERVICE_HOST)
          port: $DESIGNATE_SERVICE_PORT_MDNS

      options:
        host: $DESIGNATE_MSDNS_HOST_IP
        port: $DESIGNATE_MSDNS_HOST_PORT
EOF

    echo "# Sample Config for Windows Agent service" | sudo tee ${DESIGNATE_CONF}.win
    echo "# This file should be copied to the Windows DNS server and used as the Designate config file" | sudo tee -a ${DESIGNATE_CONF}.win
    # Configure Agent Settings
    iniset ${DESIGNATE_CONF}.win service:agent backend_driver $DESIGNATE_AGENT_BACKEND_DRIVER
    iniset ${DESIGNATE_CONF}.win service:agent host $DESIGNATE_MSDNS_HOST_IP
    iniset ${DESIGNATE_CONF}.win service:agent port $DESIGNATE_MSDNS_HOST_PORT
    iniset ${DESIGNATE_CONF}.win service:agent masters "$DESIGNATE_MSDNS_MASTERS"
}

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

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

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

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

# Restore xtrace
$DP_AGENT_MSDNS_XTRACE
