Skip to content

Commit

Permalink
Validate that resource import ids are not outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas11 committed Aug 1, 2024
1 parent 1332be6 commit 708e73a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
13 changes: 12 additions & 1 deletion pkg/pulumiyaml/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,18 @@ func (e *programEvaluator) registerResource(kvp resourceNode) (lateboundResource
}
}
if v.Options.Import != nil {
opts = append(opts, pulumi.Import(pulumi.ID(v.Options.Import.Value)))
importValue, ok := e.evaluateExpr(v.Options.Import)
if ok {
if !hasOutputs(importValue) {
opts = append(opts, pulumi.Import(pulumi.ID(v.Options.Import.Value)))
} else {
e.error(v.Options.Import, "import must be not be an output")
overallOk = false
}
} else {
e.error(v.Options.Import, "couldn't evaluate the 'import' resource option")
overallOk = false
}
}
if v.Options.IgnoreChanges != nil {
opts = append(opts, pulumi.IgnoreChanges(listStrings(v.Options.IgnoreChanges)))
Expand Down
49 changes: 48 additions & 1 deletion pkg/pulumiyaml/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ func testTemplateDiags(t *testing.T, template *ast.TemplateDecl, callback func(*
"foo": "oof",
}), args.Inputs, "expected resource test:resource:type to have property foo: oof")
assert.Equal(t, "", args.Provider)
assert.Equal(t, "", args.ID)
// ImportId is the empty string unless the 'import' resource option was given.
assert.Equal(t, args.RegisterRPC.ImportId, args.ID)

return "someID", resource.PropertyMap{
"foo": resource.NewStringProperty("qux"),
Expand Down Expand Up @@ -2552,3 +2553,49 @@ func TestConflictingEnvVarsMultipleDuplicates(t *testing.T) {
conflicts := conflictingEnvVars(env)
assert.ElementsMatch(t, []string{"FOO", "BAR"}, conflicts)
}

func TestRegisterResourceWithImport(t *testing.T) {
t.Parallel()

t.Run("Basic", func(t *testing.T) {

Check failure on line 2560 in pkg/pulumiyaml/run_test.go

View workflow job for this annotation

GitHub Actions / lint / lint

Function TestRegisterResourceWithImport missing the call to method parallel in the test run
text := `
name: test-register
runtime: yaml
resources:
bucket:
type: test:resource:type
properties:
foo: oof
options:
import: "id"
`

tmpl := yamlTemplate(t, strings.TrimSpace(text))
testTemplate(t, tmpl, func(e *programEvaluator) {
diags := e.evalContext.Evaluate(e.pulumiCtx)
requireNoErrors(t, tmpl, diags)
})
})

t.Run("Output as import id", func(t *testing.T) {

Check failure on line 2580 in pkg/pulumiyaml/run_test.go

View workflow job for this annotation

GitHub Actions / lint / lint

Function TestRegisterResourceWithImport missing the call to method parallel in the test run
text := `
name: test-register
runtime: yaml
config:
importId:
default: def
resources:
bucket:
type: test:resource:type
properties:
foo: oof
options:
import: ${importId}
`

_, diags, err := LoadYAMLBytes("<stdin>", []byte(text))
require.NoError(t, err)
require.Len(t, diags, 1)
assert.Equal(t, "import must be a string", diags[0].Diagnostic.Summary)
})
}

0 comments on commit 708e73a

Please sign in to comment.