@@ -91,12 +91,42 @@ func Test_resourceValidateRequiredTagsInterceptor(t *testing.T) {
9191 },
9292 }
9393
94+ // Null tags
9495 attrs := map [string ]tftypes.Value {
9596 "name" : tftypes .NewValue (tftypes .String , "test" ),
9697 "tags" : tftypes .NewValue (tftypes.Map {ElementType : tftypes .String }, nil ),
9798 }
9899 rawVal := tftypes .NewValue (resourceSchema .Type ().TerraformType (ctx ), attrs )
99100
101+ // Partial required tags
102+ attrsPartial := map [string ]tftypes.Value {
103+ "name" : tftypes .NewValue (tftypes .String , "test" ),
104+ "tags" : tftypes .NewValue (tftypes.Map {ElementType : tftypes .String }, map [string ]tftypes.Value {
105+ "bar" : tftypes .NewValue (tftypes .String , nil ),
106+ }),
107+ }
108+ rawValPartial := tftypes .NewValue (resourceSchema .Type ().TerraformType (ctx ), attrsPartial )
109+
110+ // All required tags
111+ attrsRequired := map [string ]tftypes.Value {
112+ "name" : tftypes .NewValue (tftypes .String , "test" ),
113+ "tags" : tftypes .NewValue (tftypes.Map {ElementType : tftypes .String }, map [string ]tftypes.Value {
114+ "foo" : tftypes .NewValue (tftypes .String , nil ),
115+ "bar" : tftypes .NewValue (tftypes .String , nil ),
116+ }),
117+ }
118+ rawValRequired := tftypes .NewValue (resourceSchema .Type ().TerraformType (ctx ), attrsRequired )
119+
120+ // Unknown tag values
121+ attrsUnknown := map [string ]tftypes.Value {
122+ "name" : tftypes .NewValue (tftypes .String , "test" ),
123+ "tags" : tftypes .NewValue (tftypes.Map {ElementType : tftypes .String }, map [string ]tftypes.Value {
124+ "foo" : tftypes .NewValue (tftypes .String , tftypes .UnknownValue ),
125+ "bar" : tftypes .NewValue (tftypes .String , tftypes .UnknownValue ),
126+ }),
127+ }
128+ rawValUnknown := tftypes .NewValue (resourceSchema .Type ().TerraformType (ctx ), attrsUnknown )
129+
100130 tests := []struct {
101131 name string
102132 opts interceptorOptions [resource.ModifyPlanRequest , resource.ModifyPlanResponse ]
@@ -135,6 +165,93 @@ func Test_resourceValidateRequiredTagsInterceptor(t *testing.T) {
135165 ),
136166 },
137167 },
168+ {
169+ name : "create, partial tags" ,
170+ opts : interceptorOptions [resource.ModifyPlanRequest , resource.ModifyPlanResponse ]{
171+ c : mockRequiredTagsClient {},
172+ request : & resource.ModifyPlanRequest {
173+ Config : tfsdk.Config {
174+ Raw : rawValPartial ,
175+ Schema : resourceSchema ,
176+ },
177+ State : tfsdk.State {
178+ Raw : tftypes .NewValue (resourceSchema .Type ().TerraformType (ctx ), nil ), // Raw state is null on creation
179+ Schema : resourceSchema ,
180+ },
181+ Plan : tfsdk.Plan {
182+ Raw : rawValPartial ,
183+ Schema : resourceSchema ,
184+ },
185+ },
186+ response : & resource.ModifyPlanResponse {
187+ Plan : tfsdk.Plan {
188+ Raw : rawValPartial ,
189+ Schema : resourceSchema ,
190+ },
191+ },
192+ when : Before ,
193+ },
194+ wantDiags : diag.Diagnostics {diag .NewAttributeErrorDiagnostic (
195+ path .Root (names .AttrTags ),
196+ "Missing Required Tags" ,
197+ "An organizational tag policy requires the following tags for aws_test: [foo]" ,
198+ ),
199+ },
200+ },
201+ {
202+ name : "create, required tags" ,
203+ opts : interceptorOptions [resource.ModifyPlanRequest , resource.ModifyPlanResponse ]{
204+ c : mockRequiredTagsClient {},
205+ request : & resource.ModifyPlanRequest {
206+ Config : tfsdk.Config {
207+ Raw : rawValRequired ,
208+ Schema : resourceSchema ,
209+ },
210+ State : tfsdk.State {
211+ Raw : tftypes .NewValue (resourceSchema .Type ().TerraformType (ctx ), nil ), // Raw state is null on creation
212+ Schema : resourceSchema ,
213+ },
214+ Plan : tfsdk.Plan {
215+ Raw : rawValRequired ,
216+ Schema : resourceSchema ,
217+ },
218+ },
219+ response : & resource.ModifyPlanResponse {
220+ Plan : tfsdk.Plan {
221+ Raw : rawValRequired ,
222+ Schema : resourceSchema ,
223+ },
224+ },
225+ when : Before ,
226+ },
227+ },
228+ {
229+ name : "create, unknown tag values" ,
230+ opts : interceptorOptions [resource.ModifyPlanRequest , resource.ModifyPlanResponse ]{
231+ c : mockRequiredTagsClient {},
232+ request : & resource.ModifyPlanRequest {
233+ Config : tfsdk.Config {
234+ Raw : rawValUnknown ,
235+ Schema : resourceSchema ,
236+ },
237+ State : tfsdk.State {
238+ Raw : tftypes .NewValue (resourceSchema .Type ().TerraformType (ctx ), nil ), // Raw state is null on creation
239+ Schema : resourceSchema ,
240+ },
241+ Plan : tfsdk.Plan {
242+ Raw : rawValUnknown ,
243+ Schema : resourceSchema ,
244+ },
245+ },
246+ response : & resource.ModifyPlanResponse {
247+ Plan : tfsdk.Plan {
248+ Raw : rawValUnknown ,
249+ Schema : resourceSchema ,
250+ },
251+ },
252+ when : Before ,
253+ },
254+ },
138255 {
139256 name : "update, no tags change" ,
140257 opts : interceptorOptions [resource.ModifyPlanRequest , resource.ModifyPlanResponse ]{
@@ -162,6 +279,93 @@ func Test_resourceValidateRequiredTagsInterceptor(t *testing.T) {
162279 when : Before ,
163280 },
164281 },
282+ {
283+ name : "update, add required" ,
284+ opts : interceptorOptions [resource.ModifyPlanRequest , resource.ModifyPlanResponse ]{
285+ c : mockRequiredTagsClient {},
286+ request : & resource.ModifyPlanRequest {
287+ Config : tfsdk.Config {
288+ Raw : rawValRequired ,
289+ Schema : resourceSchema ,
290+ },
291+ State : tfsdk.State {
292+ Raw : rawValPartial ,
293+ Schema : resourceSchema ,
294+ },
295+ Plan : tfsdk.Plan {
296+ Raw : rawValRequired ,
297+ Schema : resourceSchema ,
298+ },
299+ },
300+ response : & resource.ModifyPlanResponse {
301+ Plan : tfsdk.Plan {
302+ Raw : rawValRequired ,
303+ Schema : resourceSchema ,
304+ },
305+ },
306+ when : Before ,
307+ },
308+ },
309+ {
310+ name : "update, remove required" ,
311+ opts : interceptorOptions [resource.ModifyPlanRequest , resource.ModifyPlanResponse ]{
312+ c : mockRequiredTagsClient {},
313+ request : & resource.ModifyPlanRequest {
314+ Config : tfsdk.Config {
315+ Raw : rawValPartial ,
316+ Schema : resourceSchema ,
317+ },
318+ State : tfsdk.State {
319+ Raw : rawValRequired ,
320+ Schema : resourceSchema ,
321+ },
322+ Plan : tfsdk.Plan {
323+ Raw : rawValPartial ,
324+ Schema : resourceSchema ,
325+ },
326+ },
327+ response : & resource.ModifyPlanResponse {
328+ Plan : tfsdk.Plan {
329+ Raw : rawValRequired ,
330+ Schema : resourceSchema ,
331+ },
332+ },
333+ when : Before ,
334+ },
335+ wantDiags : diag.Diagnostics {diag .NewAttributeErrorDiagnostic (
336+ path .Root (names .AttrTags ),
337+ "Missing Required Tags" ,
338+ "An organizational tag policy requires the following tags for aws_test: [foo]" ,
339+ ),
340+ },
341+ },
342+ {
343+ name : "update, unknown tag values" ,
344+ opts : interceptorOptions [resource.ModifyPlanRequest , resource.ModifyPlanResponse ]{
345+ c : mockRequiredTagsClient {},
346+ request : & resource.ModifyPlanRequest {
347+ Config : tfsdk.Config {
348+ Raw : rawValUnknown ,
349+ Schema : resourceSchema ,
350+ },
351+ State : tfsdk.State {
352+ Raw : rawValPartial ,
353+ Schema : resourceSchema ,
354+ },
355+ Plan : tfsdk.Plan {
356+ Raw : rawValUnknown ,
357+ Schema : resourceSchema ,
358+ },
359+ },
360+ response : & resource.ModifyPlanResponse {
361+ Plan : tfsdk.Plan {
362+ Raw : rawValUnknown ,
363+ Schema : resourceSchema ,
364+ },
365+ },
366+ when : Before ,
367+ },
368+ },
165369 {
166370 name : "destroy" ,
167371 opts : interceptorOptions [resource.ModifyPlanRequest , resource.ModifyPlanResponse ]{
0 commit comments