Skip to content

fix: skip validating unknown versions list #115

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

Merged
merged 1 commit into from
Oct 21, 2024
Merged

Conversation

ethanndickson
Copy link
Member

@ethanndickson ethanndickson commented Oct 21, 2024

This fixes an error when setting the versions attribute of a coderd_template using a variable, e.g:

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:

func (v lengthBetweenValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
	if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
		return
	}
	...
}

Copy link
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @ethanndickson and the rest of your teammates on Graphite Graphite

@ethanndickson ethanndickson marked this pull request as ready for review October 21, 2024 06:11
@ethanndickson ethanndickson merged commit 269046e into main Oct 21, 2024
14 checks passed
@ethanndickson ethanndickson deleted the ethan/skip-validate branch October 21, 2024 06:13
@ethanndickson ethanndickson self-assigned this Oct 21, 2024
ethanndickson added a commit that referenced this pull request Oct 31, 2024
Same problem as seen in #115.

This fixes a problem where having multiple template version names
manually set using Terraform variables would always return a duplicate
name error, as Terraform would mark the attribute, although it is
required and already written in the config, as Unknown before calling
our custom validator. This unknown value is only ever passed to the
validator, never `Create` or `Update`.

Knowing this, we see the same problem exists for all attributes we read
in our custom validator. The only remaining one is `active`, where we
check at least one version is marked as active. In this case, we also
need to skip validating `active` if any of the booleans are unknown.

We previously didn't have any tests that explicitly used Terraform
variables, so this PR adds one, in case the behaviour of these variable
set attributes is changed in the future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant