Skip to content

Fix import with optional identity attributes #36887

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

Merged
merged 4 commits into from
Apr 16, 2025
Merged
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
8 changes: 8 additions & 0 deletions internal/configs/configschema/implied_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ func (o *Object) ImpliedType() cty.Type {
return o.specType().WithoutOptionalAttributesDeep()
}

// ConfigType returns a cty.Type that can be used to decode a configuration
// object using the receiving block schema.
//
// ConfigType will preserve optional attributes
func (o *Object) ConfigType() cty.Type {
return o.specType()
}

// specType returns the cty.Type used for decoding a NestedType Attribute using
// the receiving block schema.
func (o *Object) specType() cty.Type {
Expand Down
79 changes: 79 additions & 0 deletions internal/terraform/context_plan_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,9 @@ func TestContext2Plan_importIdentityModule(t *testing.T) {
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
Identity: cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("bar"),
}),
},
},
}
Expand Down Expand Up @@ -2138,3 +2141,79 @@ import {
}
})
}

func TestContext2Plan_importIdentityModuleWithOptional(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-identity-module")

p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{
ResourceTypes: map[string]*configschema.Block{
"aws_lb": {
Attributes: map[string]*configschema.Attribute{
"id": {
Type: cty.String,
Computed: true,
},
},
},
},
IdentityTypes: map[string]*configschema.Object{
"aws_lb": {
Attributes: map[string]*configschema.Attribute{
"name": {
Type: cty.String,
Required: true,
},
"something": {
Type: cty.Number,
Optional: true,
},
},
Nesting: configschema.NestingSingle,
},
},
})
wantIdentity := cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("bar"),
"something": cty.NumberIntVal(42),
})
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "aws_lb",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
Identity: wantIdentity,
},
},
}
ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
},
})

diags := ctx.Validate(m, &ValidateOpts{})
if diags.HasErrors() {
t.Fatalf("unexpected errors\n%s", diags.Err().Error())
}

plan, diags := ctx.Plan(m, states.NewState(), DefaultPlanOpts)
if diags.HasErrors() {
t.Fatalf("unexpected errors: %s", diags.Err())
}

addr := mustResourceInstanceAddr("aws_lb.foo")
instPlan := plan.Changes.ResourceInstance(addr)
if instPlan == nil {
t.Fatalf("no plan for %s at all", addr)
}

identityMatches := instPlan.Importing.Identity.Equals(wantIdentity)
if !identityMatches.True() {
t.Errorf("identity does not match\ngot: %s\nwant: %s",
tfdiags.ObjectToString(instPlan.Importing.Identity),
tfdiags.ObjectToString(wantIdentity))
}
}
2 changes: 1 addition & 1 deletion internal/terraform/eval_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func evaluateImportIdentityExpression(expr hcl.Expression, identity *configschem
// that context.
ctx = evalContextForModuleInstance(ctx, addrs.RootModuleInstance)
scope := ctx.EvaluationScope(nil, nil, keyData)
importIdentityVal, evalDiags := scope.EvalExpr(expr, identity.ImpliedType())
importIdentityVal, evalDiags := scope.EvalExpr(expr, identity.ConfigType())
if evalDiags.HasErrors() {
// TODO? Do we need to improve the error message?
return cty.NilVal, evalDiags
Expand Down
10 changes: 9 additions & 1 deletion internal/terraform/node_resource_plan_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,15 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
}

if importing {
change.Importing = &plans.Importing{Target: n.importTarget}
// There is a subtle difference between the import by identity
// and the import by ID. When importing by identity, we need to
// make sure to use the complete identity return by the provider
// instead of the (potential) incomplete one from the configuration.
if n.importTarget.Type().IsObjectType() {
change.Importing = &plans.Importing{Target: instanceRefreshState.Identity}
} else {
change.Importing = &plans.Importing{Target: n.importTarget}
}
}

// FIXME: here we udpate the change to reflect the reason for
Expand Down
5 changes: 5 additions & 0 deletions internal/tfdiags/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func ObjectToString(obj cty.Value) string {
result += ","
}

if val.IsNull() {
result += fmt.Sprintf("%s=<null>", keyStr)
continue
}

switch val.Type() {
case cty.Bool:
result += fmt.Sprintf("%s=%t", keyStr, val.True())
Expand Down
8 changes: 8 additions & 0 deletions internal/tfdiags/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func Test_ObjectToString(t *testing.T) {
}),
expected: "list=[a,b,c],string=hello",
},
{
name: "with null value",
value: cty.ObjectVal(map[string]cty.Value{
"string": cty.StringVal("hello"),
"null": cty.NullVal(cty.String),
}),
expected: "null=<null>,string=hello",
},
}

for _, tc := range testCases {
Expand Down
Loading