@@ -1890,3 +1890,209 @@ output "foo" {
18901890 t .Errorf ("should have reported the cycle to contain the target resource, but got %s" , got )
18911891 }
18921892}
1893+
1894+ func TestContext2Plan_importIdentityModule (t * testing.T ) {
1895+ p := testProvider ("aws" )
1896+ m := testModule (t , "import-identity-module" )
1897+
1898+ p .GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema (& providerSchema {
1899+ ResourceTypes : map [string ]* configschema.Block {
1900+ "aws_lb" : {
1901+ Attributes : map [string ]* configschema.Attribute {
1902+ "id" : {
1903+ Type : cty .String ,
1904+ Computed : true ,
1905+ },
1906+ },
1907+ },
1908+ },
1909+ IdentityTypes : map [string ]* configschema.Object {
1910+ "aws_lb" : {
1911+ Attributes : map [string ]* configschema.Attribute {
1912+ "name" : {
1913+ Type : cty .String ,
1914+ Required : true ,
1915+ },
1916+ },
1917+ Nesting : configschema .NestingSingle ,
1918+ },
1919+ },
1920+ })
1921+ p .ImportResourceStateResponse = & providers.ImportResourceStateResponse {
1922+ ImportedResources : []providers.ImportedResource {
1923+ {
1924+ TypeName : "aws_lb" ,
1925+ State : cty .ObjectVal (map [string ]cty.Value {
1926+ "id" : cty .StringVal ("foo" ),
1927+ }),
1928+ },
1929+ },
1930+ }
1931+ ctx := testContext2 (t , & ContextOpts {
1932+ Providers : map [addrs.Provider ]providers.Factory {
1933+ addrs .NewDefaultProvider ("aws" ): testProviderFuncFixed (p ),
1934+ },
1935+ })
1936+
1937+ diags := ctx .Validate (m , & ValidateOpts {})
1938+ if diags .HasErrors () {
1939+ t .Fatalf ("unexpected errors\n %s" , diags .Err ().Error ())
1940+ }
1941+
1942+ _ , diags = ctx .Plan (m , states .NewState (), DefaultPlanOpts )
1943+ if diags .HasErrors () {
1944+ t .Fatalf ("unexpected errors: %s" , diags .Err ())
1945+ }
1946+ }
1947+
1948+ func TestContext2Plan_importIdentityMissingRequired (t * testing.T ) {
1949+ p := testProvider ("aws" )
1950+ m := testModule (t , "import-identity-module" )
1951+
1952+ p .GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema (& providerSchema {
1953+ ResourceTypes : map [string ]* configschema.Block {
1954+ "aws_lb" : {
1955+ Attributes : map [string ]* configschema.Attribute {
1956+ "id" : {
1957+ Type : cty .String ,
1958+ Computed : true ,
1959+ },
1960+ },
1961+ },
1962+ },
1963+ IdentityTypes : map [string ]* configschema.Object {
1964+ "aws_lb" : {
1965+ Attributes : map [string ]* configschema.Attribute {
1966+ "name" : {
1967+ Type : cty .String ,
1968+ Required : true ,
1969+ },
1970+ "id" : {
1971+ Type : cty .String ,
1972+ Required : true ,
1973+ },
1974+ },
1975+ Nesting : configschema .NestingSingle ,
1976+ },
1977+ },
1978+ })
1979+
1980+ ctx := testContext2 (t , & ContextOpts {
1981+ Providers : map [addrs.Provider ]providers.Factory {
1982+ addrs .NewDefaultProvider ("aws" ): testProviderFuncFixed (p ),
1983+ },
1984+ })
1985+
1986+ diags := ctx .Validate (m , & ValidateOpts {})
1987+
1988+ if len (diags ) != 1 {
1989+ t .Fatalf ("expected one diag, got %d: %s" , len (diags ), diags .ErrWithWarnings ())
1990+ }
1991+
1992+ got := diags .Err ().Error ()
1993+ if ! strings .Contains (got , "Invalid expression value:" ) {
1994+ t .Errorf ("should have reported an invalid expression value, but got %s" , got )
1995+ }
1996+ }
1997+
1998+ func TestContext2Plan_importIdentityResourceAlreadyInState (t * testing.T ) {
1999+ addr := mustResourceInstanceAddr ("test_object.a" )
2000+ m := testModuleInline (t , map [string ]string {
2001+ "main.tf" : `
2002+ resource "test_object" "a" {
2003+ test_string = "foo"
2004+ }
2005+
2006+ import {
2007+ to = test_object.a
2008+ identity = {
2009+ id = "123"
2010+ }
2011+ }
2012+ ` ,
2013+ })
2014+
2015+ p := simpleMockProvider ()
2016+ ctx := testContext2 (t , & ContextOpts {
2017+ Providers : map [addrs.Provider ]providers.Factory {
2018+ addrs .NewDefaultProvider ("test" ): testProviderFuncFixed (p ),
2019+ },
2020+ })
2021+ p .GetProviderSchemaResponse = & providers.GetProviderSchemaResponse {
2022+ Provider : providers.Schema {Body : simpleTestSchema ()},
2023+ ResourceTypes : map [string ]providers.Schema {
2024+ "test_object" : {
2025+ Body : simpleTestSchema (),
2026+ Identity : & configschema.Object {
2027+ Attributes : map [string ]* configschema.Attribute {
2028+ "id" : {
2029+ Type : cty .String ,
2030+ Required : true ,
2031+ },
2032+ },
2033+ Nesting : configschema .NestingSingle ,
2034+ },
2035+ },
2036+ },
2037+ }
2038+ p .ReadResourceResponse = & providers.ReadResourceResponse {
2039+ NewState : cty .ObjectVal (map [string ]cty.Value {
2040+ "test_string" : cty .StringVal ("foo" ),
2041+ }),
2042+ }
2043+ p .ImportResourceStateResponse = & providers.ImportResourceStateResponse {
2044+ ImportedResources : []providers.ImportedResource {
2045+ {
2046+ TypeName : "test_object" ,
2047+ State : cty .ObjectVal (map [string ]cty.Value {
2048+ "test_string" : cty .StringVal ("foo" ),
2049+ }),
2050+ },
2051+ },
2052+ }
2053+
2054+ state := states .NewState ()
2055+ root := state .EnsureModule (addrs .RootModuleInstance )
2056+ root .SetResourceInstanceCurrent (
2057+ mustResourceInstanceAddr ("test_object.a" ).Resource ,
2058+ & states.ResourceInstanceObjectSrc {
2059+ Status : states .ObjectReady ,
2060+ AttrsJSON : []byte (`{"test_string":"foo"}` ),
2061+ IdentityJSON : []byte (`{"id":"123"}` ),
2062+ },
2063+ mustProviderConfig (`provider["registry.terraform.io/hashicorp/test"]` ),
2064+ )
2065+
2066+ diags := ctx .Validate (m , & ValidateOpts {})
2067+ if diags .HasErrors () {
2068+ t .Fatalf ("unexpected errors\n %s" , diags .Err ().Error ())
2069+ }
2070+
2071+ plan , diags := ctx .Plan (m , state , DefaultPlanOpts )
2072+ if diags .HasErrors () {
2073+ t .Fatalf ("unexpected errors\n %s" , diags .Err ().Error ())
2074+ }
2075+
2076+ t .Run (addr .String (), func (t * testing.T ) {
2077+ instPlan := plan .Changes .ResourceInstance (addr )
2078+ if instPlan == nil {
2079+ t .Fatalf ("no plan for %s at all" , addr )
2080+ }
2081+
2082+ if got , want := instPlan .Addr , addr ; ! got .Equal (want ) {
2083+ t .Errorf ("wrong current address\n got: %s\n want: %s" , got , want )
2084+ }
2085+ if got , want := instPlan .PrevRunAddr , addr ; ! got .Equal (want ) {
2086+ t .Errorf ("wrong previous run address\n got: %s\n want: %s" , got , want )
2087+ }
2088+ if got , want := instPlan .Action , plans .NoOp ; got != want {
2089+ t .Errorf ("wrong planned action\n got: %s\n want: %s" , got , want )
2090+ }
2091+ if got , want := instPlan .ActionReason , plans .ResourceInstanceChangeNoReason ; got != want {
2092+ t .Errorf ("wrong action reason\n got: %s\n want: %s" , got , want )
2093+ }
2094+ if instPlan .Importing != nil {
2095+ t .Errorf ("expected non-import change, got import change %#v" , instPlan .Importing )
2096+ }
2097+ })
2098+ }
0 commit comments