8000 fix: use minDisabled, maxDisabled for parameter validation by mtojek · Pull Request #7797 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix: use minDisabled, maxDisabled for parameter validation #7797

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

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor the min-max condition
  • Loading branch information
mtojek committed Jun 2, 2023
commit 3dc0474805051f8d72d3b4a8118a35bc70e09b84
26 changes: 20 additions & 6 deletions provisioner/terraform/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,26 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string, rawParameterNa
protoParam.ValidationRegex = param.Validation[0].Regex
protoParam.ValidationError = param.Validation[0].Error

// Backward compatibility with terraform-coder-plugin < v0.8.2
if _, ok := resource.AttributeValues["min_disabled"]; !ok && param.Validation[0].Min == 0 {
param.Validation[0].MinDisabled = true
}
if _, ok := resource.AttributeValues["max_disabled"]; !ok && param.Validation[0].Max == 0 {
param.Validation[0].MaxDisabled = true
validationAttributeValues, ok := resource.AttributeValues["validation"]
if ok {
validationAttributeValuesArr, ok := validationAttributeValues.([]interface{})
if ok {
validationAttributeValuesMapStr, ok := validationAttributeValuesArr[0].(map[string]interface{})
if ok {
// Backward compatibility with terraform-coder-plugin < v0.8.2:
// * "min_disabled" and "max_disabled" are not available yet
// * "min" and "max" are required to be specified together
if _, ok = validationAttributeValuesMapStr["min_disabled"]; !ok {
if param.Validation[0].Min != 0 || param.Validation[0].Max != 0 {
param.Validation[0].MinDisabled = false
param.Validation[0].MaxDisabled = false
} else {
param.Validation[0].MinDisabled = true
param.Validation[0].MaxDisabled = true
}
}
}
}
}

if !param.Validation[0].MaxDisabled {
Expand Down
24 changes: 24 additions & 0 deletions provisioner/terraform/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,18 @@ func TestConvertResources(t *testing.T) {
DefaultValue: "4",
ValidationMin: terraform.PtrInt32(3),
ValidationMax: terraform.PtrInt32(6),
}, {
Name: "number_example_min_zero",
Type: "number",
DefaultValue: "4",
ValidationMin: terraform.PtrInt32(0),
ValidationMax: terraform.PtrInt32(6),
}, {
Name: "number_example_max_zero",
Type: "number",
DefaultValue: "-2",
ValidationMin: terraform.PtrInt32(-3),
ValidationMax: terraform.PtrInt32(0),
}, {
Name: "number_example",
Type: "number",
Expand Down Expand Up @@ -364,12 +376,24 @@ func TestConvertResources(t *testing.T) {
DefaultValue: "4",
ValidationMin: terraform.PtrInt32(3),
ValidationMax: nil,
}, {
Name: "number_example_min_zero",
Type: "number",
DefaultValue: "4",
ValidationMin: terraform.PtrInt32(0),
ValidationMax: nil,
}, {
Name: "number_example_max",
Type: "number",
DefaultValue: "4",
ValidationMin: nil,
ValidationMax: terraform.PtrInt32(6),
}, {
Name: "number_example_max_zero",
Type: "number",
DefaultValue: "-3",
ValidationMin: nil,
ValidationMax: terraform.PtrInt32(0),
}, {
Name: "number_example",
Type: "number",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ data "coder_parameter" "number_example_min" {
}
}

data "coder_parameter" "number_example_min_zero" {
name = "number_example_min_zero"
type = "number"
default = 4
validation {
min = 0
}
}

data "coder_parameter" "number_example_max" {
name = "number_example_max"
type = "number"
Expand All @@ -35,6 +44,15 @@ data "coder_parameter" "number_example_max" {
}
}

data "coder_parameter" "number_example_max_zero" {
name = "number_example_max_zero"
type = "number"
default = -3
validation {
max = 0
}
}

data "coder_parameter" "number_example" {
name = "number_example"
type = "number"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.