Skip to content

feat: include template variables in dynamic parameter rendering #18819

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 14 commits into from
Jul 21, 2025

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jul 9, 2025

Closes #18671

Template variables now loaded into dynamic parameters. Future work is #18819 (comment) & #18819 (comment). Both are related

Summary by CodeRabbit

  • New Features

    • Added support for template version variables in dynamic parameter rendering and workspace tag generation.
    • Users can now provide variable values when creating or previewing template versions.
    • Terraform configurations can now expose and utilize user-defined variables in dynamic parameters.
  • Bug Fixes

    • Improved handling of variable types and defaults in dynamic parameter evaluation.
  • Tests

    • Enhanced test coverage for dynamic parameters and workspace tags with new test cases and refactored test structures.
  • Chores

    • Updated dependencies to the latest version of the preview module.
    • Added clarifying comments for future improvements in Terraform parsing.

@Emyrk Emyrk self-assigned this Jul 9, 2025
@Emyrk Emyrk force-pushed the stevenmasley/template_vars branch from e1915e7 to a8bdd04 Compare July 14, 2025 16:17
@Emyrk Emyrk marked this pull request as ready for review July 17, 2025 18:17
Comment on lines +28 to +34
// Defaulting the empty type to "string"
// TODO: This does not match the terraform behavior, however it is too late
// at this point in the code to determine this, as the database type stores all values
// as strings. The code needs to be fixed in the `Parse` step of the provisioner.
// That step should determine the type of the variable correctly and store it in the database.
case "string", "":
ctyVals[v.Name] = cty.StringVal(value)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not ideal, will resolve in another PR

Comment on lines +18 to +21
// TODO: This Parse is incomplete. It uses tfparse instead of terraform.
// The inputs are incomplete, as values such as the user context, parameters,
// etc are all important to the parsing process. This should be replaced with
// preview and have all inputs.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to fix this to remove the need for tfparse

@Emyrk Emyrk requested a review from johnstcn July 21, 2025 14:49
Copy link

coderabbitai bot commented Jul 21, 2025

Caution

Review failed

An error occurred during the review process. Please try again later.

Walkthrough

The changes introduce support for passing and handling user-supplied template variable values in dynamic parameter flows, aligning behavior with classic flows. This includes updates to data structures, dynamic parameter rendering, test coverage, and workspace builder logic. Additional refactoring and minor improvements are made to related tests and documentation.

Changes

File(s) Change Summary
coderd/coderdtest/dynamicparameters.go Added Variables field to DynamicParameterTemplateParams; logic to handle variables in template creation and parsing.
coderd/dynamicparameters/render.go, coderd/dynamicparameters/variablevalues.go Added support for loading, caching, and converting template version variables for dynamic parameter rendering.
coderd/parameters_test.go Added test coverage for template variables; extended test setup to support variable injection.
coderd/templateversions.go Updated function signatures and logic to pass user-supplied template variable values for dynamic tag generation.
coderd/testdata/parameters/variables/main.tf Added Terraform configuration for variable and coder_parameter resource for parameter exposure in tests.
coderd/wsbuilder/wsbuilder.go Integrated template version variables into dynamic parameter renderer preparation; added error handling.
enterprise/coderd/workspaces_test.go Refactored workspace tags Terraform tests; added struct for test cases, helper function, and improved assertions.
go.mod Updated github.com/coder/preview module version.
provisioner/terraform/parse.go Added a TODO comment regarding the completeness and future direction of the Parse method.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant API
    participant DB
    participant DynamicRenderer

    User->>API: Submit template version with variable values
    API->>DB: Store template and variables
    API->>DynamicRenderer: Prepare dynamic renderer (with variables)
    DynamicRenderer->>DB: Load template variable values (if not provided)
    DynamicRenderer->>DynamicRenderer: Convert variables to TFVars
    DynamicRenderer->>API: Return rendered parameters (using TFVars)
    API->>User: Respond with rendered parameters
Loading

Estimated code review effort

3 (~45 minutes)

Poem

🐇
In burrows deep, I tweak and twirl,
Now variables in both worlds swirl!
Dynamic and Classic, side by side,
No more differences to hide.
With tests and tags, the code’s refined—
A rabbit’s hop for peace of mind!
🌱


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Emyrk Emyrk requested a review from Copilot July 21, 2025 15:00
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements support for template variables in dynamic parameter rendering, allowing templates to access variable values during the dynamic parameter resolution process. Previously, template variables were not available when rendering dynamic parameters, limiting the flexibility of parameter definitions.

  • Adds template variable support to the dynamic parameter rendering system
  • Updates the preview library dependency to enable template variable functionality
  • Includes comprehensive test coverage for both "classic" and "dynamic" parameter flows

Reviewed Changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
provisioner/terraform/parse.go Adds TODO comment about incomplete Parse function implementation
go.mod Updates preview library dependency to newer version
enterprise/coderd/workspaces_test.go Refactors test to support both dynamic and classic parameter flows
coderd/wsbuilder/wsbuilder.go Integrates template variables into dynamic parameter renderer
coderd/testdata/parameters/variables/main.tf Adds test terraform configuration for variable testing
coderd/templateversions.go Passes template variables to dynamic tag rendering
coderd/parameters_test.go Adds test case for template variables in dynamic parameters
coderd/dynamicparameters/variablevalues.go Implements variable value conversion to cty.Value format
coderd/dynamicparameters/render.go Integrates template variable values into dynamic renderer
coderd/coderdtest/dynamicparameters.go Updates test helper to support template variables
Comments suppressed due to low confidence (1)

enterprise/coderd/workspaces_test.go:3178

  • The change from -time.Second*10 to -time.Second reduces the time tolerance significantly. This change appears unrelated to the PR's purpose and could make the test flaky if there are timing variations.
		require.WithinRange(t, *workspace.DormantAt, time.Now().Add(-time.Second), time.Now())

// TODO: Does this break if the type is not a string?
tfVarValues := make(map[string]cty.Value)
for _, variable := range templateVariables {
tfVarValues[variable.Name] = cty.StringVal(variable.Value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT VariableValue.Value will always be a string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, which I just found strange. This is working for now, I will test complex behavior as I edit the source of this (the parse function)

@Emyrk Emyrk merged commit aedc019 into main Jul 21, 2025
33 checks passed
@Emyrk Emyrk deleted the stevenmasley/template_vars branch July 21, 2025 18:02
@github-actions github-actions bot locked and limited conversation to collaborators Jul 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Terraform variables show different behavior between Classic and Dynamic template formats
2 participants