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

COMPONENT: Add support for optional objects in variable definitions with non-trivial default values #3595

Open
1 task done
zoonage opened this issue Apr 19, 2024 · 3 comments
Labels
enhancement New feature or request new Un-triaged issue

Comments

@zoonage
Copy link

zoonage commented Apr 19, 2024

Description

I'd like to be able to define a variable like this in cdktf, where a is optional but b is not.

variable "complex" {
  type = object({
    a = optional(number, 1)
    b = string
  })
  default = {}
}

Background context: I'm looking into cdktf adoption to try and simplify a large existing Terraform codebase.

The first area I'm looking at is removing duplicated complex variable type signatures. These can be up to ~30 lines that we manually keep in sync to work around this issue.

I'm planning on doing this by synthesising a bit of the module (such as specific variable definitions) by using a shared library but keeping the rest of the existing Terraform in place.

Happy to try and implement this

References

No response

Help Wanted

  • I'm interested in contributing a fix myself

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment
@zoonage zoonage added enhancement New feature or request new Un-triaged issue labels Apr 19, 2024
@zoonage
Copy link
Author

zoonage commented Apr 19, 2024

Happy to have a shot at implementing this. The only question I have is I'm not sure if there is a way to build a complex type for a default like

{
  otel-agent = { image = "quay.io/zoonage/otel-agent:latest" }
  some-sidecar = { image = "quay.io/zoonage/otel-agent:latest", resources = { cpu = 1 } }
}

or if that's some extra functionality that'll be needed for this

@chrcaleb
Copy link

chrcaleb commented Aug 6, 2024

I am extremely interested in this feature as well. I am hoping by providing the example Hashi provides for HCL here, the complexity is less than you think.

The optional modifier takes one or two arguments.

Type: (Required) The first argument specifies the type of the attribute.
Default: (Optional) The second argument defines the default value that Terraform should use if the attribute is not present. This must be compatible with the attribute type. If not specified, Terraform uses a null value of the appropriate type as the default.

variable "buckets" {
  type = list(object({
    name    = string
    enabled = optional(bool, true)
    website = optional(object({
      index_document = optional(string, "index.html")
      error_document = optional(string, "error.html")
      routing_rules  = optional(string)
    }), {})
  }))
}

@jsteinich
Copy link
Collaborator

I don't think it would be too difficult to add a simple version of this. If you look at https://github.com/hashicorp/terraform-cdk/blob/main/packages/cdktf/lib/terraform-variable.ts there are some static functions meant for building complex types. A new method for optional could be created as a quick version. Building better default support would take some more thought.

Actually using complex variables effectively within cdktf is another issue, but having basic support may help make things easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new Un-triaged issue
Projects
None yet
Development

No branches or pull requests

3 participants