locals {
  # If you want to use any of the variables in _this_ file, you have to load them here.
  # However, they will all be available as inputs to the module loaded in terraform.source
  # below.

  environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
  partition_vars = read_terragrunt_config(find_in_parent_folders("partition.hcl"))
  region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))
  account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
  global_vars = read_terragrunt_config(find_in_parent_folders("globals.hcl"))

  aws_partition  = local.partition_vars.locals.aws_partition
  account_id     = local.account_vars.locals.aws_account_id
  common_profile = local.partition_vars.locals.common_profile

  target_aws_region = "us-gov-west-1"
}

# Terragrunt will copy the Terraform configurations specified by the source parameter, along with any files in the
# working directory, into a temporary folder, and execute your Terraform commands in that folder.
terraform {
  # Double slash is intentional and required to show root of modules
  source = "git@github.xdr.accenturefederalcyber.com:mdr-engineering/xdr-terraform-modules.git//base/account_standards_regional?ref=v1.21.7"
}

# Include all settings from the root terragrunt.hcl file
include {
  path = find_in_parent_folders()
}

############# Custom provider for the region
generate "provider" {
  path      = "provider.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
provider "aws" {
  region = "${local.target_aws_region}"

  assume_role {
    role_arn = "arn:${local.aws_partition}:iam::${local.account_id}:role/user/mdr_terraformer"
    session_name = "terraform"
  }

  profile = "${local.common_profile}"

  # Only these AWS Account IDs may be operated on by this template
  allowed_account_ids = ["${local.account_id}"]
}
EOF
}

# These are the variables we have to pass in to use the module specified in the terragrunt source above
inputs = {
  # All of the inputs from the inherited hcl files are available automatically
  # (via the `inputs` section of the root `terragrunt.hcl`). However, modules
  # will be more flexible if you specify particular input values.
  tags = {
    Terraform = "aws/${basename(get_parent_terragrunt_dir())}/${path_relative_to_include()}/"
  }
}
