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

f-aws_pipes_pipe: support for logging #37135

Merged
merged 18 commits into from
Jun 3, 2024
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
3 changes: 3 additions & 0 deletions .changelog/37135.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_pipes_pipe: Add `log_configuration` argument
```
241 changes: 241 additions & 0 deletions internal/service/pipes/log_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package pipes

import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/pipes/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/names"
)

func logConfigurationSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"level": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: enum.Validate[types.LogLevel](),
},
"cloudwatch_logs_log_destination": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"log_group_arn": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"firehose_log_destination": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delivery_stream_arn": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"s3_log_destination": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
names.AttrBucketName: {
Type: schema.TypeString,
Required: true,
},
"bucket_owner": {
Type: schema.TypeString,
Required: true,
},
"output_format": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: enum.Validate[types.S3OutputFormat](),
},
names.AttrPrefix: {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
}
}

func expandPipeLogConfigurationParameters(tfMap map[string]interface{}) *types.PipeLogConfigurationParameters {
if tfMap == nil {
return nil
}

apiObject := &types.PipeLogConfigurationParameters{}

if v, ok := tfMap["level"].(string); ok && v != "" {
apiObject.Level = types.LogLevel(v)
}

if v, ok := tfMap["cloudwatch_logs_log_destination"].([]interface{}); ok && len(v) > 0 && v[0] != nil {
apiObject.CloudwatchLogsLogDestination = expandCloudWatchLogsLogDestinationParameters(v[0].(map[string]interface{}))
}

if v, ok := tfMap["firehose_log_destination"].([]interface{}); ok && len(v) > 0 && v[0] != nil {
apiObject.FirehoseLogDestination = expandFirehoseLogDestinationParameters(v[0].(map[string]interface{}))
}

if v, ok := tfMap["s3_log_destination"].([]interface{}); ok && len(v) > 0 && v[0] != nil {
apiObject.S3LogDestination = expandS3LogDestinationParameters(v[0].(map[string]interface{}))
}

return apiObject
}

func expandCloudWatchLogsLogDestinationParameters(tfMap map[string]interface{}) *types.CloudwatchLogsLogDestinationParameters {
if tfMap == nil {
return nil
}

apiObject := &types.CloudwatchLogsLogDestinationParameters{}

if v, ok := tfMap["log_group_arn"].(string); ok && v != "" {
apiObject.LogGroupArn = aws.String(v)
}

return apiObject
}

func expandFirehoseLogDestinationParameters(tfMap map[string]interface{}) *types.FirehoseLogDestinationParameters {
if tfMap == nil {
return nil
}

apiObject := &types.FirehoseLogDestinationParameters{}

if v, ok := tfMap["delivery_stream_arn"].(string); ok && v != "" {
apiObject.DeliveryStreamArn = aws.String(v)
}

return apiObject
}

func expandS3LogDestinationParameters(tfMap map[string]interface{}) *types.S3LogDestinationParameters {
if tfMap == nil {
return nil
}

apiObject := &types.S3LogDestinationParameters{}

if v, ok := tfMap[names.AttrBucketName].(string); ok && v != "" {
apiObject.BucketName = aws.String(v)
}

if v, ok := tfMap["bucket_owner"].(string); ok && v != "" {
apiObject.BucketOwner = aws.String(v)
}

if v, ok := tfMap["output_format"].(string); ok && v != "" {
apiObject.OutputFormat = types.S3OutputFormat(v)
}

if v, ok := tfMap[names.AttrPrefix].(string); ok && v != "" {
apiObject.Prefix = aws.String(v)
}

return apiObject
}

func flattenPipeLogConfiguration(apiObject *types.PipeLogConfiguration) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.Level; v != "" {
tfMap["level"] = v
}

if v := apiObject.CloudwatchLogsLogDestination; v != nil {
tfMap["cloudwatch_logs_log_destination"] = []interface{}{flattenCloudWatchLogsLogDestination(v)}
}

if v := apiObject.FirehoseLogDestination; v != nil {
tfMap["firehose_log_destination"] = []interface{}{flattenFirehoseLogDestination(v)}
}

if v := apiObject.S3LogDestination; v != nil {
tfMap["s3_log_destination"] = []interface{}{flattenS3LogDestination(v)}
}

return tfMap
}

func flattenCloudWatchLogsLogDestination(apiObject *types.CloudwatchLogsLogDestination) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.LogGroupArn; v != nil {
tfMap["log_group_arn"] = aws.ToString(v)
}

return tfMap
}

func flattenFirehoseLogDestination(apiObject *types.FirehoseLogDestination) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.DeliveryStreamArn; v != nil {
tfMap["delivery_stream_arn"] = aws.ToString(v)
}

return tfMap
}

func flattenS3LogDestination(apiObject *types.S3LogDestination) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.BucketName; v != nil {
tfMap[names.AttrBucketName] = aws.ToString(v)
}

if v := apiObject.BucketOwner; v != nil {
tfMap["bucket_owner"] = aws.ToString(v)
}

if v := apiObject.OutputFormat; v != "" {
tfMap["output_format"] = v
}

if v := apiObject.Prefix; v != nil {
tfMap[names.AttrPrefix] = aws.ToString(v)
}

return tfMap
}
18 changes: 18 additions & 0 deletions internal/service/pipes/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func resourcePipe() *schema.Resource {
ValidateFunc: verify.ValidARN,
},
"enrichment_parameters": enrichmentParametersSchema(),
"log_configuration": logConfigurationSchema(),
names.AttrName: {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -158,6 +159,10 @@ func resourcePipeCreate(ctx context.Context, d *schema.ResourceData, meta interf
input.TargetParameters = expandPipeTargetParameters(v.([]interface{})[0].(map[string]interface{}))
}

if v, ok := d.GetOk("log_configuration"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.LogConfiguration = expandPipeLogConfigurationParameters(v.([]interface{})[0].(map[string]interface{}))
}

output, err := conn.CreatePipe(ctx, input)

if err != nil {
Expand Down Expand Up @@ -199,6 +204,13 @@ func resourcePipeRead(ctx context.Context, d *schema.ResourceData, meta interfac
} else {
d.Set("enrichment_parameters", nil)
}
if v := output.LogConfiguration; !types.IsZero(v) {
if err := d.Set("log_configuration", []interface{}{flattenPipeLogConfiguration(v)}); err != nil {
return diag.Errorf("setting log_configuration: %s", err)
}
} else {
d.Set("log_configuration", nil)
}
d.Set(names.AttrName, output.Name)
d.Set(names.AttrNamePrefix, create.NamePrefixFromName(aws.ToString(output.Name)))
d.Set(names.AttrRoleARN, output.RoleArn)
Expand Down Expand Up @@ -248,6 +260,12 @@ func resourcePipeUpdate(ctx context.Context, d *schema.ResourceData, meta interf
}
}

if d.HasChange("log_configuration") {
if v, ok := d.GetOk("log_configuration"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.LogConfiguration = expandPipeLogConfigurationParameters(v.([]interface{})[0].(map[string]interface{}))
}
}

if d.HasChange("source_parameters") {
if v, ok := d.GetOk("source_parameters"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.SourceParameters = expandUpdatePipeSourceParameters(v.([]interface{})[0].(map[string]interface{}))
Expand Down
Loading
Loading