Skip to content

Commit 5c61579

Browse files
authored
Merge pull request #30242 from codablock/resourcegroups-no-conflict
r/aws_resourcegroups_group: Make resource_query and configuration not conflict
2 parents f9e9bbd + 2b83189 commit 5c61579

11 files changed

+317
-130
lines changed

.changelog/30242.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:enhancement
2+
resource/aws_resourcegroups_group: `resource_query` no longer conflicts with `configuration`
3+
```
4+
5+
```release-note:bug
6+
resource/aws_resourcegroups_resource: Fix crash when resource Create fails
7+
```

internal/service/resourcegroups/group.go

+22-19
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ func ResourceGroup() *schema.Resource {
4444
Computed: true,
4545
},
4646
"configuration": {
47-
Type: schema.TypeSet,
48-
Optional: true,
49-
ConflictsWith: []string{"resource_query"},
47+
Type: schema.TypeSet,
48+
Optional: true,
5049
Elem: &schema.Resource{
5150
Schema: map[string]*schema.Schema{
5251
"parameters": {
@@ -85,11 +84,10 @@ func ResourceGroup() *schema.Resource {
8584
ForceNew: true,
8685
},
8786
"resource_query": {
88-
Type: schema.TypeList,
89-
Optional: true,
90-
MinItems: 1,
91-
MaxItems: 1,
92-
ConflictsWith: []string{"configuration"},
87+
Type: schema.TypeList,
88+
Optional: true,
89+
MinItems: 1,
90+
MaxItems: 1,
9391
Elem: &schema.Resource{
9492
Schema: map[string]*schema.Schema{
9593
"query": {
@@ -178,32 +176,37 @@ func resourceGroupRead(ctx context.Context, d *schema.ResourceData, meta interfa
178176
GroupName: aws.String(d.Id()),
179177
})
180178

181-
isConfigurationGroup := false
179+
hasQuery := true
182180
if err != nil {
183181
if tfawserr.ErrCodeEquals(err, resourcegroups.ErrCodeBadRequestException) {
184182
// Attempting to get the query on a configuration group returns BadRequestException.
185-
isConfigurationGroup = true
183+
hasQuery = false
186184
} else {
187185
return diag.Errorf("reading Resource Groups Group (%s) resource query: %s", d.Id(), err)
188186
}
189187
}
190188

191-
if !isConfigurationGroup {
189+
groupCfg, err := findGroupConfigurationByGroupName(ctx, conn, d.Id())
190+
191+
hasConfiguration := true
192+
if err != nil {
193+
if tfawserr.ErrCodeEquals(err, resourcegroups.ErrCodeBadRequestException) {
194+
// Attempting to get configuration on a query group returns BadRequestException.
195+
hasConfiguration = false
196+
} else {
197+
return diag.Errorf("reading Resource Groups Group (%s) configuration: %s", d.Id(), err)
198+
}
199+
}
200+
201+
if hasQuery {
192202
resultQuery := map[string]interface{}{}
193203
resultQuery["query"] = aws.StringValue(q.GroupQuery.ResourceQuery.Query)
194204
resultQuery["type"] = aws.StringValue(q.GroupQuery.ResourceQuery.Type)
195205
if err := d.Set("resource_query", []map[string]interface{}{resultQuery}); err != nil {
196206
return diag.Errorf("setting resource_query: %s", err)
197207
}
198208
}
199-
200-
if isConfigurationGroup {
201-
groupCfg, err := findGroupConfigurationByGroupName(ctx, conn, d.Id())
202-
203-
if err != nil {
204-
return diag.Errorf("reading Resource Groups Group (%s) configuration: %s", d.Id(), err)
205-
}
206-
209+
if hasConfiguration {
207210
if err := d.Set("configuration", flattenResourceGroupConfigurationItems(groupCfg.Configuration)); err != nil {
208211
return diag.Errorf("setting configuration: %s", err)
209212
}

internal/service/resourcegroups/group_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,40 @@ func TestAccResourceGroupsGroup_configurationParametersOptional(t *testing.T) {
223223
})
224224
}
225225

226+
func TestAccResourceGroupsGroup_resourceQueryAndConfiguration(t *testing.T) {
227+
ctx := acctest.Context(t)
228+
var v resourcegroups.Group
229+
resourceName := "aws_resourcegroups_group.test"
230+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
231+
232+
configType := "AWS::NetworkFirewall::RuleGroup"
233+
234+
resource.ParallelTest(t, resource.TestCase{
235+
PreCheck: func() { acctest.PreCheck(ctx, t) },
236+
ErrorCheck: acctest.ErrorCheck(t, resourcegroups.EndpointsID),
237+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
238+
CheckDestroy: testAccCheckResourceGroupDestroy(ctx),
239+
Steps: []resource.TestStep{
240+
{
241+
Config: testAccGroupConfig_resourceQueryAndConfiguration(rName, testAccResourceGroupQueryConfig, configType),
242+
Check: resource.ComposeTestCheckFunc(
243+
testAccCheckResourceGroupExists(ctx, resourceName, &v),
244+
resource.TestCheckResourceAttr(resourceName, "name", rName),
245+
resource.TestCheckResourceAttrSet(resourceName, "arn"),
246+
resource.TestCheckResourceAttr(resourceName, "resource_query.0.query", testAccResourceGroupQueryConfig+"\n"),
247+
resource.TestCheckResourceAttr(resourceName, "configuration.#", "1"),
248+
resource.TestCheckResourceAttr(resourceName, "configuration.0.type", configType),
249+
),
250+
},
251+
{
252+
ResourceName: resourceName,
253+
ImportState: true,
254+
ImportStateVerify: true,
255+
},
256+
},
257+
})
258+
}
259+
226260
func testAccCheckResourceGroupExists(ctx context.Context, n string, v *resourcegroups.Group) resource.TestCheckFunc {
227261
return func(s *terraform.State) error {
228262
rs, ok := s.RootModule().Resources[n]
@@ -425,3 +459,22 @@ resource "aws_resourcegroups_group" "test" {
425459
}
426460
`, rName, configType1, configType2)
427461
}
462+
463+
func testAccGroupConfig_resourceQueryAndConfiguration(rName, query, configType string) string {
464+
return fmt.Sprintf(`
465+
resource "aws_resourcegroups_group" "test" {
466+
name = %[1]q
467+
468+
resource_query {
469+
query = <<JSON
470+
%[2]s
471+
JSON
472+
473+
}
474+
475+
configuration {
476+
type = %[3]q
477+
}
478+
}
479+
`, rName, query, configType)
480+
}

0 commit comments

Comments
 (0)