From 7bbfc7f2f7d84a540d1d316ad52d1aa6a7eb9c21 Mon Sep 17 00:00:00 2001 From: Ethan <39577870+ethanndickson@users.noreply.github.com> Date: Thu, 17 Oct 2024 22:45:43 -0500 Subject: [PATCH 1/2] docs: replace example that requires TFC (#114) --- docs/resources/template.md | 8 ++++---- examples/resources/coderd_template/resource.tf | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/resources/template.md b/docs/resources/template.md index cacd1b7..81edc41 100644 --- a/docs/resources/template.md +++ b/docs/resources/template.md @@ -22,8 +22,8 @@ When importing, the ID supplied can be either a template UUID retrieved via the // Provider populated from environment variables provider "coderd" {} -// Get the commit SHA of the configuration's git repository -variable "TFC_CONFIGURATION_VERSION_GIT_COMMIT_SHA" { +// Can be populated using an environment variable, or an external datasource script +variable "COMMIT_SHA" { type = string } @@ -38,12 +38,12 @@ resource "coderd_template" "ubuntu-main" { description = "The main template for developing on Ubuntu." versions = [ { - name = "stable-${var.TFC_CONFIGURATION_VERSION_GIT_COMMIT_SHA}" + name = "stable-${var.COMMIT_SHA}" description = "The stable version of the template." directory = "./stable-template" }, { - name = "staging-${var.TFC_CONFIGURATION_VERSION_GIT_COMMIT_SHA}" + name = "staging-${var.COMMIT_SHA}" description = "The staging version of the template." directory = "./staging-template" } diff --git a/examples/resources/coderd_template/resource.tf b/examples/resources/coderd_template/resource.tf index 3cbc8fe..e644d6e 100644 --- a/examples/resources/coderd_template/resource.tf +++ b/examples/resources/coderd_template/resource.tf @@ -1,8 +1,8 @@ // Provider populated from environment variables provider "coderd" {} -// Get the commit SHA of the configuration's git repository -variable "TFC_CONFIGURATION_VERSION_GIT_COMMIT_SHA" { +// Can be populated using an environment variable, or an external datasource script +variable "COMMIT_SHA" { type = string } @@ -17,12 +17,12 @@ resource "coderd_template" "ubuntu-main" { description = "The main template for developing on Ubuntu." versions = [ { - name = "stable-${var.TFC_CONFIGURATION_VERSION_GIT_COMMIT_SHA}" + name = "stable-${var.COMMIT_SHA}" description = "The stable version of the template." directory = "./stable-template" }, { - name = "staging-${var.TFC_CONFIGURATION_VERSION_GIT_COMMIT_SHA}" + name = "staging-${var.COMMIT_SHA}" description = "The staging version of the template." directory = "./staging-template" } From 269046ef5ff717448975573d0ad0f633a6c6cbeb Mon Sep 17 00:00:00 2001 From: Ethan <39577870+ethanndickson@users.noreply.github.com> Date: Mon, 21 Oct 2024 01:13:23 -0500 Subject: [PATCH 2/2] fix: skip validating unknown versions list (#115) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes an error when setting the `versions` attribute of a `coderd_template` using a variable, e.g: ```terraform resource "coderd_template" "dev" { versions = var.template_versions [...] } variable "template_versions" { description = "Versions of the Coder template." default = [ { directory = "modules/" active = true tf_vars = [ { name = "coder_instance" value = "prod" } ] } ] } ``` would return: ``` │ Error: Value Conversion Error │ │ with module.devcontainers.coderd_template.dev, │ An unexpected error was encountered trying to build a value. This is always an error in the provider. Please report the following to the provider │ developer: │ │ Received unknown value, however the target type cannot handle unknown values. Use the corresponding `types` package type or a custom type that handles │ unknown values. │ │ Path: │ Target Type: []provider.TemplateVersion │ Suggested Type: basetypes.ListValue ``` This error was caused by attempting to validate the versions list without checking if the config value is unknown. Normally, this value should never be unknown, as it's required, but it looks like Terraform does a configuration validation *before* variables are populated, as well as after. To confirm this is the correct solution, we see that all the default validators perform the same null & unknown checks, e.g: ```go func (v lengthBetweenValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) { if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() { return } ... } ``` --- internal/provider/template_resource.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/provider/template_resource.go b/internal/provider/template_resource.go index a23f408..6c3acbb 100644 --- a/internal/provider/template_resource.go +++ b/internal/provider/template_resource.go @@ -888,6 +888,10 @@ func (a *activeVersionValidator) MarkdownDescription(context.Context) string { // ValidateList implements validator.List. func (a *activeVersionValidator) ValidateList(ctx context.Context, req validator.ListRequest, resp *validator.ListResponse) { + if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() { + return + } + var data []TemplateVersion resp.Diagnostics.Append(req.ConfigValue.ElementsAs(ctx, &data, false)...) if resp.Diagnostics.HasError() {