Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Validation condition accessing other variables #26229

Closed
josh-barker opened this issue Sep 14, 2020 · 7 comments
Closed

Custom Validation condition accessing other variables #26229

josh-barker opened this issue Sep 14, 2020 · 7 comments
Labels
enhancement new new issue not yet triaged

Comments

@josh-barker
Copy link

Current Terraform Version

terraform v0.13.2

Use-cases

Hi,

We have situations where we want to create validation rules based on the value of of another variable.

For example, backup policies.

variable "frequency" { 
  type = string

  validation {
    condition = contains(["Daily", "Weekly"], var.frequecy)
    error_condition = "Must be Daily or Weekly."
  }
}

variable "retain_daily_backups" {
  type = number
  default = null

  validation {
    condition = var.frequency == "Daily" && var.retain_daily_backups >= 42 && var.retain_daily_backups < 1000
    error_condition = "Must specify daily backup value."
  }
}

variable "retain_weekly_backups" {
  type = number
  default = null

  validation {
    condition = var.frequency == "Weekly" && var.retain_weekly_backups >= 42 && var.retain_weekly_backups < 1000
    error_condition = "Must specify weekly backup value."
  }
}

Proposal

Allow validation block to access other variables apart from itself.

References

@josh-barker josh-barker added enhancement new new issue not yet triaged labels Sep 14, 2020
@apielasa
Copy link

I was just about to create this feature request. Faced the same problem as you did @josh-barker.

@aswinkm-tc
Copy link

I would also like to add accessing locals from within validation to reduce the complexity of condition statements
Example:

locals {
  storage_class = ["MULTI_REGIONAL", "REGIONAL", "NEARLINE", "COLDLINE", "ARCHIVE", "STANDARD", "DURABLE_REDUCED_AVAILABILITY"]
}
variable "storage_class" {
  type = string
  default = "STANDARD"
  validation {
    condition = contains(local.storage_class, var.storage_class)
    error_message = "Supported values include: MULTI_REGIONAL, REGIONAL, NEARLINE, COLDLINE, ARCHIVE, STANDARD, DURABLE_REDUCED_AVAILABILITY."
  }
}

rather than writing again and again

variable "storage_class" {
  type = string
  default = "STANDARD"
  validation {
    condition = contains(["MULTI_REGIONAL", "REGIONAL", "NEARLINE", "COLDLINE", "ARCHIVE", "STANDARD", "DURABLE_REDUCED_AVAILABILITY"], var.storage_class)
    error_message = "Supported values include: MULTI_REGIONAL, REGIONAL, NEARLINE, COLDLINE, ARCHIVE, STANDARD, DURABLE_REDUCED_AVAILABILITY."
  }
}

@imaginarynik
Copy link

Is there an update on this?

@dayglojesus
Copy link

The features described in this thread are essential to DRY code.

@SphenicPaul
Copy link

Agree with @AswinKakarot that having this (input) variable, validation functionality able to reference local variables would save a lot of duplication of effort and code... especially where validating (input) variable values against an array/list or map of keys and/or values that are defined as local variables and used in multiple places - I've hit this requirement today.

This might even be able to extend into using additional, nested resource/module/data output variables as part of the conditions? - Guess there will be some sort of dependency/resolution order to consider, but this sort of functionality might be especially useful if writing modules (obviouslty, wrapping around resources and other modules) and being able to use outputs from those nested resources and modules to validate input into the modules that are wrapping around and/or making use of them - This would prevent/reduce having to make direct/intermediate changes to the wrapper module(s) and allow the output variables to be passed through/up the module dependency chain.

@joe-a-t
Copy link

joe-a-t commented May 26, 2022

Mostly duplicate of #25609 so #25609 (comment) is probably the HashiCorp response. The main differentiation I see with this issue is the explicit calling out of allowing the use of locals, which isn't explicitly called out in #25609 but would probably also be solved by the proposed solution

@apparentlymart
Copy link
Member

Hi all,

Indeed, this does seem to be effectively a duplicate of #25609, which is now closed with the solution expected to arrive in the forthcoming Terraform v1.9.0 release.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

8 participants