Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: parameter validation refactored to single function#381

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Emyrk merged 24 commits intomainfromstevenmasley/value_validate
May 7, 2025

Conversation

Emyrk
Copy link
Member

@EmyrkEmyrk commentedApr 14, 2025
edited
Loading

What this PR does

Centralizes all validation logic into a single function on the parameter. This is intended to be imported by other packages (eg: preview) to do clientside validation before running terraform.

Validation changes

See diff frommain. Left side is the tests passing onmain.
https://www.diffchecker.com/GaUlaTze/

  • Input valuesmust be in the option set.Before this was allowed, but enforced incoder/coder
  • Number parameters require values to be numbers.Before this was allowed, but enforced incoder/coder

Future work

  • Disallow empty values for creating workspaces:
    // TODO: When empty values want to be rejected, uncomment this.
    // coder/coder should update to use template import mode first,
    // before this is uncommented.
    //if value == nil && mode == ValidationModeDefault {
    //var empty string
    //value = &empty
    //}
  • coder/coder to set the correct validation mode
  • validatemonotonic in the provider, passing in the old value via env var

@Emyrk
Copy link
MemberAuthor

Emyrk commentedApr 14, 2025
edited
Loading

@johnstcn@matifali should this pass a template import?

Before this PR, this would pass aterraform plan.

data"coder_parameter""region" {name="region"description="Which region would you like to deploy to?"type="string"order=1option {name="Europe"value="eu"  }option {name="United States"value="us"  }}

EDIT: Test case here

| EmtyOpts | string,number | | | 1,2,3 | | | "" | false | |

This PR sets the error to:

