8
8
"fmt"
9
9
"testing"
10
10
11
+ "github.com/YakDriver/regexache"
11
12
"github.com/aws/aws-sdk-go-v2/aws"
12
13
"github.com/aws/aws-sdk-go-v2/service/iot"
13
14
awstypes "github.com/aws/aws-sdk-go-v2/service/iot/types"
@@ -26,6 +27,7 @@ func TestAccIoTThingPrincipalAttachment_basic(t *testing.T) {
26
27
ctx := acctest .Context (t )
27
28
thingName := sdkacctest .RandomWithPrefix ("tf-acc" )
28
29
thingName2 := sdkacctest .RandomWithPrefix ("tf-acc2" )
30
+ resourceName := "aws_iot_thing_principal_attachment.att"
29
31
30
32
resource .ParallelTest (t , resource.TestCase {
31
33
PreCheck : func () { acctest .PreCheck (ctx , t ) },
@@ -38,8 +40,14 @@ func TestAccIoTThingPrincipalAttachment_basic(t *testing.T) {
38
40
Check : resource .ComposeTestCheckFunc (
39
41
testAccCheckThingPrincipalAttachmentExists (ctx , "aws_iot_thing_principal_attachment.att" ),
40
42
testAccCheckThingPrincipalAttachmentStatus (ctx , thingName , true , []string {"aws_iot_certificate.cert" }),
43
+ resource .TestCheckResourceAttr (resourceName , "thing_principal_type" , string (awstypes .ThingPrincipalTypeNonExclusiveThing )),
41
44
),
42
45
},
46
+ {
47
+ ResourceName : resourceName ,
48
+ ImportState : true ,
49
+ ImportStateVerify : true ,
50
+ },
43
51
{
44
52
Config : testAccThingPrincipalAttachmentConfig_update1 (thingName , thingName2 ),
45
53
Check : resource .ComposeTestCheckFunc (
@@ -76,6 +84,63 @@ func TestAccIoTThingPrincipalAttachment_basic(t *testing.T) {
76
84
},
77
85
})
78
86
}
87
+ func TestAccIoTThingPrincipalAttachment_thingPrincipalType (t * testing.T ) {
88
+ ctx := acctest .Context (t )
89
+ thingName := sdkacctest .RandomWithPrefix ("tf-acc" )
90
+ thingName2 := sdkacctest .RandomWithPrefix ("tf-acc2" )
91
+ resourceName := "aws_iot_thing_principal_attachment.att"
92
+ resourceThingName := "aws_iot_thing.thing"
93
+ resourceCertName := "aws_iot_certificate.cert"
94
+
95
+ resource .ParallelTest (t , resource.TestCase {
96
+ PreCheck : func () { acctest .PreCheck (ctx , t ) },
97
+ ErrorCheck : acctest .ErrorCheck (t , names .IoTServiceID ),
98
+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories ,
99
+ CheckDestroy : testAccCheckThingPrincipalAttachmentDestroy (ctx ),
100
+ Steps : []resource.TestStep {
101
+ {
102
+ Config : testAccThingPrincipalAttachmentConfig_thingPrincipalType (thingName ),
103
+ Check : resource .ComposeTestCheckFunc (
104
+ testAccCheckThingPrincipalAttachmentExists (ctx , "aws_iot_thing_principal_attachment.att" ),
105
+ testAccCheckThingPrincipalAttachmentStatus (ctx , thingName , true , []string {"aws_iot_certificate.cert" }),
106
+ resource .TestCheckResourceAttr (resourceName , "thing_principal_type" , string (awstypes .ThingPrincipalTypeExclusiveThing )),
107
+ resource .TestCheckResourceAttrPair (resourceName , "thing" , resourceThingName , names .AttrName ),
108
+ resource .TestCheckResourceAttrPair (resourceName , names .AttrPrincipal , resourceCertName , names .AttrARN ),
109
+ ),
110
+ },
111
+ {
112
+ ResourceName : resourceName ,
113
+ ImportState : true ,
114
+ ImportStateVerify : true ,
115
+ },
116
+ {
117
+ // The first attachment is specified as EXCLUSIVE_THING.
118
+ // Try to attach the same principal to another Thing, which should fail
119
+ // because exclusive principals can only be attached to one thing.
120
+ Config : testAccThingPrincipalAttachmentConfig_thingPrincipalTypeUpdate1 (thingName , thingName2 ),
121
+ ExpectError : regexache .MustCompile (`InvalidRequestException: Principal already has an exclusive Thing attached to it` ),
122
+ },
123
+ {
124
+ // Reset to one attachment with NON_EXCLUSIVE_THING.
125
+ Config : testAccThingPrincipalAttachmentConfig_thingPrincipalTypeUpdate2 (thingName ),
126
+ Check : resource .ComposeTestCheckFunc (
127
+ testAccCheckThingPrincipalAttachmentExists (ctx , "aws_iot_thing_principal_attachment.att" ),
128
+ testAccCheckThingPrincipalAttachmentStatus (ctx , thingName , true , []string {"aws_iot_certificate.cert" }),
129
+ resource .TestCheckResourceAttr (resourceName , "thing_principal_type" , string (awstypes .ThingPrincipalTypeNonExclusiveThing )),
130
+ resource .TestCheckResourceAttrPair (resourceName , "thing" , resourceThingName , names .AttrName ),
131
+ resource .TestCheckResourceAttrPair (resourceName , names .AttrPrincipal , resourceCertName , names .AttrARN ),
132
+ ),
133
+ },
134
+ {
135
+ // Try to attach the same principal to another Thing specifying EXCLUSIVE_THING,
136
+ // which should fail because the principal already has a non-exclusive attachment
137
+ // and exclusive attachments cannot coexist with any other attachments.
138
+ Config : testAccThingPrincipalAttachmentConfig_thingPrincipalTypeUpdate3 (thingName , thingName2 ),
139
+ ExpectError : regexache .MustCompile (`InvalidRequestException: Principal already has a Thing attached to it` ),
140
+ },
141
+ },
142
+ })
143
+ }
79
144
80
145
func testAccCheckThingPrincipalAttachmentDestroy (ctx context.Context ) resource.TestCheckFunc {
81
146
return func (s * terraform.State ) error {
@@ -282,3 +347,101 @@ resource "aws_iot_thing_principal_attachment" "att2" {
282
347
}
283
348
` , thingName )
284
349
}
350
+
351
+ func testAccThingPrincipalAttachmentConfig_thingPrincipalType (thingName string ) string {
352
+ return fmt .Sprintf (`
353
+ resource "aws_iot_certificate" "cert" {
354
+ csr = file("test-fixtures/iot-csr.pem")
355
+ active = true
356
+ }
357
+
358
+ resource "aws_iot_thing" "thing" {
359
+ name = "%s"
360
+ }
361
+
362
+ resource "aws_iot_thing_principal_attachment" "att" {
363
+ thing = aws_iot_thing.thing.name
364
+ principal = aws_iot_certificate.cert.arn
365
+
366
+ thing_principal_type = "EXCLUSIVE_THING"
367
+ }
368
+ ` , thingName )
369
+ }
370
+
371
+ func testAccThingPrincipalAttachmentConfig_thingPrincipalTypeUpdate1 (thingName , thingName2 string ) string {
372
+ return fmt .Sprintf (`
373
+ resource "aws_iot_certificate" "cert" {
374
+ csr = file("test-fixtures/iot-csr.pem")
375
+ active = true
376
+ }
377
+
378
+ resource "aws_iot_thing" "thing" {
379
+ name = %[1]q
380
+ }
381
+
382
+ resource "aws_iot_thing" "thing2" {
383
+ name = %[2]q
384
+ }
385
+
386
+ resource "aws_iot_thing_principal_attachment" "att" {
387
+ thing = aws_iot_thing.thing.name
388
+ principal = aws_iot_certificate.cert.arn
389
+
390
+ thing_principal_type = "EXCLUSIVE_THING"
391
+ }
392
+
393
+ resource "aws_iot_thing_principal_attachment" "att2" {
394
+ thing = aws_iot_thing.thing2.name
395
+ principal = aws_iot_certificate.cert.arn
396
+ }
397
+ ` , thingName , thingName2 )
398
+ }
399
+
400
+ func testAccThingPrincipalAttachmentConfig_thingPrincipalTypeUpdate2 (thingName string ) string {
401
+ return fmt .Sprintf (`
402
+ resource "aws_iot_certificate" "cert" {
403
+ csr = file("test-fixtures/iot-csr.pem")
404
+ active = true
405
+ }
406
+
407
+ resource "aws_iot_thing" "thing" {
408
+ name = "%s"
409
+ }
410
+
411
+ resource "aws_iot_thing_principal_attachment" "att" {
412
+ thing = aws_iot_thing.thing.name
413
+ principal = aws_iot_certificate.cert.arn
414
+
415
+ thing_principal_type = "NON_EXCLUSIVE_THING"
416
+ }
417
+ ` , thingName )
418
+ }
419
+
420
+ func testAccThingPrincipalAttachmentConfig_thingPrincipalTypeUpdate3 (thingName , thingName2 string ) string {
421
+ return fmt .Sprintf (`
422
+ resource "aws_iot_certificate" "cert" {
423
+ csr = file("test-fixtures/iot-csr.pem")
424
+ active = true
425
+ }
426
+
427
+ resource "aws_iot_thing" "thing" {
428
+ name = %[1]q
429
+ }
430
+
431
+ resource "aws_iot_thing" "thing2" {
432
+ name = %[2]q
433
+ }
434
+
435
+ resource "aws_iot_thing_principal_attachment" "att" {
436
+ thing = aws_iot_thing.thing.name
437
+ principal = aws_iot_certificate.cert.arn
438
+ }
439
+
440
+ resource "aws_iot_thing_principal_attachment" "att2" {
441
+ thing = aws_iot_thing.thing2.name
442
+ principal = aws_iot_certificate.cert.arn
443
+
444
+ thing_principal_type = "EXCLUSIVE_THING"
445
+ }
446
+ ` , thingName , thingName2 )
447
+ }
0 commit comments