Skip to content

Commit

Permalink
Add a CheckDestroy to tests. Remove importing and refactor how tags a…
Browse files Browse the repository at this point in the history
…re handled.
  • Loading branch information
Joe Stump committed Oct 20, 2018
1 parent b5e7954 commit f557b45
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
28 changes: 24 additions & 4 deletions aws/resource_aws_resourcegroups_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ func resourceAwsResourceGroupsGroup() *schema.Resource {
Update: resourceAwsResourceGroupsGroupUpdate,
Delete: resourceAwsResourceGroupsGroupDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
// As of 10/20/2018 it's not possible to import a complete resource because
// a resource group's tags are not returned by GetGroup nor ListGroups. This
// leads to dirty plans after an import, which breaks import tests.
//
// For now we ForceNew on the tags attribute and disable importing.
//
// Importer: &schema.ResourceImporter{
// State: schema.ImportStatePassthrough,
// },

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -62,7 +68,11 @@ func resourceAwsResourceGroupsGroup() *schema.Resource {
Computed: true,
},

"tags": tagsSchema(),
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
},
}
}
Expand All @@ -76,13 +86,23 @@ func extractResourceGroupResourceQuery(resourceQueryList []interface{}) *resourc
}
}

func extractResourceGroupTags(m map[string]interface{}) map[string]*string {
result := make(map[string]*string)
for k, v := range m {
result[k] = aws.String(v.(string))
}

return result
}

func resourceAwsResourceGroupsGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).resourcegroupsconn

input := resourcegroups.CreateGroupInput{
Description: aws.String(d.Get("description").(string)),
Name: aws.String(d.Get("name").(string)),
ResourceQuery: extractResourceGroupResourceQuery(d.Get("resource_query").([]interface{})),
Tags: extractResourceGroupTags(d.Get("tags").(map[string]interface{})),
}

res, err := conn.CreateGroup(&input)
Expand Down
44 changes: 37 additions & 7 deletions aws/resource_aws_resourcegroups_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func TestAccAWSResourceGroup_basic(t *testing.T) {
`

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSResourceGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSResourceGroupConfig_basic(n, desc1, query1),
Expand All @@ -58,11 +59,12 @@ func TestAccAWSResourceGroup_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "arn"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
// See comment in resource. We can't cleanly import resources as of 10/20/2018.
// {
// ResourceName: resourceName,
// ImportState: true,
// ImportStateVerify: true,
// },
{
Config: testAccAWSResourceGroupConfig_basic(n, desc2, query2),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -99,6 +101,34 @@ func testAccCheckAWSResourceGroupExists(n string) resource.TestCheckFunc {
}
}

func testAccCheckAWSResourceGroupDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_waf_rule_group" {
continue
}

conn := testAccProvider.Meta().(*AWSClient).resourcegroupsconn
resp, err := conn.GetGroup(&resourcegroups.GetGroupInput{
GroupName: aws.String(rs.Primary.ID),
})

if err == nil {
if *resp.Group.Name == rs.Primary.ID {
return fmt.Errorf("Resource Group %s still exists", rs.Primary.ID)
}
}

if isAWSErr(err, resourcegroups.ErrCodeNotFoundException, "") {

return nil
}

return err
}

return nil
}

func testAccAWSResourceGroupConfig_basic(rName string, desc string, query string) string {
return fmt.Sprintf(`
resource "aws_resourcegroups_group" "test" {
Expand Down

0 comments on commit f557b45

Please sign in to comment.