From 895d4a7eed210e0ecd551d6b9cc28edb006caf2f Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 25 Aug 2020 16:17:37 +0300 Subject: [PATCH 01/11] add xray to tag service list --- aws/internal/keyvaluetags/generators/servicetags/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/internal/keyvaluetags/generators/servicetags/main.go b/aws/internal/keyvaluetags/generators/servicetags/main.go index 0fbcb7d89ad..85e35e44f4d 100644 --- a/aws/internal/keyvaluetags/generators/servicetags/main.go +++ b/aws/internal/keyvaluetags/generators/servicetags/main.go @@ -96,6 +96,7 @@ var sliceServiceNames = []string{ "wafregional", "wafv2", "workspaces", + "xray", } var mapServiceNames = []string{ From 653c61e00af6f71c3b3b92f5dbb17bb0ecdf81cd Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 25 Aug 2020 16:20:38 +0300 Subject: [PATCH 02/11] add xray tag list and update --- aws/internal/keyvaluetags/generators/listtags/main.go | 1 + aws/internal/keyvaluetags/generators/updatetags/main.go | 1 + 2 files changed, 2 insertions(+) diff --git a/aws/internal/keyvaluetags/generators/listtags/main.go b/aws/internal/keyvaluetags/generators/listtags/main.go index 9ce6dd2867b..bee18a8f225 100644 --- a/aws/internal/keyvaluetags/generators/listtags/main.go +++ b/aws/internal/keyvaluetags/generators/listtags/main.go @@ -114,6 +114,7 @@ var serviceNames = []string{ "wafv2", "worklink", "workspaces", + "xray", } type TemplateData struct { diff --git a/aws/internal/keyvaluetags/generators/updatetags/main.go b/aws/internal/keyvaluetags/generators/updatetags/main.go index b14f9c6ccd7..555e67ba04f 100644 --- a/aws/internal/keyvaluetags/generators/updatetags/main.go +++ b/aws/internal/keyvaluetags/generators/updatetags/main.go @@ -121,6 +121,7 @@ var serviceNames = []string{ "wafv2", "worklink", "workspaces", + "xray", } type TemplateData struct { From 2853e19547842f89d2e92cab17b29d0bb445e102 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 25 Aug 2020 16:28:43 +0300 Subject: [PATCH 03/11] add xray to generation --- aws/internal/keyvaluetags/service_generation_customizations.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aws/internal/keyvaluetags/service_generation_customizations.go b/aws/internal/keyvaluetags/service_generation_customizations.go index 76237cd6801..e75e8fbc415 100644 --- a/aws/internal/keyvaluetags/service_generation_customizations.go +++ b/aws/internal/keyvaluetags/service_generation_customizations.go @@ -112,6 +112,7 @@ import ( "github.com/aws/aws-sdk-go/service/wafv2" "github.com/aws/aws-sdk-go/service/worklink" "github.com/aws/aws-sdk-go/service/workspaces" + "github.com/aws/aws-sdk-go/service/xray" ) // ServiceClientType determines the service client Go type. @@ -333,6 +334,8 @@ func ServiceClientType(serviceName string) string { funcType = reflect.TypeOf(worklink.New) case "workspaces": funcType = reflect.TypeOf(workspaces.New) + case "xray": + funcType = reflect.TypeOf(xray.New) default: panic(fmt.Sprintf("unrecognized ServiceClientType: %s", serviceName)) } From 631d90a0ee509c3ea4d50431311ded9d91a4daf0 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 25 Aug 2020 16:37:57 +0300 Subject: [PATCH 04/11] use xray specific input field --- aws/internal/keyvaluetags/service_generation_customizations.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/aws/internal/keyvaluetags/service_generation_customizations.go b/aws/internal/keyvaluetags/service_generation_customizations.go index e75e8fbc415..86c45b1ece9 100644 --- a/aws/internal/keyvaluetags/service_generation_customizations.go +++ b/aws/internal/keyvaluetags/service_generation_customizations.go @@ -735,6 +735,8 @@ func ServiceTagInputIdentifierField(serviceName string) string { return "ResourceARN" case "workspaces": return "ResourceId" + case "xray": + return "ResourceARN" default: return "ResourceArn" } From c6d5802d7aba5a8732accb66cc7e251c1a3bda67 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 25 Aug 2020 16:43:14 +0300 Subject: [PATCH 05/11] add kv tags generated --- aws/internal/keyvaluetags/list_tags_gen.go | 18 +++++++++ aws/internal/keyvaluetags/service_tags_gen.go | 28 ++++++++++++++ aws/internal/keyvaluetags/update_tags_gen.go | 37 +++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/aws/internal/keyvaluetags/list_tags_gen.go b/aws/internal/keyvaluetags/list_tags_gen.go index bff556eaeac..1ed937c97c5 100644 --- a/aws/internal/keyvaluetags/list_tags_gen.go +++ b/aws/internal/keyvaluetags/list_tags_gen.go @@ -101,6 +101,7 @@ import ( "github.com/aws/aws-sdk-go/service/wafv2" "github.com/aws/aws-sdk-go/service/worklink" "github.com/aws/aws-sdk-go/service/workspaces" + "github.com/aws/aws-sdk-go/service/xray" ) // AccessanalyzerListTags lists accessanalyzer service tags. @@ -1758,3 +1759,20 @@ func WorkspacesListTags(conn *workspaces.WorkSpaces, identifier string) (KeyValu return WorkspacesKeyValueTags(output.TagList), nil } + +// XrayListTags lists xray service tags. +// The identifier is typically the Amazon Resource Name (ARN), although +// it may also be a different identifier depending on the service. +func XrayListTags(conn *xray.XRay, identifier string) (KeyValueTags, error) { + input := &xray.ListTagsForResourceInput{ + ResourceARN: aws.String(identifier), + } + + output, err := conn.ListTagsForResource(input) + + if err != nil { + return New(nil), err + } + + return XrayKeyValueTags(output.Tags), nil +} diff --git a/aws/internal/keyvaluetags/service_tags_gen.go b/aws/internal/keyvaluetags/service_tags_gen.go index 970b60c31ae..8ed15980412 100644 --- a/aws/internal/keyvaluetags/service_tags_gen.go +++ b/aws/internal/keyvaluetags/service_tags_gen.go @@ -80,6 +80,7 @@ import ( "github.com/aws/aws-sdk-go/service/waf" "github.com/aws/aws-sdk-go/service/wafv2" "github.com/aws/aws-sdk-go/service/workspaces" + "github.com/aws/aws-sdk-go/service/xray" ) // map[string]*string handling @@ -2563,3 +2564,30 @@ func WorkspacesKeyValueTags(tags []*workspaces.Tag) KeyValueTags { return New(m) } + +// XrayTags returns xray service tags. +func (tags KeyValueTags) XrayTags() []*xray.Tag { + result := make([]*xray.Tag, 0, len(tags)) + + for k, v := range tags.Map() { + tag := &xray.Tag{ + Key: aws.String(k), + Value: aws.String(v), + } + + result = append(result, tag) + } + + return result +} + +// XrayKeyValueTags creates KeyValueTags from xray service tags. +func XrayKeyValueTags(tags []*xray.Tag) KeyValueTags { + m := make(map[string]*string, len(tags)) + + for _, tag := range tags { + m[aws.StringValue(tag.Key)] = tag.Value + } + + return New(m) +} diff --git a/aws/internal/keyvaluetags/update_tags_gen.go b/aws/internal/keyvaluetags/update_tags_gen.go index 76af8214597..f2d5cd53b8f 100644 --- a/aws/internal/keyvaluetags/update_tags_gen.go +++ b/aws/internal/keyvaluetags/update_tags_gen.go @@ -110,6 +110,7 @@ import ( "github.com/aws/aws-sdk-go/service/wafv2" "github.com/aws/aws-sdk-go/service/worklink" "github.com/aws/aws-sdk-go/service/workspaces" + "github.com/aws/aws-sdk-go/service/xray" ) // AccessanalyzerUpdateTags updates accessanalyzer service tags. @@ -3860,3 +3861,39 @@ func WorkspacesUpdateTags(conn *workspaces.WorkSpaces, identifier string, oldTag return nil } + +// XrayUpdateTags updates xray service tags. +// The identifier is typically the Amazon Resource Name (ARN), although +// it may also be a different identifier depending on the service. +func XrayUpdateTags(conn *xray.XRay, identifier string, oldTagsMap interface{}, newTagsMap interface{}) error { + oldTags := New(oldTagsMap) + newTags := New(newTagsMap) + + if removedTags := oldTags.Removed(newTags); len(removedTags) > 0 { + input := &xray.UntagResourceInput{ + ResourceARN: aws.String(identifier), + TagKeys: aws.StringSlice(removedTags.IgnoreAws().Keys()), + } + + _, err := conn.UntagResource(input) + + if err != nil { + return fmt.Errorf("error untagging resource (%s): %w", identifier, err) + } + } + + if updatedTags := oldTags.Updated(newTags); len(updatedTags) > 0 { + input := &xray.TagResourceInput{ + ResourceARN: aws.String(identifier), + Tags: updatedTags.IgnoreAws().XrayTags(), + } + + _, err := conn.TagResource(input) + + if err != nil { + return fmt.Errorf("error tagging resource (%s): %w", identifier, err) + } + } + + return nil +} From 4812aa1783e66494dd5c2aefd56a80bd078a255f Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 25 Aug 2020 17:10:12 +0300 Subject: [PATCH 06/11] add tags to xray sampling rule --- aws/resource_aws_xray_sampling_rule.go | 32 +++++++++++++++++---- aws/resource_aws_xray_sampling_rule_test.go | 8 +++--- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/aws/resource_aws_xray_sampling_rule.go b/aws/resource_aws_xray_sampling_rule.go index 80cbc674069..88db2973827 100644 --- a/aws/resource_aws_xray_sampling_rule.go +++ b/aws/resource_aws_xray_sampling_rule.go @@ -8,6 +8,7 @@ import ( "github.com/aws/aws-sdk-go/service/xray" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) func resourceAwsXraySamplingRule() *schema.Resource { @@ -89,6 +90,7 @@ func resourceAwsXraySamplingRule() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "tags": tagsSchema(), }, } } @@ -115,25 +117,27 @@ func resourceAwsXraySamplingRuleCreate(d *schema.ResourceData, meta interface{}) params := &xray.CreateSamplingRuleInput{ SamplingRule: samplingRule, + Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().XrayTags(), } out, err := conn.CreateSamplingRule(params) if err != nil { - return fmt.Errorf("error creating XRay Sampling Rule: %s", err) + return fmt.Errorf("error creating XRay Sampling Rule: %w", err) } - d.SetId(*out.SamplingRuleRecord.SamplingRule.RuleName) + d.SetId(aws.StringValue(out.SamplingRuleRecord.SamplingRule.RuleName)) return resourceAwsXraySamplingRuleRead(d, meta) } func resourceAwsXraySamplingRuleRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).xrayconn + ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig samplingRule, err := getXraySamplingRule(conn, d.Id()) if err != nil { - return fmt.Errorf("error reading XRay Sampling Rule (%s): %s", d.Id(), err) + return fmt.Errorf("error reading XRay Sampling Rule (%s): %w", d.Id(), err) } if samplingRule == nil { @@ -142,6 +146,7 @@ func resourceAwsXraySamplingRuleRead(d *schema.ResourceData, meta interface{}) e return nil } + arn := aws.StringValue(samplingRule.RuleARN) d.Set("rule_name", samplingRule.RuleName) d.Set("resource_arn", samplingRule.ResourceARN) d.Set("priority", samplingRule.Priority) @@ -154,13 +159,30 @@ func resourceAwsXraySamplingRuleRead(d *schema.ResourceData, meta interface{}) e d.Set("url_path", samplingRule.URLPath) d.Set("version", samplingRule.Version) d.Set("attributes", aws.StringValueMap(samplingRule.Attributes)) - d.Set("arn", samplingRule.RuleARN) + d.Set("arn", arn) + + tags, err := keyvaluetags.XrayListTags(conn, arn) + if err != nil { + return fmt.Errorf("error listing tags for Xray Sampling 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 resourceAwsXraySamplingRuleUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).xrayconn + + 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) + } + } + samplingRuleUpdate := &xray.SamplingRuleUpdate{ RuleName: aws.String(d.Id()), Priority: aws.Int64(int64(d.Get("priority").(int))), @@ -189,7 +211,7 @@ func resourceAwsXraySamplingRuleUpdate(d *schema.ResourceData, meta interface{}) _, err := conn.UpdateSamplingRule(params) if err != nil { - return fmt.Errorf("error updating XRay Sampling Rule (%s): %s", d.Id(), err) + return fmt.Errorf("error updating XRay Sampling Rule (%s): %w", d.Id(), err) } return resourceAwsXraySamplingRuleRead(d, meta) diff --git a/aws/resource_aws_xray_sampling_rule_test.go b/aws/resource_aws_xray_sampling_rule_test.go index 41e2fa008c9..90327fe9f97 100644 --- a/aws/resource_aws_xray_sampling_rule_test.go +++ b/aws/resource_aws_xray_sampling_rule_test.go @@ -13,8 +13,7 @@ import ( func TestAccAWSXraySamplingRule_basic(t *testing.T) { var samplingRule xray.SamplingRule resourceName := "aws_xray_sampling_rule.test" - rString := acctest.RandString(8) - ruleName := fmt.Sprintf("tf_acc_sampling_rule_%s", rString) + rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSXray(t) }, @@ -22,10 +21,10 @@ func TestAccAWSXraySamplingRule_basic(t *testing.T) { CheckDestroy: testAccCheckAWSXraySamplingRuleDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSXraySamplingRuleConfig_basic(ruleName), + Config: testAccAWSXraySamplingRuleConfig_basic(rName), Check: resource.ComposeTestCheckFunc( testAccCheckXraySamplingRuleExists(resourceName, &samplingRule), - testAccCheckResourceAttrRegionalARN(resourceName, "arn", "xray", fmt.Sprintf("sampling-rule/%s", ruleName)), + testAccCheckResourceAttrRegionalARN(resourceName, "arn", "xray", fmt.Sprintf("sampling-rule/%s", rName)), resource.TestCheckResourceAttr(resourceName, "priority", "5"), resource.TestCheckResourceAttr(resourceName, "version", "1"), resource.TestCheckResourceAttr(resourceName, "reservoir_size", "10"), @@ -37,6 +36,7 @@ func TestAccAWSXraySamplingRule_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "service_name", "*"), resource.TestCheckResourceAttr(resourceName, "service_type", "*"), resource.TestCheckResourceAttr(resourceName, "attributes.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), ), }, { From 7dd74f38b4b70bab5a3562bd1bf896bcfef34672 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 25 Aug 2020 17:14:09 +0300 Subject: [PATCH 07/11] docs --- website/docs/r/xray_sampling_rule.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/r/xray_sampling_rule.html.markdown b/website/docs/r/xray_sampling_rule.html.markdown index 3c84b72d9f3..7353e97f706 100644 --- a/website/docs/r/xray_sampling_rule.html.markdown +++ b/website/docs/r/xray_sampling_rule.html.markdown @@ -46,6 +46,7 @@ resource "aws_xray_sampling_rule" "example" { * `url_path` - (Required) Matches the path from a request URL. * `version` - (Required) The version of the sampling rule format (`1` ) * `attributes` - (Optional) Matches attributes derived from the request. +* `tags` - (Optional) Key-value mapping of resource tags ## Attributes Reference From e21e7b7c9d52c1c9ea233dbad52910781418c580 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 25 Aug 2020 17:23:41 +0300 Subject: [PATCH 08/11] add tags test --- aws/resource_aws_xray_sampling_rule_test.go | 97 +++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/aws/resource_aws_xray_sampling_rule_test.go b/aws/resource_aws_xray_sampling_rule_test.go index 90327fe9f97..261a5798e44 100644 --- a/aws/resource_aws_xray_sampling_rule_test.go +++ b/aws/resource_aws_xray_sampling_rule_test.go @@ -106,6 +106,50 @@ func TestAccAWSXraySamplingRule_update(t *testing.T) { }) } +func TestAccAWSXraySamplingRule_tags(t *testing.T) { + var samplingRule xray.SamplingRule + resourceName := "aws_xray_sampling_rule.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSXray(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSXraySamplingRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSXraySamplingRuleConfigTags1(rName, "key1", "value1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckXraySamplingRuleExists(resourceName, &samplingRule), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSXraySamplingRuleConfigTags2(rName, "key1", "value1updated", "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckXraySamplingRuleExists(resourceName, &samplingRule), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + Config: testAccAWSXraySamplingRuleConfigTags1(rName, "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckXraySamplingRuleExists(resourceName, &samplingRule), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + }, + }) +} + func testAccCheckXraySamplingRuleExists(n string, samplingRule *xray.SamplingRule) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -207,3 +251,56 @@ resource "aws_xray_sampling_rule" "test" { } `, ruleName, priority, reservoirSize) } + +func testAccAWSXraySamplingRuleConfigTags1(ruleName, tagKey1, tagValue1 string) string { + return fmt.Sprintf(` +resource "aws_xray_sampling_rule" "test" { + rule_name = %[1]q + priority = 5 + reservoir_size = 10 + url_path = "*" + host = "*" + http_method = "GET" + service_type = "*" + service_name = "*" + fixed_rate = 0.3 + resource_arn = "*" + version = 1 + + attributes = { + Hello = "World" + } + + tags = { + %[2]q = %[3]q + } +} +`, ruleName, tagKey1, tagValue1) +} + +func testAccAWSXraySamplingRuleConfigTags2(ruleName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { + return fmt.Sprintf(` +resource "aws_xray_sampling_rule" "test" { + rule_name = %[1]q + priority = 5 + reservoir_size = 10 + url_path = "*" + host = "*" + http_method = "GET" + service_type = "*" + service_name = "*" + fixed_rate = 0.3 + resource_arn = "*" + version = 1 + + attributes = { + Hello = "World" + } + + tags = { + %[2]q = %[3]q + %[4]q = %[5]q + } +} +`, ruleName, tagKey1, tagValue1, tagKey2, tagValue2) +} From 603fd446b5c47cd01d866adfe736a36a23cb6a1e Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 25 Aug 2020 17:30:10 +0300 Subject: [PATCH 09/11] add disappears test --- aws/resource_aws_xray_sampling_rule_test.go | 33 +++++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/aws/resource_aws_xray_sampling_rule_test.go b/aws/resource_aws_xray_sampling_rule_test.go index 261a5798e44..7f079133503 100644 --- a/aws/resource_aws_xray_sampling_rule_test.go +++ b/aws/resource_aws_xray_sampling_rule_test.go @@ -51,8 +51,7 @@ func TestAccAWSXraySamplingRule_basic(t *testing.T) { func TestAccAWSXraySamplingRule_update(t *testing.T) { var samplingRule xray.SamplingRule resourceName := "aws_xray_sampling_rule.test" - rString := acctest.RandString(8) - ruleName := fmt.Sprintf("tf_acc_sampling_rule_%s", rString) + rName := acctest.RandomWithPrefix("tf-acc-test") updatedPriority := acctest.RandIntRange(0, 9999) updatedReservoirSize := acctest.RandIntRange(0, 2147483647) @@ -62,10 +61,10 @@ func TestAccAWSXraySamplingRule_update(t *testing.T) { CheckDestroy: testAccCheckAWSXraySamplingRuleDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSXraySamplingRuleConfig_update(ruleName, acctest.RandIntRange(0, 9999), acctest.RandIntRange(0, 2147483647)), + Config: testAccAWSXraySamplingRuleConfig_update(rName, acctest.RandIntRange(0, 9999), acctest.RandIntRange(0, 2147483647)), Check: resource.ComposeTestCheckFunc( testAccCheckXraySamplingRuleExists(resourceName, &samplingRule), - testAccCheckResourceAttrRegionalARN(resourceName, "arn", "xray", fmt.Sprintf("sampling-rule/%s", ruleName)), + testAccCheckResourceAttrRegionalARN(resourceName, "arn", "xray", fmt.Sprintf("sampling-rule/%s", rName)), resource.TestCheckResourceAttrSet(resourceName, "priority"), resource.TestCheckResourceAttrSet(resourceName, "reservoir_size"), resource.TestCheckResourceAttr(resourceName, "version", "1"), @@ -80,10 +79,10 @@ func TestAccAWSXraySamplingRule_update(t *testing.T) { ), }, { // Update attributes - Config: testAccAWSXraySamplingRuleConfig_update(ruleName, updatedPriority, updatedReservoirSize), + Config: testAccAWSXraySamplingRuleConfig_update(rName, updatedPriority, updatedReservoirSize), Check: resource.ComposeTestCheckFunc( testAccCheckXraySamplingRuleExists(resourceName, &samplingRule), - testAccCheckResourceAttrRegionalARN(resourceName, "arn", "xray", fmt.Sprintf("sampling-rule/%s", ruleName)), + testAccCheckResourceAttrRegionalARN(resourceName, "arn", "xray", fmt.Sprintf("sampling-rule/%s", rName)), resource.TestCheckResourceAttr(resourceName, "priority", fmt.Sprintf("%d", updatedPriority)), resource.TestCheckResourceAttr(resourceName, "reservoir_size", fmt.Sprintf("%d", updatedReservoirSize)), resource.TestCheckResourceAttr(resourceName, "version", "1"), @@ -150,6 +149,28 @@ func TestAccAWSXraySamplingRule_tags(t *testing.T) { }) } +func TestAccAWSXraySamplingRule_disappears(t *testing.T) { + var samplingRule xray.SamplingRule + resourceName := "aws_xray_sampling_rule.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSXray(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSXraySamplingRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSXraySamplingRuleConfig_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckXraySamplingRuleExists(resourceName, &samplingRule), + testAccCheckResourceDisappears(testAccProvider, resourceAwsXraySamplingRule(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccCheckXraySamplingRuleExists(n string, samplingRule *xray.SamplingRule) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] From ebe43ccd7cb574c0bf99c2c17ae7882260d9a785 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 25 Aug 2020 18:34:51 +0300 Subject: [PATCH 10/11] dont update tags when other arguments are updated and vice versa --- aws/resource_aws_xray_sampling_rule.go | 52 ++++++++++++++------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/aws/resource_aws_xray_sampling_rule.go b/aws/resource_aws_xray_sampling_rule.go index 88db2973827..76f446049be 100644 --- a/aws/resource_aws_xray_sampling_rule.go +++ b/aws/resource_aws_xray_sampling_rule.go @@ -183,35 +183,39 @@ func resourceAwsXraySamplingRuleUpdate(d *schema.ResourceData, meta interface{}) } } - samplingRuleUpdate := &xray.SamplingRuleUpdate{ - RuleName: aws.String(d.Id()), - Priority: aws.Int64(int64(d.Get("priority").(int))), - FixedRate: aws.Float64(d.Get("fixed_rate").(float64)), - ReservoirSize: aws.Int64(int64(d.Get("reservoir_size").(int))), - ServiceName: aws.String(d.Get("service_name").(string)), - ServiceType: aws.String(d.Get("service_type").(string)), - Host: aws.String(d.Get("host").(string)), - HTTPMethod: aws.String(d.Get("http_method").(string)), - URLPath: aws.String(d.Get("url_path").(string)), - } + if d.HasChanges("priority", "fixed_rate", "reservoir_size", "service_name", "service_type", + "host", "http_method", "url_path", "resource_arn") { + samplingRuleUpdate := &xray.SamplingRuleUpdate{ + RuleName: aws.String(d.Id()), + Priority: aws.Int64(int64(d.Get("priority").(int))), + FixedRate: aws.Float64(d.Get("fixed_rate").(float64)), + ReservoirSize: aws.Int64(int64(d.Get("reservoir_size").(int))), + ServiceName: aws.String(d.Get("service_name").(string)), + ServiceType: aws.String(d.Get("service_type").(string)), + Host: aws.String(d.Get("host").(string)), + HTTPMethod: aws.String(d.Get("http_method").(string)), + URLPath: aws.String(d.Get("url_path").(string)), + ResourceARN: aws.String(d.Get("resource_arn").(string)), + } - if d.HasChange("attributes") { - attributes := map[string]*string{} - if v, ok := d.GetOk("attributes"); ok { - if m, ok := v.(map[string]interface{}); ok { - attributes = stringMapToPointers(m) + if d.HasChange("attributes") { + attributes := map[string]*string{} + if v, ok := d.GetOk("attributes"); ok { + if m, ok := v.(map[string]interface{}); ok { + attributes = stringMapToPointers(m) + } } + samplingRuleUpdate.Attributes = attributes } - samplingRuleUpdate.Attributes = attributes - } - params := &xray.UpdateSamplingRuleInput{ - SamplingRuleUpdate: samplingRuleUpdate, - } + params := &xray.UpdateSamplingRuleInput{ + SamplingRuleUpdate: samplingRuleUpdate, + } - _, err := conn.UpdateSamplingRule(params) - if err != nil { - return fmt.Errorf("error updating XRay Sampling Rule (%s): %w", d.Id(), err) + _, err := conn.UpdateSamplingRule(params) + if err != nil { + return fmt.Errorf("error updating XRay Sampling Rule (%s): %w", d.Id(), err) + } } return resourceAwsXraySamplingRuleRead(d, meta) From e95e9f7893706244f9db92ce02a2aabecc617388 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Tue, 25 Aug 2020 19:26:36 +0300 Subject: [PATCH 11/11] Update aws/resource_aws_xray_sampling_rule.go Co-authored-by: Brian Flad --- aws/resource_aws_xray_sampling_rule.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_xray_sampling_rule.go b/aws/resource_aws_xray_sampling_rule.go index 76f446049be..772b777d8ee 100644 --- a/aws/resource_aws_xray_sampling_rule.go +++ b/aws/resource_aws_xray_sampling_rule.go @@ -183,7 +183,7 @@ func resourceAwsXraySamplingRuleUpdate(d *schema.ResourceData, meta interface{}) } } - if d.HasChanges("priority", "fixed_rate", "reservoir_size", "service_name", "service_type", + if d.HasChanges("attributes", "priority", "fixed_rate", "reservoir_size", "service_name", "service_type", "host", "http_method", "url_path", "resource_arn") { samplingRuleUpdate := &xray.SamplingRuleUpdate{ RuleName: aws.String(d.Id()),