Skip to content

Commit 7d71ed5

Browse files
committed
Validate identity in import response
1 parent 7414a3f commit 7d71ed5

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

internal/terraform/context_plan_import_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,3 +2217,61 @@ func TestContext2Plan_importIdentityModuleWithOptional(t *testing.T) {
22172217
tfdiags.ObjectToString(wantIdentity))
22182218
}
22192219
}
2220+
2221+
func TestContext2Plan_importIdentityMissingResponse(t *testing.T) {
2222+
p := testProvider("aws")
2223+
m := testModule(t, "import-identity-module")
2224+
2225+
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{
2226+
ResourceTypes: map[string]*configschema.Block{
2227+
"aws_lb": {
2228+
Attributes: map[string]*configschema.Attribute{
2229+
"id": {
2230+
Type: cty.String,
2231+
Computed: true,
2232+
},
2233+
},
2234+
},
2235+
},
2236+
IdentityTypes: map[string]*configschema.Object{
2237+
"aws_lb": {
2238+
Attributes: map[string]*configschema.Attribute{
2239+
"name": {
2240+
Type: cty.String,
2241+
Required: true,
2242+
},
2243+
},
2244+
Nesting: configschema.NestingSingle,
2245+
},
2246+
},
2247+
})
2248+
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
2249+
ImportedResources: []providers.ImportedResource{
2250+
{
2251+
TypeName: "aws_lb",
2252+
State: cty.ObjectVal(map[string]cty.Value{
2253+
"id": cty.StringVal("foo"),
2254+
}),
2255+
// No identity returned
2256+
},
2257+
},
2258+
}
2259+
ctx := testContext2(t, &ContextOpts{
2260+
Providers: map[addrs.Provider]providers.Factory{
2261+
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
2262+
},
2263+
})
2264+
2265+
diags := ctx.Validate(m, &ValidateOpts{})
2266+
if diags.HasErrors() {
2267+
t.Fatalf("unexpected errors\n%s", diags.Err().Error())
2268+
}
2269+
2270+
_, diags = ctx.Plan(m, states.NewState(), DefaultPlanOpts)
2271+
if !diags.HasErrors() {
2272+
t.Fatal("succeeded; want errors")
2273+
}
2274+
if got, want := diags.Err().Error(), `import of aws_lb.foo didn't return an identity`; !strings.Contains(got, want) {
2275+
t.Fatalf("wrong error:\ngot: %s\nwant: message containing %q", got, want)
2276+
}
2277+
}

internal/terraform/node_resource_plan_instance.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,12 @@ func (n *NodePlannableResourceInstance) importState(ctx EvalContext, addr addrs.
753753
return nil, deferred, diags
754754
}
755755

756+
// Providers are supposed to return an identity when importing by identity
757+
if importTarget.Type().IsObjectType() && imported[0].Identity.IsNull() {
758+
diags = diags.Append(fmt.Errorf("import of %s didn't return an identity", n.Addr.String()))
759+
return nil, deferred, diags
760+
}
761+
756762
// Providers are supposed to return null values for all write-only attributes
757763
writeOnlyDiags := ephemeral.ValidateWriteOnlyAttributes(
758764
"Import returned a non-null value for a write-only attribute",
@@ -782,7 +788,6 @@ func (n *NodePlannableResourceInstance) importState(ctx EvalContext, addr addrs.
782788
importType, importValue,
783789
),
784790
))
785-
786791
}
787792

788793
// refresh

0 commit comments

Comments
 (0)