Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

r/Xray_sampling_rule - add support for tags #14831

Merged
merged 11 commits into from
Aug 25, 2020
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.

32 changes: 27 additions & 5 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,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{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: For tags only updates -- we should skip the UpdateSamplingRule API call below. I really need to get this "HasChangesOtherThan" thing merged upstream 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if a should handle this, NP. coming right up.

Copy link
Collaborator Author

@DrFaust92 DrFaust92 Aug 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, funny enough it takes longer to update tags than other arguments

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may need to be split more neatly to see if things can partially updated but left as is

RuleName: aws.String(d.Id()),
Priority: aws.Int64(int64(d.Get("priority").(int))),
Expand Down Expand Up @@ -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)
Expand Down
Loading