Skip to content

Commit

Permalink
resource/aws_xray_sampling_rule: Add tags argument (#14831)
Browse files Browse the repository at this point in the history
Output from acceptance testing:

```
--- PASS: TestAccAWSXraySamplingRule_basic (13.93s)
--- PASS: TestAccAWSXraySamplingRule_disappears (19.04s)
--- PASS: TestAccAWSXraySamplingRule_update (22.61s)
--- PASS: TestAccAWSXraySamplingRule_tags (53.74s)
```
  • Loading branch information
DrFaust92 committed Aug 25, 2020
1 parent 11301c1 commit 2086fa9
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 37 deletions.
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/listtags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ var serviceNames = []string{
"wafv2",
"worklink",
"workspaces",
"xray",
}

type TemplateData struct {
Expand Down
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/servicetags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ var sliceServiceNames = []string{
"wafregional",
"wafv2",
"workspaces",
"xray",
}

var mapServiceNames = []string{
Expand Down
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/updatetags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ var serviceNames = []string{
"wafv2",
"worklink",
"workspaces",
"xray",
}

type TemplateData struct {
Expand Down
18 changes: 18 additions & 0 deletions aws/internal/keyvaluetags/list_tags_gen.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -732,6 +735,8 @@ func ServiceTagInputIdentifierField(serviceName string) string {
return "ResourceARN"
case "workspaces":
return "ResourceId"
case "xray":
return "ResourceARN"
default:
return "ResourceArn"
}
Expand Down
28 changes: 28 additions & 0 deletions aws/internal/keyvaluetags/service_tags_gen.go

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

37 changes: 37 additions & 0 deletions aws/internal/keyvaluetags/update_tags_gen.go

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

80 changes: 53 additions & 27 deletions aws/resource_aws_xray_sampling_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -89,6 +90,7 @@ func resourceAwsXraySamplingRule() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -154,42 +159,63 @@ 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
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.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)
}
}

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.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()),
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)
}
}
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): %s", 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)
Expand Down
Loading

0 comments on commit 2086fa9

Please sign in to comment.