astronomer / terraform-provider-astro

Astro Terraform Provider
https://registry.terraform.io/providers/astronomer/astro/latest
Other
11 stars 4 forks source link

[Bug]: is_development_mode and scaling_spec parametrized not working #168

Open HongVietLE opened 5 days ago

HongVietLE commented 5 days ago

Description

I'm trying to create 2 astro deployments (dev and prod) using a variable

The behavior is the same when using a for_each loop on a set of string

Steps To Reproduce

Working ✅

terraform init
terraform plan
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "3.113.0"
    }
    astro = {
      source  = "astronomer/astro"
      version = "1.0.0"
    }
  }
  required_version = "1.9.4"

}

provider "azurerm" {
  # Configuration options
  features {}
}

provider "astro" {
  organization_id = "xxx"
  token           = "xxx"
}

locals {
  is_development_mode_by_env = {
    dev  = true
    prod = false
  }
  development_mode_scaling_spec = {
    hibernation_spec = {
      schedules = [
        {
          wake_at_cron      = "0 7 * * 1"
          is_enabled        = true
          hibernate_at_cron = "0 17 * * 5"
        },
        {
          wake_at_cron      = "0 7 * * 2-5"
          is_enabled        = true
          hibernate_at_cron = "0 17 * * 1-4"
        }
      ]
    }
  }
}

resource "astro_deployment" "main" {

  is_development_mode = local.is_development_mode_by_env["dev"]
  scaling_spec        = local.is_development_mode_by_env["dev"] ? local.development_mode_scaling_spec : null
  workspace_id            = "xxxxxxxxxxxxxxxxxxxxxxxxx"

  name                    = "deploiement"
  description             = "description"
  type                    = "STANDARD"
  region                  = "westeurope"
  cloud_provider          = "AZURE"
  contact_emails          = ["john@doe.com"]
  default_task_pod_cpu    = 1
  default_task_pod_memory = 2
  executor                = "CELERY"
  is_cicd_enforced        = false
  is_dag_deploy_enabled   = true
  is_high_availability    = false
  resource_quota_cpu      = 10
  resource_quota_memory   = "20Gi"
  scheduler_size          = "SMALL"
  environment_variables   = []
  worker_queues = [{
    name               = "default"
    is_default         = true
    astro_machine      = "A5"
    max_worker_count   = 10
    min_worker_count   = 0
    worker_concurrency = 4
  }]
}

Not working :x:

terraform init
terraform plan
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "3.113.0"
    }
    astro = {
      source  = "astronomer/astro"
      version = "1.0.0"
    }
  }
  required_version = "1.9.4"

}

provider "azurerm" {
  # Configuration options
  features {}
}

provider "astro" {
  organization_id = "xxx"
  token           = "xxx"
}

variable "env" {
  type    = string
  default = "dev"
}

locals {
  is_development_mode_by_env = {
    dev  = true
    prod = false
  }
  development_mode_scaling_spec = {
    hibernation_spec = {
      schedules = [
        {
          wake_at_cron      = "0 7 * * 1"
          is_enabled        = true
          hibernate_at_cron = "0 17 * * 5"
        },
        {
          wake_at_cron      = "0 7 * * 2-5"
          is_enabled        = true
          hibernate_at_cron = "0 17 * * 1-4"
        }
      ]
    }
  }
}

resource "astro_deployment" "main" {

  is_development_mode = local.is_development_mode_by_env[var.env]
  scaling_spec        = local.is_development_mode_by_env[var.env] ? local.development_mode_scaling_spec : null
  workspace_id            = "xxxxxxxxxxxxxxxxxxxxxxxxx"

  name                    = "deploiement"
  description             = "description"
  type                    = "STANDARD"
  region                  = "westeurope"
  cloud_provider          = "AZURE"
  contact_emails          = ["john@doe.com"]
  default_task_pod_cpu    = 1
  default_task_pod_memory = 2
  executor                = "CELERY"
  is_cicd_enforced        = false
  is_dag_deploy_enabled   = true
  is_high_availability    = false
  resource_quota_cpu      = 10
  resource_quota_memory   = "20Gi"
  scheduler_size          = "SMALL"
  environment_variables   = []
  worker_queues = [{
    name               = "default"
    is_default         = true
    astro_machine      = "A5"
    max_worker_count   = 10
    min_worker_count   = 0
    worker_concurrency = 4
  }]
}

Output

╷
│ Error: scaling_spec (hibernation) is only supported for is_development_mode set to true
│ 
│   with astro_deployment.main,
│   on main_astro.tf line 29, in resource "astro_deployment" "main":
│   29: resource "astro_deployment" "main" {
│ 
│ Either set is_development_mode to true or remove scaling_spec
╵

Acceptance Criteria

No response

Anything else?

No response

blaivs commented 2 days ago

Hi, I'm facing the same problem. I did debug it quickly and looks like when validation is first called, both IsDevelopmentMode and ScalingSpec values are unknown. Probably adding a check for skipping validation if ScalingSpec is unknown would suffice, as subsequent validations will eventually have the value set.

Just adding !data.ScalingSpec.IsUnknown() check on https://github.com/astronomer/terraform-provider-astro/blob/40a473711c32ea1410f693000b0a0feb6889a833/internal/provider/resources/resource_deployment.go#L876 and https://github.com/astronomer/terraform-provider-astro/blob/40a473711c32ea1410f693000b0a0feb6889a833/internal/provider/resources/resource_deployment.go#L884