Skip to content

Commit

Permalink
Improves error handling in aws xray sampling rule resource
Browse files Browse the repository at this point in the history
  • Loading branch information
dedoussis committed May 7, 2019
1 parent 6d49669 commit 75ba18a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
15 changes: 11 additions & 4 deletions aws/resource_aws_xray_sampling_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
22 changes: 5 additions & 17 deletions aws/resource_aws_xray_sampling_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func TestAccAWSXraySamplingRule_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "attributes.%", "1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 75ba18a

Please sign in to comment.