@@ -4098,3 +4098,203 @@ resource "test_object" "a" {
40984098 }
40994099 }
41004100}
4101+
4102+ func TestContext2Plan_importResourceBasic (t * testing.T ) {
4103+ addr := mustResourceInstanceAddr ("test_object.a" )
4104+ m := testModuleInline (t , map [string ]string {
4105+ "main.tf" : `
4106+ resource "test_object" "a" {
4107+ test_string = "foo"
4108+ }
4109+
4110+ import {
4111+ to = test_object.a
4112+ id = "123"
4113+ }
4114+ ` ,
4115+ })
4116+
4117+ p := simpleMockProvider ()
4118+ ctx := testContext2 (t , & ContextOpts {
4119+ Providers : map [addrs.Provider ]providers.Factory {
4120+ addrs .NewDefaultProvider ("test" ): testProviderFuncFixed (p ),
4121+ },
4122+ })
4123+ p .ReadResourceResponse = & providers.ReadResourceResponse {
4124+ NewState : cty .ObjectVal (map [string ]cty.Value {
4125+ "test_string" : cty .StringVal ("foo" ),
4126+ }),
4127+ }
4128+ p .ImportResourceStateResponse = & providers.ImportResourceStateResponse {
4129+ ImportedResources : []providers.ImportedResource {
4130+ {
4131+ TypeName : "test_object" ,
4132+ State : cty .ObjectVal (map [string ]cty.Value {
4133+ "test_string" : cty .StringVal ("foo" ),
4134+ }),
4135+ },
4136+ },
4137+ }
4138+
4139+ plan , diags := ctx .Plan (m , states .NewState (), DefaultPlanOpts )
4140+ if diags .HasErrors () {
4141+ t .Fatalf ("unexpected errors\n %s" , diags .Err ().Error ())
4142+ }
4143+
4144+ t .Run (addr .String (), func (t * testing.T ) {
4145+ instPlan := plan .Changes .ResourceInstance (addr )
4146+ if instPlan == nil {
4147+ t .Fatalf ("no plan for %s at all" , addr )
4148+ }
4149+
4150+ if got , want := instPlan .Addr , addr ; ! got .Equal (want ) {
4151+ t .Errorf ("wrong current address\n got: %s\n want: %s" , got , want )
4152+ }
4153+ if got , want := instPlan .PrevRunAddr , addr ; ! got .Equal (want ) {
4154+ t .Errorf ("wrong previous run address\n got: %s\n want: %s" , got , want )
4155+ }
4156+ if got , want := instPlan .Action , plans .NoOp ; got != want {
4157+ t .Errorf ("wrong planned action\n got: %s\n want: %s" , got , want )
4158+ }
4159+ if got , want := instPlan .ActionReason , plans .ResourceInstanceChangeNoReason ; got != want {
4160+ t .Errorf ("wrong action reason\n got: %s\n want: %s" , got , want )
4161+ }
4162+ if ! instPlan .Importing {
4163+ t .Errorf ("expected import change, got non-import change" )
4164+ }
4165+ })
4166+ }
4167+
4168+ func TestContext2Plan_importResourceUpdate (t * testing.T ) {
4169+ addr := mustResourceInstanceAddr ("test_object.a" )
4170+ m := testModuleInline (t , map [string ]string {
4171+ "main.tf" : `
4172+ resource "test_object" "a" {
4173+ test_string = "bar"
4174+ }
4175+
4176+ import {
4177+ to = test_object.a
4178+ id = "123"
4179+ }
4180+ ` ,
4181+ })
4182+
4183+ p := simpleMockProvider ()
4184+ ctx := testContext2 (t , & ContextOpts {
4185+ Providers : map [addrs.Provider ]providers.Factory {
4186+ addrs .NewDefaultProvider ("test" ): testProviderFuncFixed (p ),
4187+ },
4188+ })
4189+ p .ReadResourceResponse = & providers.ReadResourceResponse {
4190+ NewState : cty .ObjectVal (map [string ]cty.Value {
4191+ "test_string" : cty .StringVal ("foo" ),
4192+ }),
4193+ }
4194+ p .ImportResourceStateResponse = & providers.ImportResourceStateResponse {
4195+ ImportedResources : []providers.ImportedResource {
4196+ {
4197+ TypeName : "test_object" ,
4198+ State : cty .ObjectVal (map [string ]cty.Value {
4199+ "test_string" : cty .StringVal ("foo" ),
4200+ }),
4201+ },
4202+ },
4203+ }
4204+
4205+ plan , diags := ctx .Plan (m , states .NewState (), DefaultPlanOpts )
4206+ if diags .HasErrors () {
4207+ t .Fatalf ("unexpected errors\n %s" , diags .Err ().Error ())
4208+ }
4209+
4210+ t .Run (addr .String (), func (t * testing.T ) {
4211+ instPlan := plan .Changes .ResourceInstance (addr )
4212+ if instPlan == nil {
4213+ t .Fatalf ("no plan for %s at all" , addr )
4214+ }
4215+
4216+ if got , want := instPlan .Addr , addr ; ! got .Equal (want ) {
4217+ t .Errorf ("wrong current address\n got: %s\n want: %s" , got , want )
4218+ }
4219+ if got , want := instPlan .PrevRunAddr , addr ; ! got .Equal (want ) {
4220+ t .Errorf ("wrong previous run address\n got: %s\n want: %s" , got , want )
4221+ }
4222+ if got , want := instPlan .Action , plans .Update ; got != want {
4223+ t .Errorf ("wrong planned action\n got: %s\n want: %s" , got , want )
4224+ }
4225+ if got , want := instPlan .ActionReason , plans .ResourceInstanceChangeNoReason ; got != want {
4226+ t .Errorf ("wrong action reason\n got: %s\n want: %s" , got , want )
4227+ }
4228+ if ! instPlan .Importing {
4229+ t .Errorf ("expected import change, got non-import change" )
4230+ }
4231+ })
4232+ }
4233+
4234+ func TestContext2Plan_importResourceReplace (t * testing.T ) {
4235+ addr := mustResourceInstanceAddr ("test_object.a" )
4236+ m := testModuleInline (t , map [string ]string {
4237+ "main.tf" : `
4238+ resource "test_object" "a" {
4239+ test_string = "bar"
4240+ }
4241+
4242+ import {
4243+ to = test_object.a
4244+ id = "123"
4245+ }
4246+ ` ,
4247+ })
4248+
4249+ p := simpleMockProvider ()
4250+ ctx := testContext2 (t , & ContextOpts {
4251+ Providers : map [addrs.Provider ]providers.Factory {
4252+ addrs .NewDefaultProvider ("test" ): testProviderFuncFixed (p ),
4253+ },
4254+ })
4255+ p .ReadResourceResponse = & providers.ReadResourceResponse {
4256+ NewState : cty .ObjectVal (map [string ]cty.Value {
4257+ "test_string" : cty .StringVal ("foo" ),
4258+ }),
4259+ }
4260+ p .ImportResourceStateResponse = & providers.ImportResourceStateResponse {
4261+ ImportedResources : []providers.ImportedResource {
4262+ {
4263+ TypeName : "test_object" ,
4264+ State : cty .ObjectVal (map [string ]cty.Value {
4265+ "test_string" : cty .StringVal ("foo" ),
4266+ }),
4267+ },
4268+ },
4269+ }
4270+
4271+ plan , diags := ctx .Plan (m , states .NewState (), & PlanOpts {
4272+ Mode : plans .NormalMode ,
4273+ ForceReplace : []addrs.AbsResourceInstance {
4274+ addr ,
4275+ },
4276+ })
4277+ if diags .HasErrors () {
4278+ t .Fatalf ("unexpected errors\n %s" , diags .Err ().Error ())
4279+ }
4280+
4281+ t .Run (addr .String (), func (t * testing.T ) {
4282+ instPlan := plan .Changes .ResourceInstance (addr )
4283+ if instPlan == nil {
4284+ t .Fatalf ("no plan for %s at all" , addr )
4285+ }
4286+
4287+ if got , want := instPlan .Addr , addr ; ! got .Equal (want ) {
4288+ t .Errorf ("wrong current address\n got: %s\n want: %s" , got , want )
4289+ }
4290+ if got , want := instPlan .PrevRunAddr , addr ; ! got .Equal (want ) {
4291+ t .Errorf ("wrong previous run address\n got: %s\n want: %s" , got , want )
4292+ }
4293+ if got , want := instPlan .Action , plans .DeleteThenCreate ; got != want {
4294+ t .Errorf ("wrong planned action\n got: %s\n want: %s" , got , want )
4295+ }
4296+ if ! instPlan .Importing {
4297+ t .Errorf ("expected import change, got non-import change" )
4298+ }
4299+ })
4300+ }
0 commit comments