From 75ba18a200ce7f1ada75af5c18b492b0201233a2 Mon Sep 17 00:00:00 2001 From: Dimitrios Dedoussis Date: Tue, 7 May 2019 17:30:16 +0100 Subject: [PATCH] Improves error handling in aws xray sampling rule resource --- aws/resource_aws_xray_sampling_rule.go | 15 ++++++++++---- aws/resource_aws_xray_sampling_rule_test.go | 22 +++++---------------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/aws/resource_aws_xray_sampling_rule.go b/aws/resource_aws_xray_sampling_rule.go index 54a95bc640d..94f297ba472 100644 --- a/aws/resource_aws_xray_sampling_rule.go +++ b/aws/resource_aws_xray_sampling_rule.go @@ -119,7 +119,7 @@ func resourceAwsXraySamplingRuleCreate(d *schema.ResourceData, meta interface{}) out, err := conn.CreateSamplingRule(params) if err != nil { - return err + return fmt.Errorf("error creating XRay Sampling Rule: %s", err) } d.SetId(*out.SamplingRuleRecord.SamplingRule.RuleName) @@ -133,7 +133,13 @@ func resourceAwsXraySamplingRuleRead(d *schema.ResourceData, meta interface{}) e samplingRule, err := getXraySamplingRule(conn, d.Id()) if err != nil { - return err + return fmt.Errorf("error reading XRay Sampling Rule (%s): %s", d.Id(), err) + } + + if samplingRule == nil { + log.Printf("[WARN] XRay Sampling Rule (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil } d.Set("rule_name", samplingRule.RuleName) @@ -149,6 +155,7 @@ func resourceAwsXraySamplingRuleRead(d *schema.ResourceData, meta interface{}) e d.Set("version", samplingRule.Version) d.Set("attributes", aws.StringValueMap(samplingRule.Attributes)) d.Set("arn", samplingRule.RuleARN) + return nil } @@ -182,7 +189,7 @@ func resourceAwsXraySamplingRuleUpdate(d *schema.ResourceData, meta interface{}) _, err := conn.UpdateSamplingRule(params) if err != nil { - return err + return fmt.Errorf("error updating XRay Sampling Rule (%s): %s", d.Id(), err) } return resourceAwsXraySamplingRuleRead(d, meta) @@ -198,7 +205,7 @@ func resourceAwsXraySamplingRuleDelete(d *schema.ResourceData, meta interface{}) } _, err := conn.DeleteSamplingRule(params) if err != nil { - return fmt.Errorf("Error deleting XRay Sampling Rule: %s", d.Id()) + return fmt.Errorf("error deleting XRay Sampling Rule: %s", d.Id()) } return nil diff --git a/aws/resource_aws_xray_sampling_rule_test.go b/aws/resource_aws_xray_sampling_rule_test.go index 5545249c2a1..2dd363880b9 100644 --- a/aws/resource_aws_xray_sampling_rule_test.go +++ b/aws/resource_aws_xray_sampling_rule_test.go @@ -39,6 +39,11 @@ func TestAccAWSXraySamplingRule_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "attributes.%", "1"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -74,23 +79,6 @@ func TestAccAWSXraySamplingRule_update(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "attributes.%", "0"), ), }, - }, - }) -} - -func TestAccAWSXraySamplingRule_import(t *testing.T) { - resourceName := "aws_xray_sampling_rule.test" - rString := acctest.RandString(8) - ruleName := fmt.Sprintf("tf_acc_sampling_rule_%s", rString) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSXraySamplingRuleDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSXraySamplingRuleConfig_basic(ruleName), - }, { ResourceName: resourceName, ImportState: true,