Skip to content
New issue

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

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

Already on GitHub? # to your account

r/xray_group - new resource #13597

Merged
merged 4 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ func Provider() *schema.Provider {
"aws_pinpoint_event_stream": resourceAwsPinpointEventStream(),
"aws_pinpoint_gcm_channel": resourceAwsPinpointGCMChannel(),
"aws_pinpoint_sms_channel": resourceAwsPinpointSMSChannel(),
"aws_xray_group": resourceAwsXrayGroup(),
"aws_xray_sampling_rule": resourceAwsXraySamplingRule(),
"aws_workspaces_ip_group": resourceAwsWorkspacesIpGroup(),

Expand Down
136 changes: 136 additions & 0 deletions aws/resource_aws_xray_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/xray"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsXrayGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsXrayGroupCreate,
Read: resourceAwsXrayGroupRead,
Update: resourceAwsXrayGroupUpdate,
Delete: resourceAwsXrayGroupDelete,

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

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"filter_expression": {
Type: schema.TypeString,
Required: true,
},
"tags": tagsSchema(),
},
}
}

func resourceAwsXrayGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).xrayconn
input := &xray.CreateGroupInput{
GroupName: aws.String(d.Get("group_name").(string)),
FilterExpression: aws.String(d.Get("filter_expression").(string)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().XrayTags(),
}

out, err := conn.CreateGroup(input)
if err != nil {
return fmt.Errorf("error creating XRay Group: %w", err)
}

d.SetId(aws.StringValue(out.Group.GroupARN))

return resourceAwsXrayGroupRead(d, meta)
}

func resourceAwsXrayGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).xrayconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

input := &xray.GetGroupInput{
GroupARN: aws.String(d.Id()),
}

group, err := conn.GetGroup(input)

if err != nil {
if isAWSErr(err, xray.ErrCodeInvalidRequestException, "Group not found") {
log.Printf("[WARN] XRay Group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("error reading XRay Group (%s): %w", d.Id(), err)
}

arn := aws.StringValue(group.Group.GroupARN)
d.Set("arn", arn)
d.Set("group_name", group.Group.GroupName)
d.Set("filter_expression", group.Group.FilterExpression)

tags, err := keyvaluetags.XrayListTags(conn, arn)
if err != nil {
return fmt.Errorf("error listing tags for Xray Group (%q): %s", d.Id(), err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

return nil
}

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

if d.HasChange("filter_expression") {
input := &xray.UpdateGroupInput{
GroupARN: aws.String(d.Id()),
FilterExpression: aws.String(d.Get("filter_expression").(string)),
}

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

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.XrayUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags: %w", err)
}
}

return resourceAwsXrayGroupRead(d, meta)
}

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

log.Printf("[INFO] Deleting XRay Group: %s", d.Id())

params := &xray.DeleteGroupInput{
GroupARN: aws.String(d.Id()),
}
_, err := conn.DeleteGroup(params)
if err != nil {
return fmt.Errorf("error deleting XRay Group (%s): %w", d.Id(), err)
}

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

import (
"fmt"
"regexp"
"testing"

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

func TestAccAWSXrayGroup_basic(t *testing.T) {
var Group xray.Group
resourceName := "aws_xray_group.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSXrayGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSXrayGroupBasicConfig(rName, "responsetime > 5"),
Check: resource.ComposeTestCheckFunc(
testAccCheckXrayGroupExists(resourceName, &Group),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "xray", regexp.MustCompile(`group/.+`)),
resource.TestCheckResourceAttr(resourceName, "group_name", rName),
resource.TestCheckResourceAttr(resourceName, "filter_expression", "responsetime > 5"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSXrayGroupBasicConfig(rName, "responsetime > 10"),
Check: resource.ComposeTestCheckFunc(
testAccCheckXrayGroupExists(resourceName, &Group),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "xray", regexp.MustCompile(`group/.+`)),
resource.TestCheckResourceAttr(resourceName, "group_name", rName),
resource.TestCheckResourceAttr(resourceName, "filter_expression", "responsetime > 10"),
),
},
},
})
}

func TestAccAWSXrayGroup_tags(t *testing.T) {
var Group xray.Group
resourceName := "aws_xray_group.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSXrayGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSXrayGroupBasicConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckXrayGroupExists(resourceName, &Group),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSXrayGroupBasicConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckXrayGroupExists(resourceName, &Group),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSXrayGroupBasicConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckXrayGroupExists(resourceName, &Group),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2")),
},
},
})
}

func TestAccAWSXrayGroup_disappears(t *testing.T) {
var Group xray.Group
resourceName := "aws_xray_group.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSXrayGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSXrayGroupBasicConfig(rName, "responsetime > 5"),
Check: resource.ComposeTestCheckFunc(
testAccCheckXrayGroupExists(resourceName, &Group),
testAccCheckResourceDisappears(testAccProvider, resourceAwsXrayGroup(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckXrayGroupExists(n string, Group *xray.Group) 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 XRay Group ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).xrayconn

input := &xray.GetGroupInput{
GroupARN: aws.String(rs.Primary.ID),
}

group, err := conn.GetGroup(input)

if err != nil {
return err
}

*Group = *group.Group

return nil
}
}

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

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

input := &xray.GetGroupInput{
GroupARN: aws.String(rs.Primary.ID),
}

group, err := conn.GetGroup(input)

if isAWSErr(err, xray.ErrCodeInvalidRequestException, "Group not found") {
continue
}

if err != nil {
return err
}

if group != nil {
return fmt.Errorf("Expected XRay Group to be destroyed, %s found", rs.Primary.ID)
}
}

return nil
}

func testAccAWSXrayGroupBasicConfig(rName, expression string) string {
return fmt.Sprintf(`
resource "aws_xray_group" "test" {
group_name = %[1]q
filter_expression = %[2]q
}
`, rName, expression)
}

func testAccAWSXrayGroupBasicConfigTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_xray_group" "test" {
group_name = %[1]q
filter_expression = "responsetime > 5"

tags = {
%[2]q = %[3]q
}
}
`, rName, tagKey1, tagValue1)
}

func testAccAWSXrayGroupBasicConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_xray_group" "test" {
group_name = %[1]q
filter_expression = "responsetime > 5"

tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}
41 changes: 41 additions & 0 deletions website/docs/r/xray_group.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
subcategory: "XRay"
layout: "aws"
page_title: "AWS: aws_xray_group"
description: |-
Creates and manages an AWS XRay Group.
---

# Resource: aws_xray_group

Creates and manages an AWS XRay Group.

## Example Usage

```hcl
resource "aws_xray_group" "example" {
group_name = "example"
filter_expression = "responsetime > 5"
}
```

## Argument Reference

* `group_name` - (Required) The name of the group.
* `filter_expression` - (Required) The filter expression defining criteria by which to group traces. more info can be found in official [docs](https://docs.aws.amazon.com/xray/latest/devguide/xray-console-filters.html).
* `tags` - (Optional) Key-value mapping of resource tags

## Attributes Reference

In addition to the arguments above, the following attributes are exported:

* `id` - The ARN of the Group.
* `arn` - The ARN of the Group.

## Import

XRay Groups can be imported using the ARN, e.g.

```
$ terraform import aws_xray_group.example arn:aws:xray:us-west-2:1234567890:group/example-group/TNGX7SW5U6QY36T4ZMOUA3HVLBYCZTWDIOOXY3CJAXTHSS3YCWUA
```