@@ -1890,3 +1890,215 @@ output "foo" {
1890
1890
t .Errorf ("should have reported the cycle to contain the target resource, but got %s" , got )
1891
1891
}
1892
1892
}
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
+ Identity : cty .ObjectVal (map [string ]cty.Value {
2043
+ "id" : cty .StringVal ("123" ),
2044
+ }),
2045
+ }
2046
+ p .ImportResourceStateResponse = & providers.ImportResourceStateResponse {
2047
+ ImportedResources : []providers.ImportedResource {
2048
+ {
2049
+ TypeName : "test_object" ,
2050
+ State : cty .ObjectVal (map [string ]cty.Value {
2051
+ "test_string" : cty .StringVal ("foo" ),
2052
+ }),
2053
+ Identity : cty .ObjectVal (map [string ]cty.Value {
2054
+ "id" : cty .StringVal ("123" ),
2055
+ }),
2056
+ },
2057
+ },
2058
+ }
2059
+
2060
+ state := states .NewState ()
2061
+ root := state .EnsureModule (addrs .RootModuleInstance )
2062
+ root .SetResourceInstanceCurrent (
2063
+ mustResourceInstanceAddr ("test_object.a" ).Resource ,
2064
+ & states.ResourceInstanceObjectSrc {
2065
+ Status : states .ObjectReady ,
2066
+ AttrsJSON : []byte (`{"test_string":"foo"}` ),
2067
+ IdentityJSON : []byte (`{"id":"123"}` ),
2068
+ },
2069
+ mustProviderConfig (`provider["registry.terraform.io/hashicorp/test"]` ),
2070
+ )
2071
+
2072
+ diags := ctx .Validate (m , & ValidateOpts {})
2073
+ if diags .HasErrors () {
2074
+ t .Fatalf ("unexpected errors\n %s" , diags .Err ().Error ())
2075
+ }
2076
+
2077
+ plan , diags := ctx .Plan (m , state , DefaultPlanOpts )
2078
+ if diags .HasErrors () {
2079
+ t .Fatalf ("unexpected errors\n %s" , diags .Err ().Error ())
2080
+ }
2081
+
2082
+ t .Run (addr .String (), func (t * testing.T ) {
2083
+ instPlan := plan .Changes .ResourceInstance (addr )
2084
+ if instPlan == nil {
2085
+ t .Fatalf ("no plan for %s at all" , addr )
2086
+ }
2087
+
2088
+ if got , want := instPlan .Addr , addr ; ! got .Equal (want ) {
2089
+ t .Errorf ("wrong current address\n got: %s\n want: %s" , got , want )
2090
+ }
2091
+ if got , want := instPlan .PrevRunAddr , addr ; ! got .Equal (want ) {
2092
+ t .Errorf ("wrong previous run address\n got: %s\n want: %s" , got , want )
2093
+ }
2094
+ if got , want := instPlan .Action , plans .NoOp ; got != want {
2095
+ t .Errorf ("wrong planned action\n got: %s\n want: %s" , got , want )
2096
+ }
2097
+ if got , want := instPlan .ActionReason , plans .ResourceInstanceChangeNoReason ; got != want {
2098
+ t .Errorf ("wrong action reason\n got: %s\n want: %s" , got , want )
2099
+ }
2100
+ if instPlan .Importing != nil {
2101
+ t .Errorf ("expected non-import change, got import change %#v" , instPlan .Importing )
2102
+ }
2103
+ })
2104
+ }
0 commit comments