Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an aws_resource_group resource for managing AWS resource groups #6217

Merged
merged 17 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import (
"github.com/aws/aws-sdk-go/service/pricing"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/redshift"
"github.com/aws/aws-sdk-go/service/resourcegroups"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/secretsmanager"
Expand Down Expand Up @@ -195,6 +196,7 @@ type AWSClient struct {
snsconn *sns.SNS
stsconn *sts.STS
redshiftconn *redshift.Redshift
resourcegroupsconn *resourcegroups.ResourceGroups
r53conn *route53.Route53
partition string
accountid string
Expand Down Expand Up @@ -545,6 +547,7 @@ func (c *Config) Client() (interface{}, error) {
client.r53conn = route53.New(r53Sess)
client.rdsconn = rds.New(awsRdsSess)
client.redshiftconn = redshift.New(sess)
client.resourcegroupsconn = resourcegroups.New(sess)
client.simpledbconn = simpledb.New(sess)
client.s3conn = s3.New(awsS3Sess)
client.scconn = servicecatalog.New(sess)
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ func Provider() terraform.ResourceProvider {
"aws_redshift_subnet_group": resourceAwsRedshiftSubnetGroup(),
"aws_redshift_snapshot_copy_grant": resourceAwsRedshiftSnapshotCopyGrant(),
"aws_redshift_event_subscription": resourceAwsRedshiftEventSubscription(),
"aws_resourcegroups_group": resourceAwsResourceGroupsGroup(),
"aws_route53_delegation_set": resourceAwsRoute53DelegationSet(),
"aws_route53_query_log": resourceAwsRoute53QueryLog(),
"aws_route53_record": resourceAwsRoute53Record(),
Expand Down
178 changes: 178 additions & 0 deletions aws/resource_aws_resourcegroups_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/resourcegroups"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsResourceGroupsGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsResourceGroupsGroupCreate,
Read: resourceAwsResourceGroupsGroupRead,
Update: resourceAwsResourceGroupsGroupUpdate,
Delete: resourceAwsResourceGroupsGroupDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"description": {
Type: schema.TypeString,
Optional: true,
},

"resource_query": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"query": {
Type: schema.TypeString,
Required: true,
},

"type": {
Type: schema.TypeString,
Optional: true,
Default: resourcegroups.QueryTypeTagFilters10,
ValidateFunc: validation.StringInSlice([]string{
resourcegroups.QueryTypeTagFilters10,
}, false),
},
},
},
},

"arn": {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchema(),
},
}
}

func extractResourceGroupResourceQuery(resourceQueryList []interface{}) *resourcegroups.ResourceQuery {
resourceQuery := resourceQueryList[0].(map[string]interface{})

return &resourcegroups.ResourceQuery{
Query: aws.String(resourceQuery["query"].(string)),
Type: aws.String(resourceQuery["type"].(string)),
}
}

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{})),
}

res, err := conn.CreateGroup(&input)
if err != nil {
return fmt.Errorf("error creating resource group: %s", err)
}

d.SetId(aws.StringValue(res.Group.Name))

return resourceAwsResourceGroupsGroupRead(d, meta)
}

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

g, err := conn.GetGroup(&resourcegroups.GetGroupInput{
GroupName: aws.String(d.Id()),
})

if err != nil {
if isAWSErr(err, resourcegroups.ErrCodeNotFoundException, "") {
log.Printf("[WARN] Resource Groups Group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

return fmt.Errorf("error reading resource group (%s): %s", d.Id(), err)
}

d.Set("name", aws.StringValue(g.Group.Name))
d.Set("description", aws.StringValue(g.Group.Description))
d.Set("arn", aws.StringValue(g.Group.GroupArn))

q, err := conn.GetGroupQuery(&resourcegroups.GetGroupQueryInput{
GroupName: aws.String(d.Id()),
})

if err != nil {
return fmt.Errorf("error reading resource query for resource group (%s): %s", d.Id(), err)
}

resultQuery := map[string]interface{}{}
resultQuery["query"] = aws.StringValue(q.GroupQuery.ResourceQuery.Query)
resultQuery["type"] = aws.StringValue(q.GroupQuery.ResourceQuery.Type)
d.Set("resource_query", []map[string]interface{}{resultQuery})

return nil
}

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

if d.HasChange("description") {
input := resourcegroups.UpdateGroupInput{
GroupName: aws.String(d.Id()),
Description: aws.String(d.Get("description").(string)),
}

_, err := conn.UpdateGroup(&input)
if err != nil {
return fmt.Errorf("error updating resource group (%s): %s", d.Id(), err)
}
}

if d.HasChange("resource_query") {
input := resourcegroups.UpdateGroupQueryInput{
GroupName: aws.String(d.Id()),
ResourceQuery: extractResourceGroupResourceQuery(d.Get("resource_query").([]interface{})),
}

_, err := conn.UpdateGroupQuery(&input)
if err != nil {
return fmt.Errorf("error updating resource query for resource group (%s): %s", d.Id(), err)
}
}

return resourceAwsResourceGroupsGroupRead(d, meta)
}

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

input := resourcegroups.DeleteGroupInput{
GroupName: aws.String(d.Id()),
}

_, err := conn.DeleteGroup(&input)
if err != nil {
return fmt.Errorf("error deleting resource group (%s): %s", d.Id(), err)
}

return nil
}
115 changes: 115 additions & 0 deletions aws/resource_aws_resourcegroups_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/resourcegroups"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSResourceGroup_basic(t *testing.T) {
resourceName := "aws_resourcegroups_group.test"
n := fmt.Sprintf("test-group-%d", acctest.RandInt())

desc1 := "Hello World"
desc2 := "Foo Bar"

query1 := `{
"ResourceTypeFilters": [
"AWS::EC2::Instance"
],
"TagFilters": [
{
"Key": "Stage",
"Values": ["Test"]
}
]
}
`

query2 := `{
"ResourceTypeFilters": [
"AWS::EC2::Instance"
],
"TagFilters": [
{
"Key": "Hello",
"Values": ["World"]
}
]
}
`

resource.ParallelTest(t, resource.TestCase{
bflad marked this conversation as resolved.
Show resolved Hide resolved
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAWSResourceGroupConfig_basic(n, desc1, query1),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSResourceGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", n),
resource.TestCheckResourceAttr(resourceName, "description", desc1),
resource.TestCheckResourceAttr(resourceName, "resource_query.0.query", query1+"\n"),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSResourceGroupConfig_basic(n, desc2, query2),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "description", desc2),
resource.TestCheckResourceAttr(resourceName, "resource_query.0.query", query2+"\n"),
),
},
},
})
}

func testAccCheckAWSResourceGroupExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No resource group name is set")
}

conn := testAccProvider.Meta().(*AWSClient).resourcegroupsconn

_, err := conn.GetGroup(&resourcegroups.GetGroupInput{
GroupName: aws.String(rs.Primary.ID),
})

if err != nil {
return err
}

return nil
}
}

func testAccAWSResourceGroupConfig_basic(rName string, desc string, query string) string {
return fmt.Sprintf(`
resource "aws_resourcegroups_group" "test" {
name = "%s"
description = "%s"

resource_query {
query = <<JSON
%s
JSON
}
}
`, rName, desc, query)
}
2 changes: 1 addition & 1 deletion vendor/github.com/aws/aws-sdk-go/aws/version.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading