Skip to content

Commit e9d07f9

Browse files
committed
backport of commit 1ac4d90
1 parent 50182a4 commit e9d07f9

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

Diff for: internal/terraform/context_plan_import_test.go

+79
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,9 @@ func TestContext2Plan_importIdentityModule(t *testing.T) {
19611961
State: cty.ObjectVal(map[string]cty.Value{
19621962
"id": cty.StringVal("foo"),
19631963
}),
1964+
Identity: cty.ObjectVal(map[string]cty.Value{
1965+
"name": cty.StringVal("bar"),
1966+
}),
19641967
},
19651968
},
19661969
}
@@ -2138,3 +2141,79 @@ import {
21382141
}
21392142
})
21402143
}
2144+
2145+
func TestContext2Plan_importIdentityModuleWithOptional(t *testing.T) {
2146+
p := testProvider("aws")
2147+
m := testModule(t, "import-identity-module")
2148+
2149+
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{
2150+
ResourceTypes: map[string]*configschema.Block{
2151+
"aws_lb": {
2152+
Attributes: map[string]*configschema.Attribute{
2153+
"id": {
2154+
Type: cty.String,
2155+
Computed: true,
2156+
},
2157+
},
2158+
},
2159+
},
2160+
IdentityTypes: map[string]*configschema.Object{
2161+
"aws_lb": {
2162+
Attributes: map[string]*configschema.Attribute{
2163+
"name": {
2164+
Type: cty.String,
2165+
Required: true,
2166+
},
2167+
"something": {
2168+
Type: cty.Number,
2169+
Optional: true,
2170+
},
2171+
},
2172+
Nesting: configschema.NestingSingle,
2173+
},
2174+
},
2175+
})
2176+
wantIdentity := cty.ObjectVal(map[string]cty.Value{
2177+
"name": cty.StringVal("bar"),
2178+
"something": cty.NumberIntVal(42),
2179+
})
2180+
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
2181+
ImportedResources: []providers.ImportedResource{
2182+
{
2183+
TypeName: "aws_lb",
2184+
State: cty.ObjectVal(map[string]cty.Value{
2185+
"id": cty.StringVal("foo"),
2186+
}),
2187+
Identity: wantIdentity,
2188+
},
2189+
},
2190+
}
2191+
ctx := testContext2(t, &ContextOpts{
2192+
Providers: map[addrs.Provider]providers.Factory{
2193+
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
2194+
},
2195+
})
2196+
2197+
diags := ctx.Validate(m, &ValidateOpts{})
2198+
if diags.HasErrors() {
2199+
t.Fatalf("unexpected errors\n%s", diags.Err().Error())
2200+
}
2201+
2202+
plan, diags := ctx.Plan(m, states.NewState(), DefaultPlanOpts)
2203+
if diags.HasErrors() {
2204+
t.Fatalf("unexpected errors: %s", diags.Err())
2205+
}
2206+
2207+
addr := mustResourceInstanceAddr("aws_lb.foo")
2208+
instPlan := plan.Changes.ResourceInstance(addr)
2209+
if instPlan == nil {
2210+
t.Fatalf("no plan for %s at all", addr)
2211+
}
2212+
2213+
identityMatches := instPlan.Importing.Identity.Equals(wantIdentity)
2214+
if !identityMatches.True() {
2215+
t.Errorf("identity does not match\ngot: %s\nwant: %s",
2216+
tfdiags.ObjectToString(instPlan.Importing.Identity),
2217+
tfdiags.ObjectToString(wantIdentity))
2218+
}
2219+
}

0 commit comments

Comments
 (0)