resource "aws_iam_user" "this" {
  name = var.aws_username
  tags = var.tags
}

resource "aws_iam_user_policy_attachment" "this" {
  user       = aws_iam_user.this.name
  policy_arn = var.policy_arn
}

resource "time_rotating" "this" {
  rotation_days = var.rotation_days
}

resource "time_static" "this" {
  rfc3339 = time_rotating.this.rfc3339
}

resource "aws_iam_access_key" "this" {
  user = aws_iam_user.this.name
  lifecycle {
    replace_triggered_by = [time_static.this]
  }
}

resource "time_sleep" "this" {
  create_duration = var.update_delay

  triggers = {
    access_key = aws_iam_access_key.this.id
    secret_key = aws_iam_access_key.this.secret
  }
}

resource "octopusdeploy_aws_elastic_container_registry" "this" {
  access_key = time_sleep.this.triggers["access_key"]
  secret_key = time_sleep.this.triggers["secret_key"]
  region     = var.region #"us-east-1"
  name       = "ECR ${var.profile}"
}
