variable "environment" {
  default = "dev"
}

variable "service_name" {
  default = "ows-test-with-volumes"
}

variable "aws_region" {
  default = "us-east-1"
}

variable "application_family" {
  default = "devops"
}

variable "team_name" {
  default = "devops"
}

variable "vpc_id" {
  default = "vpc-34dbfd51"
}

variable "subnet_ids" {
  default = [
    "subnet-44c19a21",
    "subnet-b649dfef",
  ]
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
  team_name          = var.team_name
}

provider "aws" {
  region  = var.aws_region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags_networking.tags
  }
}

module "default_tags_networking" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = "networking"
  application_family = var.application_family
  service_name       = var.service_name
  team_name          = var.team_name
}

data "aws_route53_zone" "route53_zone" {
  name = "${var.environment}.theorchard.io"
}

data "aws_route53_zone" "networking_route53_zone" {
  name = "${var.environment}.theorchard.io"

  provider = aws.networking
}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git?ref=1.0.0"

  environment = var.environment
}

# Create efs file system.
module "efs_volume" {
  source                   = "git@github.com:theorchard/terraform-efs.git//?ref=2.2.1"
  environment              = var.environment
  service_name             = var.service_name
  application_family       = "test"
  zone_id                  = "Z21XEY26C989RH"
  owner_gid                = "48"
  owner_uid                = "48"
  access_point_permissions = "750"
  vpc_id                   = var.vpc_id
  subnet_ids               = var.subnet_ids
  efs_ingress_additional_cidr_blocks = [
    "192.168.32.0/24",
  ]
}

# This rule will add fargate security group to efs security group ingress rule so fargate task can access efs mount.
resource "aws_security_group_rule" "allow_efs_sg_inbound" {
  from_port                = 2049
  to_port                  = 2049
  protocol                 = "tcp"
  security_group_id        = module.efs_volume.efs_security_group_id_output
  source_security_group_id = module.ows_service_environment_with_volumes.fargate_security_group_id
  type                     = "ingress"
}

module "ows_service_environment_with_volumes" {
  source = "../../../"

  providers = {
    aws.dns = aws.networking
  }

  environment              = var.environment
  service_name             = var.service_name
  application_family       = "platform"
  aws_region               = var.aws_region
  service_platform_version = "1.4.0"
  commit_sha               = "latest"
  container_port           = "8080"
  task_type                = "web_service"
  desired_task_count       = 1
  task_cpu                 = 2048
  task_memory              = 4096
  maximum_capacity         = 4
  minimum_capacity         = 1
  override_route53_zone_id = data.aws_route53_zone.route53_zone.zone_id
  docker_volumes = [
    {
      name            = "${var.environment}-${var.service_name}-efs"
      file_system_id  = module.efs_volume.efs_file_system_id_output
      access_point_id = module.efs_volume.efs_access_point_id_output
    },
    {
      name = "${var.environment}-${var.service_name}-bind-mount"
      type = "BIND_MOUNT"
    }
  ]
  docker_volume_mount_points = [
    {
      source_volume  = "${var.environment}-${var.service_name}-efs"
      container_path = "/tmp"
    },
    {
      source_volume  = "${var.environment}-${var.service_name}-bind-mount"
      container_path = "/data"
    },
  ]

  fargate_service_subnets = var.subnet_ids

  load_balancer_subnets = var.subnet_ids

  vpc_id = module.vpc_info.vpc_id

  environment_variables = [
    {
      Environment = "${var.environment}"
    },
    {
      LOGGER_DSN = "https://logger_dsn"
    },
  ]
  iam_managed_policy_attachments = [
    module.efs_volume.efs_iam_policy_arn_output,
  ]
}
