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

feat: Add resource filtering support to aws_oam_link #38277

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/38277.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_oam_link: Add `link_configuration` argument
```

```release-note:enhancement
data-source/aws_oam_link: Add `link_configuration` attribute
```
145 changes: 140 additions & 5 deletions internal/service/oam/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
Expand Down Expand Up @@ -58,6 +59,43 @@ func ResourceLink() *schema.Resource {
Required: true,
ForceNew: true,
},
"link_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"log_group_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
names.AttrFilter: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 2000),
},
},
},
},
"metric_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
names.AttrFilter: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 2000),
},
},
},
},
},
},
},
"link_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -98,10 +136,11 @@ func resourceLinkCreate(ctx context.Context, d *schema.ResourceData, meta interf
conn := meta.(*conns.AWSClient).ObservabilityAccessManagerClient(ctx)

in := &oam.CreateLinkInput{
LabelTemplate: aws.String(d.Get("label_template").(string)),
ResourceTypes: flex.ExpandStringyValueSet[types.ResourceType](d.Get("resource_types").(*schema.Set)),
SinkIdentifier: aws.String(d.Get("sink_identifier").(string)),
Tags: getTagsIn(ctx),
LabelTemplate: aws.String(d.Get("label_template").(string)),
LinkConfiguration: expandLinkConfiguration(d.Get("link_configuration").([]interface{})),
ResourceTypes: flex.ExpandStringyValueSet[types.ResourceType](d.Get("resource_types").(*schema.Set)),
SinkIdentifier: aws.String(d.Get("sink_identifier").(string)),
Tags: getTagsIn(ctx),
}

out, err := conn.CreateLink(ctx, in)
Expand Down Expand Up @@ -137,6 +176,7 @@ func resourceLinkRead(ctx context.Context, d *schema.ResourceData, meta interfac
d.Set(names.AttrARN, out.Arn)
d.Set("label", out.Label)
d.Set("label_template", out.LabelTemplate)
d.Set("link_configuration", flattenLinkConfiguration(out.LinkConfiguration))
d.Set("link_id", out.Id)
d.Set("resource_types", flex.FlattenStringValueList(out.ResourceTypes))
d.Set("sink_arn", out.SinkArn)
Expand All @@ -155,8 +195,13 @@ func resourceLinkUpdate(ctx context.Context, d *schema.ResourceData, meta interf
Identifier: aws.String(d.Id()),
}

if d.HasChanges("resource_types") {
if d.HasChanges("resource_types", "link_configuration") {
in.ResourceTypes = flex.ExpandStringyValueSet[types.ResourceType](d.Get("resource_types").(*schema.Set))

if d.HasChanges("link_configuration") {
in.LinkConfiguration = expandLinkConfiguration(d.Get("link_configuration").([]interface{}))
}

update = true
}

Expand Down Expand Up @@ -216,3 +261,93 @@ func findLinkByID(ctx context.Context, conn *oam.Client, id string) (*oam.GetLin

return out, nil
}

func expandLinkConfiguration(l []interface{}) *types.LinkConfiguration {
if len(l) == 0 || l[0] == nil {
return nil
}

config := &types.LinkConfiguration{}

m := l[0].(map[string]interface{})
if v, ok := m["log_group_configuration"]; ok {
config.LogGroupConfiguration = expandLogGroupConfiguration(v.([]interface{}))
}
if v, ok := m["metric_configuration"]; ok {
config.MetricConfiguration = expandMetricConfiguration(v.([]interface{}))
}

return config
}

func expandLogGroupConfiguration(l []interface{}) *types.LogGroupConfiguration {
if len(l) == 0 || l[0] == nil {
return nil
}

config := &types.LogGroupConfiguration{}

m := l[0].(map[string]interface{})
if v, ok := m[names.AttrFilter]; ok && v != "" {
config.Filter = aws.String(v.(string))
}

return config
}

func expandMetricConfiguration(l []interface{}) *types.MetricConfiguration {
if len(l) == 0 || l[0] == nil {
return nil
}

config := &types.MetricConfiguration{}

m := l[0].(map[string]interface{})
if v, ok := m[names.AttrFilter]; ok && v != "" {
config.Filter = aws.String(v.(string))
}

return config
}

func flattenLinkConfiguration(a *types.LinkConfiguration) []interface{} {
if a == nil {
return []interface{}{}
}
m := map[string]interface{}{}

if a.LogGroupConfiguration != nil {
m["log_group_configuration"] = flattenLogGroupConfiguration(a.LogGroupConfiguration)
}
if a.MetricConfiguration != nil {
m["metric_configuration"] = flattenMetricConfiguration(a.MetricConfiguration)
}

return []interface{}{m}
}

func flattenLogGroupConfiguration(a *types.LogGroupConfiguration) []interface{} {
if a == nil {
return []interface{}{}
}
m := map[string]interface{}{}

if a.Filter != nil {
m[names.AttrFilter] = aws.ToString(a.Filter)
}

return []interface{}{m}
}

func flattenMetricConfiguration(a *types.MetricConfiguration) []interface{} {
if a == nil {
return []interface{}{}
}
m := map[string]interface{}{}

if a.Filter != nil {
m[names.AttrFilter] = aws.ToString(a.Filter)
}

return []interface{}{m}
}
47 changes: 40 additions & 7 deletions internal/service/oam/link_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,54 @@ func DataSourceLink() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"link_id": {
"label": {
Type: schema.TypeString,
Computed: true,
},
"link_identifier": {
"label_template": {
Type: schema.TypeString,
Required: true,
Computed: true,
},
"label": {
Type: schema.TypeString,
"link_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"log_group_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
names.AttrFilter: {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"metric_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
names.AttrFilter: {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
"label_template": {
"link_id": {
Type: schema.TypeString,
Computed: true,
},
"link_identifier": {
Type: schema.TypeString,
Required: true,
},
"resource_types": {
Type: schema.TypeSet,
Computed: true,
Expand Down Expand Up @@ -76,9 +108,10 @@ func dataSourceLinkRead(ctx context.Context, d *schema.ResourceData, meta interf
d.SetId(aws.ToString(out.Arn))

d.Set(names.AttrARN, out.Arn)
d.Set("link_id", out.Id)
d.Set("label", out.Label)
d.Set("label_template", out.LabelTemplate)
d.Set("link_configuration", flattenLinkConfiguration(out.LinkConfiguration))
d.Set("link_id", out.Id)
d.Set("resource_types", flex.FlattenStringValueList(out.ResourceTypes))
d.Set("sink_arn", out.SinkArn)

Expand Down
Loading
Loading