Planning failed. Terraform encountered an error while generating this plan.╷│ Error: Value must be a valid option. The value is empty, did you forget to set it with a default or from user input?│ │   with data.coder_parameter.region,│   on main.tf line 21, in data "coder_parameter" "region":│   21: data "coder_parameter" "region" {│ │ the value "" must be defined as one of options

To fix this, either the value must be passed via an env var:

CODER_PARAMETER_c697d2981bf416569a16cfbcdec1542b5398f3cc77d2b905819aa99c46ecf6f6=us terraform plan

Or with a default:

data "coder_parameter" "region" {  # ...  default = "us"  # ...}

I feel like we should reject it? But that is a breaking change.

@matifali
Copy link
Member

matifali commentedApr 14, 2025
edited
Loading

There are use cases where an admin may not want to set a default to force the user to choose an option. At least it was possible before this PR. I am not sure how many users were actually using it.

But I have seen requests to supportcoder_parameter without a default value better:#144

@johnstcn
Copy link
Member

johnstcn commentedApr 15, 2025
edited
Loading

There are use cases where an admin may not want to set a default to force the user to choose an option.

This behaviour is problematic because you could end up with a plan that does not correspond to the apply, depending on how the value of the parameter is used.

Would marking the parameter asephemeral be a possible workaround here?

EDIT: marking a parameter as ephemeral requires it to be mutable and to also have a default set. This may not suit certain use-cases.

@matifali
Copy link
Member

matifali commentedApr 15, 2025
edited
Loading

Do we require a user input if the parameters are ephemeral? Do we not allow setting a default value for such parameters?
Do such ephemeral parameters support options too?

If the answers are yes, then we just need better docs on how to fulfill this use case.

So that people don't try to use the existing non ephemeral parameters.

@Emyrk
Copy link
MemberAuthor

@matifali@johnstcn

How I see it is the relaxed validation on template import is incorrect. If a parameter has 0 values, but will have some value at workspace create, then theterraform plan could be incorrect.

However, I understand the use case. The provider SDK we are using does not indicate anywhere ifterraform plan vsterraform apply is being run. So there is no way to conditionally enforce validation that way.

My gut thought is that we need to effectively have 2 modes of validation. 1 for template import, 1 for workspace create, where the latter is strict. The strict validation is the most correct.

What if we pass through an env var a relaxed validation mode?coder/coder can passCODER_VALIDATION=relaxed when doing a template import. Then the provider can acceptnull values.

@johnstcn
Copy link
Member

What if we pass through an env var a relaxed validation mode?coder/coder can passCODER_VALIDATION=relaxed when doing a template import. Then the provider can acceptnull values.

I think this approach could work, but we'd want to perhaps tie its behaviour specifically to what "operation" we're doing in Coder. We might also want to be wary of folks just setting this env var on the provisioner to work around null values entirely.

@Emyrk
Copy link
MemberAuthor

I think this approach could work, but we'd want to perhaps tie its behaviour specifically to what "operation" we're doing in Coder. We might also want to be wary of folks just setting this env var on the provisioner to work around null values entirely.

Yes, template import would have to pass some env var to change the behavior.

As for folks setting this env var at home, I think that would just be on them. By default the validation should be handled correctly in coder (when to be relaxed, and when not)

@EmyrkEmyrkforce-pushed thestevenmasley/value_validate branch froma61664c to5e19e2aCompareApril 30, 2025 20:46
Some validation changes:- Invalid options rejected (not in option set)
@EmyrkEmyrk changed the titlefeat: validate user input values the same way as "default"feat: stricter parameter input validationMay 1, 2025
@EmyrkEmyrk marked this pull request as ready for reviewMay 1, 2025 19:17
@EmyrkEmyrk changed the titlefeat: stricter parameter input validationfeat: 2 parameter validation modes for create & importMay 1, 2025
@spikecurtis
Copy link
Contributor

As for folks setting this env var at home, I think that would just be on them. By default the validation should be handled correctly in coder (when to be relaxed, and when not)

If provisionerdalways sets the environment variable before exec'ing terraform, then I don't think we need to worry about users setting it.

Copy link
Contributor

@spikecurtisspikecurtis left a comment

Choose a reason for hiding this comment

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

It seems very odd to me that in this PR, template-import mode, which is supposedly being created for back-compatibility ismore strict than what you're calling default mode.

Shouldn't it be the other way around? That template-import mode skips some checks that would break backward compatibility?

return"",diags
}

ifmode==ValidationModeTemplateImport&&v.Default!=nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a weird case where template import ismore strict than default. What is the justification for that? I thought we were only making template import less strict so that we don't break people.

Copy link
MemberAuthor

@EmyrkEmyrkMay 2, 2025
edited
Loading

Choose a reason for hiding this comment

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

You are correct. When we turn this on in coderd,new templates imported will be subject to a stricter validation. There is 2 reasons.

Strict options

You can currently addoptions to your parameters that are invalid. That is a bit confusing on the UI, when options exist, but if you select them, it would fail to build.

Atcreate time, the options are ignored, since they are not used.

Strict Default

This was actually done before tooiff no input value was given (which is usually the case for template-import). I made this more explicit, where theDefault has to be valid if set. Even if you passed in an input value at import time.

I enforce this, because if you do not, then a user who leaves a default value for a form has an invalid value and cannot build.


Sidenote, but if you want to place a value in the UI without it being valid, we are addingstyling:

data"coder_parameter""numbers" {name="numebrs"type="number"order=1styling=jsonencode({    placeholder="Enter a number"  })}

@Emyrk
Copy link
MemberAuthor

Emyrk commentedMay 2, 2025
edited
Loading

It seems very odd to me that in this PR, template-import mode, which is supposedly being created for back-compatibility is more strict than what you're calling default mode.

Shouldn't it be the other way around? That template-import mode skips some checks that would break backward compatibility?

Maybe I should have broken this into 2 PRs. My goal is to make existing imported templates backwards compatible. They will function as they used to.

Future imported templates I am closing some of the validation holes.

And the backwardsfeature I am keeping, is allowing empty andefault value. So this isstill valid:

data"coder_parameter""region" {name="region"description="Which region would you like to deploy to?"type="string"order=1option {name="Europe"value="eu"  }}

The other changes I made are fixing validation bugs. Meaning validation used to be skipped on present values.


Validating input values is in the option set

This is incoder/coder. This change shifts some of the validation incoder/coder ->terraform-provider-coder.

This is now invalid except attemplate-import, since no input values are given. So attemplate-import, there is no change in behavior here.

# Using input value 3# CODER_PARAMETER_f3c7807d475073ba009bf4801b2d934e9f0126cb96dd19a27dbffcae23a7f5a3=3 terraform plandata"coder_parameter""numbers" {name="numebrs"type="number"order=1option {name="Five"value="5"  }}

Validating option values

This used to pass, and it will continue to passcreate so that previous templates still build if they have been imported. I cannot break existing imported templates.

For futuretemplate-imports, I throw an error. I think this fix can be seen as a bug fix. Otherwise, dropdown options in the UI form will fail when you select them.

data"coder_parameter""numbers" {name="numebrs"type="number"order=1default=7validation {min=6max=10error="The number must be between 6 and 10"  }option {name="Seven"value="7"  }option {name="Four"value="4"  }}
│ Error: Option "Four": Invalid parameter value according to 'validation' block│ │   with data.coder_parameter.numbers,│   on main.tf line 10, in data "coder_parameter" "numbers":│   10: data "coder_parameter" "numbers" {│ │ The number must be between 6 and 10

@Emyrk
Copy link
MemberAuthor

If provisionerdalways sets the environment variable before exec'ing terraform, then I don't think we need to worry about users setting it.

I agree, I was asked this in a voice chat. It can be set by users, but I don't imagine they will.

@EmyrkEmyrk requested a review fromspikecurtisMay 2, 2025 15:05
@EmyrkEmyrk changed the titlefeat: 2 parameter validation modes for create & importfeat: parameter validation refactored to single functionMay 5, 2025
@Emyrk
Copy link
MemberAuthor

I removed the idea of 2 validation modes. There is only 1.option values are not validated, just as they were previously skipped.

The new validation mode is backwards compatible in all intended behavior.

Copy link
Contributor

@spikecurtisspikecurtis left a comment

Choose a reason for hiding this comment

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

some comments and suggestions inline, but I don't need to review again.

// Always set back the value, as it can be sourced from the default
rd.Set("value",value)

// Set the form_type as it could have changed in the validation.
Copy link
Contributor

Choose a reason for hiding this comment

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

I found this curious and confusing. A fuller explanation is probably worth setting out here: theform_type can start off unset, in which case the "validation" code selects a form type based on thetype.

It's also a bit of a naming failure that we call the procedure that does this "Validation", which implies it doesn't change anything and just renders a verdict. In the Coderd code, I believe we called this procedure "resolution".

Emyrk reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

It is unfortunate. I updated the comment.

@EmyrkEmyrk merged commit3c74804 intomainMay 7, 2025
6 checks passed
@EmyrkEmyrk deleted the stevenmasley/value_validate branchMay 7, 2025 13:01
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsMay 7, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@spikecurtisspikecurtisspikecurtis approved these changes

@johnstcnjohnstcnAwaiting requested review from johnstcn

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@Emyrk@matifali@johnstcn@spikecurtis

[8]ページ先頭

©2009-2025 Movatter.jp