resource "random_password" "this" {
  length           = 14
  special          = false
  override_special = "!#$%&*-_=+[]{}<>:?"
}

resource "aws_mq_broker" "this" {
  provider = aws.this

  broker_name                = "${var.name_prefix}-${var.name_suffix}"
  engine_type                = "RabbitMQ"
  engine_version             = var.engine_version
  host_instance_type         = var.host_instance_type
  publicly_accessible        = var.publicly_accessible
  storage_type               = "ebs"
  subnet_ids                 = local.subnet_ids
  deployment_mode            = var.deployment_mode
  auto_minor_version_upgrade = true
  security_groups = [
    aws_security_group.this.id,
  ]

  logs {
    general = true
  }

  user {
    username = "admin"                     // the password is required to create a resource,
    password = random_password.this.result // will be changed after creation, ignored via lifecycle
  }

  tags = {
    service = "Amazon MQ"
  }

  lifecycle {
    ignore_changes = [engine_version]
  }
}
