@@ -10,6 +10,7 @@ import (
10
10
11
11
types "github.com/aws/aws-sdk-go-v2/service/controltower/types"
12
12
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
13
+ "github.com/hashicorp/terraform-plugin-testing/plancheck"
13
14
"github.com/hashicorp/terraform-plugin-testing/terraform"
14
15
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
15
16
"github.com/hashicorp/terraform-provider-aws/internal/conns"
@@ -18,42 +19,29 @@ import (
18
19
"github.com/hashicorp/terraform-provider-aws/names"
19
20
)
20
21
21
- func TestAccControlTowerControl_serial (t * testing.T ) {
22
- t .Parallel ()
23
-
24
- testCases := map [string ]map [string ]func (t * testing.T ){
25
- "Control" : {
26
- acctest .CtBasic : testAccControl_basic ,
27
- acctest .CtDisappears : testAccControl_disappears ,
28
- },
29
- }
30
-
31
- acctest .RunSerialTests2Levels (t , testCases , 0 )
32
- }
33
-
34
22
func testAccControl_basic (t * testing.T ) {
35
23
ctx := acctest .Context (t )
36
24
var control types.EnabledControlSummary
37
25
resourceName := "aws_controltower_control.test"
38
- controlName := "AWS-GR_EC2_VOLUME_INUSE_CHECK"
39
- ouName := "Security"
40
- region := "us-west-2" //lintignore:AWSAT003
26
+ ouDataSourceName := "data.aws_organizations_organizational_unit.test"
27
+ ouName := acctest .SkipIfEnvVarNotSet (t , "TF_AWS_CONTROLTOWER_CONTROL_OU_NAME" )
41
28
42
29
resource .Test (t , resource.TestCase {
43
30
PreCheck : func () {
44
31
acctest .PreCheck (ctx , t )
45
32
acctest .PreCheckOrganizationManagementAccount (ctx , t )
46
- testAccPreCheck (ctx , t )
47
33
},
48
34
ErrorCheck : acctest .ErrorCheck (t , names .ControlTowerServiceID ),
49
35
CheckDestroy : testAccCheckControlDestroy (ctx ),
50
36
ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories ,
51
37
Steps : []resource.TestStep {
52
38
{
53
- Config : testAccControlConfig_basic (controlName , ouName , region ),
39
+ Config : testAccControlConfig_basic (ouName ),
54
40
Check : resource .ComposeTestCheckFunc (
55
41
testAccCheckControlExists (ctx , resourceName , & control ),
56
42
resource .TestCheckResourceAttrSet (resourceName , "control_identifier" ),
43
+ resource .TestCheckResourceAttrPair (resourceName , "target_identifier" , ouDataSourceName , names .AttrARN ),
44
+ resource .TestCheckResourceAttr (resourceName , "parameters.#" , "0" ),
57
45
),
58
46
},
59
47
},
@@ -64,27 +52,84 @@ func testAccControl_disappears(t *testing.T) {
64
52
ctx := acctest .Context (t )
65
53
var control types.EnabledControlSummary
66
54
resourceName := "aws_controltower_control.test"
67
- controlName := "AWS-GR_EC2_VOLUME_INUSE_CHECK"
68
- ouName := "Security"
69
- region := "us-west-2" //lintignore:AWSAT003
55
+ ouName := acctest .SkipIfEnvVarNotSet (t , "TF_AWS_CONTROLTOWER_CONTROL_OU_NAME" )
70
56
71
57
resource .Test (t , resource.TestCase {
72
58
PreCheck : func () {
73
59
acctest .PreCheck (ctx , t )
74
60
acctest .PreCheckOrganizationManagementAccount (ctx , t )
75
- testAccPreCheck (ctx , t )
76
61
},
77
62
ErrorCheck : acctest .ErrorCheck (t , names .ControlTowerServiceID ),
78
63
ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories ,
79
64
CheckDestroy : testAccCheckControlDestroy (ctx ),
80
65
Steps : []resource.TestStep {
81
66
{
82
- Config : testAccControlConfig_basic (controlName , ouName , region ),
67
+ Config : testAccControlConfig_basic (ouName ),
83
68
Check : resource .ComposeTestCheckFunc (
84
69
testAccCheckControlExists (ctx , resourceName , & control ),
85
70
acctest .CheckResourceDisappears (ctx , acctest .Provider , tfcontroltower .ResourceControl (), resourceName ),
86
71
),
87
72
ExpectNonEmptyPlan : true ,
73
+ ConfigPlanChecks : resource.ConfigPlanChecks {
74
+ PostApplyPostRefresh : []plancheck.PlanCheck {
75
+ plancheck .ExpectResourceAction (resourceName , plancheck .ResourceActionCreate ),
76
+ },
77
+ },
78
+ },
79
+ },
80
+ })
81
+ }
82
+
83
+ func testAccControl_parameters (t * testing.T ) {
84
+ ctx := acctest .Context (t )
85
+ var control types.EnabledControlSummary
86
+ resourceName := "aws_controltower_control.test"
87
+ ouName := acctest .SkipIfEnvVarNotSet (t , "TF_AWS_CONTROLTOWER_CONTROL_OU_NAME" )
88
+
89
+ resource .Test (t , resource.TestCase {
90
+ PreCheck : func () {
91
+ acctest .PreCheck (ctx , t )
92
+ acctest .PreCheckOrganizationManagementAccount (ctx , t )
93
+ },
94
+ ErrorCheck : acctest .ErrorCheck (t , names .ControlTowerServiceID ),
95
+ CheckDestroy : testAccCheckControlDestroy (ctx ),
96
+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories ,
97
+ Steps : []resource.TestStep {
98
+ {
99
+ Config : testAccControlConfig_parameters (ouName ),
100
+ Check : resource .ComposeTestCheckFunc (
101
+ testAccCheckControlExists (ctx , resourceName , & control ),
102
+ resource .TestCheckResourceAttrSet (resourceName , "control_identifier" ),
103
+ resource .TestCheckResourceAttr (resourceName , "parameters.#" , "1" ),
104
+ resource .TestCheckResourceAttr (resourceName , "parameters.0.key" , "ExemptedPrincipalArns" ),
105
+ ),
106
+ },
107
+ {
108
+ Config : testAccControlConfig_basic (ouName ),
109
+ Check : resource .ComposeTestCheckFunc (
110
+ testAccCheckControlExists (ctx , resourceName , & control ),
111
+ resource .TestCheckResourceAttrSet (resourceName , "control_identifier" ),
112
+ resource .TestCheckResourceAttr (resourceName , "parameters.#" , "0" ),
113
+ ),
114
+ ConfigPlanChecks : resource.ConfigPlanChecks {
115
+ PreApply : []plancheck.PlanCheck {
116
+ plancheck .ExpectResourceAction (resourceName , plancheck .ResourceActionUpdate ),
117
+ },
118
+ },
119
+ },
120
+ {
121
+ Config : testAccControlConfig_parameters (ouName ),
122
+ Check : resource .ComposeTestCheckFunc (
123
+ testAccCheckControlExists (ctx , resourceName , & control ),
124
+ resource .TestCheckResourceAttrSet (resourceName , "control_identifier" ),
125
+ resource .TestCheckResourceAttr (resourceName , "parameters.#" , "1" ),
126
+ resource .TestCheckResourceAttr (resourceName , "parameters.0.key" , "ExemptedPrincipalArns" ),
127
+ ),
128
+ ConfigPlanChecks : resource.ConfigPlanChecks {
129
+ PreApply : []plancheck.PlanCheck {
130
+ plancheck .ExpectResourceAction (resourceName , plancheck .ResourceActionUpdate ),
131
+ },
132
+ },
88
133
},
89
134
},
90
135
})
@@ -137,29 +182,49 @@ func testAccCheckControlDestroy(ctx context.Context) resource.TestCheckFunc {
137
182
}
138
183
}
139
184
140
- func testAccControlConfig_basic ( controlName , ouName , region string ) string {
185
+ func testAccControlConfigBase ( ouName string ) string {
141
186
return fmt .Sprintf (`
142
- data "aws_region" "current" {}
143
-
144
187
data "aws_partition" "current" {}
188
+ data "aws_region" "current" {}
189
+ data "aws_caller_identity" "current" {}
145
190
146
191
data "aws_organizations_organization" "test" {}
147
192
148
- data "aws_organizations_organizational_units " "test" {
193
+ data "aws_organizations_organizational_unit " "test" {
149
194
parent_id = data.aws_organizations_organization.test.roots[0].id
195
+ name = %[1]q
196
+ }
197
+ ` , ouName )
198
+ }
199
+
200
+ func testAccControlConfig_basic (ouName string ) string {
201
+ return acctest .ConfigCompose (
202
+ testAccControlConfigBase (ouName ),
203
+ `
204
+ resource "aws_controltower_control" "test" {
205
+ control_identifier = "arn:${data.aws_partition.current.partition}:controltower:${data.aws_region.current.name}::control/AWS-GR_DISALLOW_CROSS_REGION_NETWORKING"
206
+ target_identifier = data.aws_organizations_organizational_unit.test.arn
207
+ }
208
+ ` )
150
209
}
151
210
211
+ // See the AWS documentation for a list of parameterized controls.
212
+ //
213
+ // Ref:
214
+ // - https://docs.aws.amazon.com/controltower/latest/controlreference/control-parameter-concepts.html
215
+ // - https://docs.aws.amazon.com/controltower/latest/controlreference/elective-preventive-controls.html
216
+ func testAccControlConfig_parameters (ouName string ) string {
217
+ return acctest .ConfigCompose (
218
+ testAccControlConfigBase (ouName ),
219
+ `
152
220
resource "aws_controltower_control" "test" {
153
- control_identifier = "arn:${data.aws_partition.current.partition}:controltower:${data.aws_region.current.name}::control/%[1]s"
154
- target_identifier = [
155
- for x in data.aws_organizations_organizational_units.test.children :
156
- x.arn if x.name == "%[2]s"
157
- ][0]
221
+ control_identifier = "arn:${data.aws_partition.current.partition}:controltower:${data.aws_region.current.name}::control/AWS-GR_DISALLOW_CROSS_REGION_NETWORKING"
222
+ target_identifier = data.aws_organizations_organizational_unit.test.arn
158
223
159
224
parameters {
160
- key = "AllowedRegions "
161
- value = jsonencode([%[3]q ])
225
+ key = "ExemptedPrincipalArns "
226
+ value = jsonencode(["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role/tf-acctest-example" ])
162
227
}
163
228
}
164
- ` , controlName , ouName , region )
229
+ ` )
165
230
}
0 commit comments