spacelift-io / terraform-provider-spacelift

Terraform provider to interact with Spacelift
MIT License
76 stars 29 forks source link

Allow for dynamic blocks on spacelift_stack_dependency_reference #506

Closed jmch1692 closed 7 months ago

jmch1692 commented 9 months ago

Current usage

resource "spacelift_stack" "infra" {
  branch     = "master"
  name       = "Infrastructure stack"
  repository = "core-infra"
}

resource "spacelift_stack" "app" {
  branch     = "master"
  name       = "Application stack"
  repository = "app"
}

resource "spacelift_stack_dependency" "test" {
  stack_id            = spacelift_stack.app.id
  depends_on_stack_id = spacelift_stack.infra.id
}

resource "spacelift_stack_dependency_reference" "test" {
  stack_dependency_id = spacelift_stack_dependency.test.id
  output_name         = "DB_CONNECTION_STRING"
  input_name          = "APP_DB_URL"
}

#..repeat spacelift_stack_dependency_reference for every spacelift_stack_dependency_reference i need

Proposal

Allow in-body definition of multiple dependency references, in a single spacelift_stack_dependency_reference block. This should allow an easier iteration over the dependency resource, reference would enclose 1 or many actual output>input maps

resource "spacelift_stack" "infra" {
  branch     = "master"
  name       = "Infrastructure stack"
  repository = "core-infra"
}

resource "spacelift_stack" "app" {
  branch     = "master"
  name       = "Application stack"
  repository = "app"
}

resource "spacelift_stack_dependency" "test" {
  stack_id            = spacelift_stack.app.id
  depends_on_stack_id = spacelift_stack.infra.id
}

resource "spacelift_stack_dependency_reference" "test" {
  stack_dependency_id = spacelift_stack_dependency.test.id
  reference = {
     input_name = DB_CONNECTION_STRING
     output_name = APP_DB_URL
  }

  reference = {
     input_name = DB_CONNECTION_USER
     output_name = APP_DB_USER
  }

  reference = {
     input_name = DB_CONNECTION_TIMEOUT
     output_name = APP_DB_TIMEOUT
  }
  #...So, i could ultimately do something like...
  dynamic "reference" {
    for_each = var.references
    content {
      input_name = setting.references["input_name"]
      output_name = setting.references["output_name"]
    }
  }
}

#..repeat spacelift_stack_dependency_reference for every spacelift_stack_dependency_reference i need
marcinwyszynski commented 7 months ago

The reason I'd rather keep it this way is that each reference is a single API call. Having a Terraform resource depend on multiple API calls some of which succeed, and some of which can fail adds quite a bit of complexity (you'd get partially successful resources 🤮 ).

But, nothing really prevents you from using a for_each for the entire resource. Since we don't charge for resources under management, you can go wild :)