-
Notifications
You must be signed in to change notification settings - Fork 165
Validate that resource keys do not contain variable references #5169
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| bundle: | ||
| name: variable-in-resource-key | ||
|
|
||
| variables: | ||
| env: | ||
| description: The target environment | ||
| default: dev | ||
|
|
||
| resources: | ||
| jobs: | ||
| ${var.env}_job: | ||
| name: my-job | ||
|
|
||
| targets: | ||
| dev: | ||
| default: true | ||
| resources: | ||
| jobs: | ||
| ${var.env}_job_2: | ||
| name: my-job | ||
| ${var.env}: my-job-description | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| Warning: unknown field: ${var.env} | ||
| at targets.dev.resources.jobs.${var.env}_job_2 | ||
| in databricks.yml:21:11 | ||
|
|
||
| Error: resource key "${var.env}_job" must not contain variable references | ||
|
Contributor
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. Could you add as a comment or on PR description what output this test had before this PR?
Contributor
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 see from #5098 that it's panic stacktrace, but good to confirm.
Contributor
Author
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 did fail with the same error before the fix |
||
| at resources.jobs.${var.env}_job | ||
| in databricks.yml:12:7 | ||
|
|
||
| Error: resource key "${var.env}_job_2" must not contain variable references | ||
| at targets.dev.resources.jobs.${var.env}_job_2 | ||
| in databricks.yml:20:11 | ||
|
|
||
|
|
||
| Exit code: 1 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| $CLI bundle plan |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package validate | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/libs/diag" | ||
| "github.com/databricks/cli/libs/dyn" | ||
| "github.com/databricks/cli/libs/dyn/dynvar" | ||
| ) | ||
|
|
||
| type noVariableReferenceInResourceKey struct{} | ||
|
|
||
| // NoVariableReferenceInResourceKey validates that no resource key contains a variable reference. | ||
| // Resource keys are used as identifiers throughout the deployment pipeline and must be static strings. | ||
| func NoVariableReferenceInResourceKey() bundle.Mutator { | ||
| return &noVariableReferenceInResourceKey{} | ||
| } | ||
|
|
||
| func (m *noVariableReferenceInResourceKey) Name() string { | ||
| return "validate:no_variable_reference_in_resource_key" | ||
| } | ||
|
|
||
| func (m *noVariableReferenceInResourceKey) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnostics { | ||
| var diags diag.Diagnostics | ||
|
|
||
| patterns := []dyn.Pattern{ | ||
| dyn.NewPattern(dyn.Key("resources"), dyn.AnyKey(), dyn.AnyKey()), | ||
|
andrewnester marked this conversation as resolved.
andrewnester marked this conversation as resolved.
|
||
| dyn.NewPattern(dyn.Key("targets"), dyn.AnyKey(), dyn.Key("resources"), dyn.AnyKey(), dyn.AnyKey()), | ||
| } | ||
|
|
||
| for _, pattern := range patterns { | ||
| _, err := dyn.MapByPattern( | ||
| b.Config.Value(), | ||
| pattern, | ||
| func(p dyn.Path, v dyn.Value) (dyn.Value, error) { | ||
| key := p[len(p)-1].Key() | ||
| if dynvar.ContainsVariableReference(key) { | ||
| diags = append(diags, diag.Diagnostic{ | ||
| Severity: diag.Error, | ||
| Summary: fmt.Sprintf("resource key %q must not contain variable references", key), | ||
| Locations: v.Locations(), | ||
| Paths: []dyn.Path{p}, | ||
| }) | ||
| } | ||
| return v, nil | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| diags = append(diags, diag.FromErr(err)...) | ||
| } | ||
| } | ||
|
|
||
| return diags | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.