#!/bin/bash -e
# Copyright (c) 2017 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

mod=""
target="$(dirname "$0")/cluster.tf"
base="$(dirname "$0")/..$mod/modules/instance"

defs="variables.tf"
complex_inputs=( dns_nameservers additional_domains )
inputs=( )

#echo "inputs: $defs"

contains()
{
  local i
  local e="$1"
  shift
  for i; do
    if [ "$i" == "$e" ]; then
      return 0
    fi
  done
  return 1
}

print_args()
{
  for l in "${inputs[@]}"; do
    echo "  $l = \"\${var.$l}\""
  done
}

variables()
{
  for v in $defs; do
    cat "$base/$v"
  done
}

for v in $defs; do
  inputs+=( $(grep "variable[[:space:]]" "$base/$v" | sed -e 's/.*"\(.*\)".*/\1/') )
done

cat >"$target" <<EOF
##############################################################################
#
# DO NOT MODIFY MANUALLY
#
# general cluster configuration handling
# generated by variants/create.sh based on linked variables.tf found
# in module instance.
#
# DO NOT MODIFY MANUALLY
#
##############################################################################

variable "versions" {
  type = "map"
  default = {}
}

variable "etcd_backup" {
  type = "map"
  default = {
    storage_type = "pv"
  }
}
variable "dns" {
  type = "map"
}
variable "route53_access_key" {
  default = ""
}
variable "route53_secret_key" {
  default = ""
}
variable "route53_hosted_zone_id" {
  default = ""
}
locals {
  hosted_zone_id = "\${var.route53_hosted_zone_id == "" ? lookup(var.dns, "hosted_zone_id", "") : var.route53_hosted_zone_id}"
  dns = "\${merge(var.dns, map("hosted_zone_id", local.hosted_zone_id))}"
}

module "route53" {
  source = "../../modules/access/aws"
  defaults = {
    region = "us-east-1"
  }
  access_info = "\${local.dns}"

  access_key = "\${var.route53_access_key}"
  secret_key = "\${var.route53_secret_key}"
}

module "dns" {
  source = "../../modules/condmap"
  if = "\${lookup(local.dns,"dns_type","") == "route53"}"
  then = "\${merge(local.dns,module.route53.access_info)}"
  else = "\${local.dns}"
}

module "s3_etcd_backup" {
  source = "../../modules/access/aws"
  defaults = "\${module.route53.access_info}"
  access_info = "\${var.etcd_backup}"
}

provider "aws" {
  alias      = "route53"
  access_key = "\${module.route53.access_key}"
  secret_key = "\${module.route53.secret_key}"
  region     = "\${module.route53.region}"
}
provider "aws" {
  alias      = "s3_etcd_backup"
  access_key = "\${module.s3_etcd_backup.access_key}"
  secret_key = "\${module.s3_etcd_backup.secret_key}"
  region     = "\${module.s3_etcd_backup.region}"
}

locals {
  access_info = {
    route53_dns    = "\${module.route53.access_info}"
    s3_etcd_backup = "\${module.s3_etcd_backup.access_info}"
  }
}

$(variables)

#
# create cluster by calling main module
#
module "instance" {
  source = "../..$mod/modules/instance"

  iaas_config = "\${module.iaas_config.iaas_config}"
  versions    = "\${var.versions}"

  platform = "\${local.platform}"

  access_info = "\${local.access_info}"
  etcd_backup = "\${var.etcd_backup}"
  cluster-lb = "\${var.cluster-lb}"
  dns = "\${module.dns.value}"
$(print_args)
}

#
# standard output required by utility scripts
#
output "bastion" {
  value = "\${module.instance.bastion}"
}
output "bastion_user" {
  value = "\${module.instance.bastion_user}"
}


output "master_roll_info" {
  value = "\${module.instance.master_roll_info}"
}
output "worker_roll_info" {
  value = "\${module.instance.worker_roll_info}"
}

output "master" {
  value = "\${module.instance.master}"
}
output "master_ips" {
  value = "\${module.instance.master_ips}"
}
output "master_count" {
  value = "\${module.instance.master_count}"
}

output "worker" {
  value = "\${module.instance.worker}"
}
output "worker_ips" {
  value = "\${module.instance.worker_ips}"
}
output "worker_count" {
  value = "\${module.instance.worker_count}"
}

output "etcd_service_ip" {
  value = "\${module.instance.etcd_service_ip}"
}
output "structure-version" {
  value = "\${module.instance.structure-version}"
}
EOF
