Skip to content

Implement create_when_missing #37260

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions internal/configs/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Import struct {

ForEach hcl.Expression

CreateWhenMissing hcl.Expression

ProviderConfigRef *ProviderConfigRef
Provider addrs.Provider

Expand Down Expand Up @@ -77,6 +79,10 @@ func decodeImportBlock(block *hcl.Block) (*Import, hcl.Diagnostics) {
imp.ForEach = attr.Expr
}

if attr, exists := content.Attributes["create_when_missing"]; exists {
imp.CreateWhenMissing = attr.Expr
}

if attr, exists := content.Attributes["provider"]; exists {
if len(imp.ToResource.Module) > 0 {
diags = append(diags, &hcl.Diagnostic{
Expand Down Expand Up @@ -132,6 +138,9 @@ var importBlockSchema = &hcl.BodySchema{
{
Name: "identity",
},
{
Name: "create_when_missing",
},
},
}

Expand Down
29 changes: 29 additions & 0 deletions internal/configs/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,35 @@ func TestImportBlock_decode(t *testing.T) {
},
``,
},
"with create_when_missing": {
&hcl.Block{
Type: "import",
Body: hcltest.MockBody(&hcl.BodyContent{
Attributes: hcl.Attributes{
"id": {
Name: "id",
Expr: foo_str_expr,
},
"to": {
Name: "to",
Expr: bar_expr,
},
"create_when_missing": {
Name: "create_when_missing",
Expr: hcltest.MockExprLiteral(cty.BoolVal(true)),
},
},
}),
DefRange: blockRange,
},
&Import{
ToResource: mustAbsResourceInstanceAddr("test_instance.bar").ConfigResource(),
ID: foo_str_expr,
CreateWhenMissing: hcltest.MockExprLiteral(cty.BoolVal(true)),
DeclRange: blockRange,
},
``,
},
"indexed resources": {
&hcl.Block{
Type: "import",
Expand Down
Loading
Loading