- Notifications
You must be signed in to change notification settings - Fork1k
fix(provisioner/terraform/tfparse): evaluate coder_parameter defaults with variables#15800
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -172,7 +172,7 @@ func (p *Parser) WorkspaceTagDefaults(ctx context.Context) (map[string]string, e | ||
if err != nil { | ||
return nil, xerrors.Errorf("load variable defaults: %w", err) | ||
} | ||
paramsDefaults, err := p.CoderParameterDefaults(ctx, varsDefaults) | ||
if err != nil { | ||
return nil, xerrors.Errorf("load parameter defaults: %w", err) | ||
} | ||
@@ -268,7 +268,7 @@ func (p *Parser) VariableDefaults(ctx context.Context) (map[string]string, error | ||
// CoderParameterDefaults returns the default values of all coder_parameter data sources | ||
// in the parsed module. | ||
func (p *Parser) CoderParameterDefaults(ctx context.Context, varsDefaults map[string]string) (map[string]string, error) { | ||
defaultsM := make(map[string]string) | ||
var ( | ||
skipped []string | ||
@@ -316,14 +316,28 @@ func (p *Parser) CoderParameterDefaults(ctx context.Context) (map[string]string, | ||
} | ||
if _, ok := resContent.Attributes["default"]; !ok { | ||
p.logger.Warn(ctx, "coder_parameter data source does not have a default value", slog.F("name", dataResource.Name)) | ||
defaultsM[dataResource.Name] = "" | ||
} else { | ||
expr := resContent.Attributes["default"].Expr | ||
value, err := previewFileContent(expr.Range()) | ||
if err != nil { | ||
return nil, xerrors.Errorf("can't preview the resource file: %v", err) | ||
} | ||
// Issue #15795: the "default" value could also be an expression we need | ||
// to evaluate. | ||
// TODO: should we support coder_parameter default values that reference other coder_parameter data sources? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Has there been an ask? If not, we can probably defer it until such a time. It's best to be careful how much we expand the scope of these expressions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I'm not aware of any ask for this just yet. I'd much prefer to defer it as it opens up several cans of worms. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. It is sort of a FIXME :) | ||
evalCtx := buildEvalContext(varsDefaults, nil) | ||
val, diags := expr.Value(evalCtx) | ||
if diags.HasErrors() { | ||
return nil, xerrors.Errorf("failed to evaluate coder_parameter %q default value %q: %s", dataResource.Name, value, diags.Error()) | ||
} | ||
// Do not use "val.AsString()" as it can panic | ||
strVal, err := ctyValueString(val) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to marshal coder_parameter %q default value %q as string: %s", dataResource.Name, value, err) | ||
} | ||
defaultsM[dataResource.Name] = strings.Trim(strVal, `"`) | ||
} | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -114,6 +114,73 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) { | ||
expectTags: map[string]string{"platform": "kubernetes", "cluster": "developers", "region": "us", "az": "a"}, | ||
expectError: "", | ||
}, | ||
{ | ||
name: "main.tf with parameter that has default value from dynamic value", | ||
files: map[string]string{ | ||
"main.tf": ` | ||
provider "foo" {} | ||
resource "foo_bar" "baz" {} | ||
variable "region" { | ||
type = string | ||
default = "us" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I don't remember the current bevahior, so let me ask - There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Correct, if there is no default value we will fail this 'preflight' check with an error. This only happens if you're using the | ||
} | ||
variable "az" { | ||
type = string | ||
default = "${""}${"a"}" | ||
} | ||
data "base" "ours" { | ||
all = true | ||
} | ||
data "coder_parameter" "az" { | ||
name = "az" | ||
type = "string" | ||
default = var.az | ||
} | ||
data "coder_workspace_tags" "tags" { | ||
tags = { | ||
"platform" = "kubernetes", | ||
"cluster" = "${"devel"}${"opers"}" | ||
"region" = var.region | ||
"az" = data.coder_parameter.az.value | ||
} | ||
}`, | ||
}, | ||
expectTags: map[string]string{"platform": "kubernetes", "cluster": "developers", "region": "us", "az": "a"}, | ||
expectError: "", | ||
}, | ||
{ | ||
name: "main.tf with parameter that has default value from another parameter", | ||
files: map[string]string{ | ||
"main.tf": ` | ||
provider "foo" {} | ||
resource "foo_bar" "baz" {} | ||
variable "region" { | ||
type = string | ||
default = "us" | ||
} | ||
data "base" "ours" { | ||
all = true | ||
} | ||
data "coder_parameter" "az" { | ||
type = string | ||
default = "${""}${"a"}" | ||
} | ||
data "coder_parameter" "az2" { | ||
name = "az" | ||
type = "string" | ||
default = data.coder_parameter.az.value | ||
} | ||
data "coder_workspace_tags" "tags" { | ||
tags = { | ||
"platform" = "kubernetes", | ||
"cluster" = "${"devel"}${"opers"}" | ||
"region" = var.region | ||
"az" = data.coder_parameter.az2.value | ||
} | ||
}`, | ||
}, | ||
expectError: "Unknown variable; There is no variable named \"data\".", | ||
}, | ||
{ | ||
name: "main.tf with multiple valid workspace tags", | ||
files: map[string]string{ | ||
Uh oh!
There was an error while loading.Please reload this page.