33
44package schema
55
6- import "testing"
6+ import (
7+ "testing"
8+
9+ "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+ )
711
812func TestInternalValidate (t * testing.T ) {
913 r := & ResourceImporter {
@@ -14,3 +18,186 @@ func TestInternalValidate(t *testing.T) {
1418 t .Fatal ("ResourceImporter should not allow State and StateContext to be set" )
1519 }
1620}
21+
22+ func TestImportStatePassthroughWithIdentity (t * testing.T ) {
23+ // shared among all tests, defined once to keep them shorter
24+ identitySchema := map [string ]* Schema {
25+ "email" : {
26+ Type : TypeString ,
27+ RequiredForImport : true ,
28+ },
29+ "region" : {
30+ Type : TypeString ,
31+ OptionalForImport : true ,
32+ },
33+ }
34+
35+ tests := []struct {
36+ name string
37+ idAttributePath string
38+ resourceData * ResourceData
39+ expectedResourceData * ResourceData
40+ expectedError string
41+ }{
42+ {
43+ name : "import from id just sets id" ,
44+ idAttributePath : "email" ,
45+ resourceData : & ResourceData {
46+ identitySchema : identitySchema ,
47+ state : & terraform.InstanceState {
48+ 49+ },
50+ },
51+ expectedResourceData : & ResourceData {
52+ identitySchema : identitySchema ,
53+ state : & terraform.InstanceState {
54+ 55+ },
56+ },
57+ },
58+ {
59+ name : "import from identity sets id and identity" ,
60+ idAttributePath : "email" ,
61+ resourceData : & ResourceData {
62+ identitySchema : identitySchema ,
63+ state : & terraform.InstanceState {
64+ Identity : map [string ]string {
65+ 66+ },
67+ },
68+ },
69+ expectedResourceData : & ResourceData {
70+ identitySchema : identitySchema ,
71+ state : & terraform.InstanceState {
72+ 73+ },
74+ newIdentity : & IdentityData {
75+ schema : identitySchema ,
76+ raw : map [string ]string {
77+ 78+ },
79+ },
80+ },
81+ },
82+ {
83+ name : "import from identity sets id and identity (with region set)" ,
84+ idAttributePath : "email" ,
85+ resourceData : & ResourceData {
86+ identitySchema : identitySchema ,
87+ state : & terraform.InstanceState {
88+ Identity : map [string ]string {
89+ 90+ "region" : "eu-west-1" ,
91+ },
92+ },
93+ },
94+ expectedResourceData : & ResourceData {
95+ identitySchema : identitySchema ,
96+ state : & terraform.InstanceState {
97+ 98+ },
99+ newIdentity : & IdentityData {
100+ schema : identitySchema ,
101+ raw : map [string ]string {
102+ 103+ "region" : "eu-west-1" ,
104+ },
105+ },
106+ },
107+ },
108+ {
109+ name : "import from identity fails without required field" ,
110+ idAttributePath : "email" ,
111+ resourceData : & ResourceData {
112+ identitySchema : identitySchema ,
113+ state : & terraform.InstanceState {
114+ Identity : map [string ]string {
115+ "region" : "eu-west-1" ,
116+ },
117+ },
118+ },
119+ expectedError : "expected identity to contain key email" ,
120+ },
121+ {
122+ name : "import from identity fails if attribute is not a string" ,
123+ idAttributePath : "number" ,
124+ resourceData : & ResourceData {
125+ identitySchema : map [string ]* Schema {
126+ "number" : {
127+ Type : TypeInt ,
128+ RequiredForImport : true ,
129+ },
130+ },
131+ state : & terraform.InstanceState {
132+ Identity : map [string ]string {
133+ "number" : "1" ,
134+ },
135+ },
136+ },
137+ expectedError : "expected identity key number to be a string, was: int" ,
138+ },
139+ {
140+ name : "import from identity fails without schema" ,
141+ idAttributePath : "email" ,
142+ resourceData : & ResourceData {
143+ state : & terraform.InstanceState {
144+ Identity : map [string ]string {
145+ 146+ },
147+ },
148+ },
149+ expectedError : "error getting identity: Resource does not have Identity schema. Please set one in order to use Identity(). This is always a problem in the provider code." ,
150+ },
151+ }
152+
153+ for _ , test := range tests {
154+ t .Run (test .name , func (t * testing.T ) {
155+ results , err := ImportStatePassthroughWithIdentity (test .idAttributePath )(nil , test .resourceData , nil )
156+ if err != nil {
157+ if test .expectedError == "" {
158+ t .Fatalf ("unexpected error: %s" , err )
159+ }
160+ if err .Error () != test .expectedError {
161+ t .Fatalf ("expected error: %s, got: %s" , test .expectedError , err )
162+ }
163+ return // we don't expect any results if there is an error
164+ }
165+ if len (results ) != 1 {
166+ t .Fatalf ("expected 1 result, got: %d" , len (results ))
167+ }
168+ // compare id and identity in resource data
169+ if results [0 ].Id () != test .expectedResourceData .Id () {
170+ t .Fatalf ("expected id: %s, got: %s" , test .expectedResourceData .Id (), results [0 ].Id ())
171+ }
172+ // compare identity
173+ expectedIdentity , err := test .expectedResourceData .Identity ()
174+ if err != nil {
175+ t .Fatalf ("unexpected error: %s" , err )
176+ }
177+ resultIdentity , err := results [0 ].Identity ()
178+ if err != nil {
179+ t .Fatalf ("unexpected error: %s" , err )
180+ }
181+
182+ // check whether all result identity attributes exist as expected
183+ for key := range expectedIdentity .schema {
184+ expected := expectedIdentity .getRaw (key )
185+ if expected .Exists {
186+ result := resultIdentity .getRaw (key )
187+ if ! result .Exists {
188+ t .Fatalf ("expected identity attribute %s to exist" , key )
189+ }
190+ if expected .Value != result .Value {
191+ t .Fatalf ("expected identity attribute %s to be %s, got: %s" , key , expected .Value , result .Value )
192+ }
193+ }
194+ }
195+ // check whether there are no additional attributes in the result identity
196+ for key := range resultIdentity .schema {
197+ if _ , ok := expectedIdentity .schema [key ]; ! ok {
198+ t .Fatalf ("unexpected identity attribute %s" , key )
199+ }
200+ }
201+ })
202+ }
203+ }
0 commit comments