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

Add logging configuration to aws_fis_experiment_template #32102

Merged
merged 8 commits into from
Jun 28, 2023
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/32102.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_fis_experiment_template: Add `log_configuration` configuration block
```
226 changes: 197 additions & 29 deletions internal/service/fis/experiment_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,49 @@ func ResourceExperimentTemplate() *schema.Resource {
Required: true,
ValidateFunc: validation.StringLenBetween(0, 512),
},
"log_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cloudwatch_logs_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"log_group_arn": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"log_schema_version": {
Type: schema.TypeInt,
Required: true,
},
"s3_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"bucket_name": {
Type: schema.TypeString,
Required: true,
},
"prefix": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
},
"role_arn": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -236,12 +279,13 @@ func resourceExperimentTemplateCreate(ctx context.Context, d *schema.ResourceDat
conn := meta.(*conns.AWSClient).FISClient(ctx)

input := &fis.CreateExperimentTemplateInput{
Actions: expandExperimentTemplateActions(d.Get("action").(*schema.Set)),
ClientToken: aws.String(id.UniqueId()),
Description: aws.String(d.Get("description").(string)),
RoleArn: aws.String(d.Get("role_arn").(string)),
StopConditions: expandExperimentTemplateStopConditions(d.Get("stop_condition").(*schema.Set)),
Tags: getTagsIn(ctx),
Actions: expandExperimentTemplateActions(d.Get("action").(*schema.Set)),
ClientToken: aws.String(id.UniqueId()),
Description: aws.String(d.Get("description").(string)),
LogConfiguration: expandExperimentTemplateLogConfiguration(d.Get("log_configuration").([]interface{})),
RoleArn: aws.String(d.Get("role_arn").(string)),
StopConditions: expandExperimentTemplateStopConditions(d.Get("stop_condition").(*schema.Set)),
Tags: getTagsIn(ctx),
}

targets, err := expandExperimentTemplateTargets(d.Get("target").(*schema.Set))
Expand Down Expand Up @@ -296,6 +340,10 @@ func resourceExperimentTemplateRead(ctx context.Context, d *schema.ResourceData,
return create.DiagSettingError(names.FIS, ResNameExperimentTemplate, d.Id(), "action", err)
}

if err := d.Set("log_configuration", flattenExperimentTemplateLogConfiguration(experimentTemplate.LogConfiguration)); err != nil {
return create.DiagSettingError(names.FIS, ResNameExperimentTemplate, d.Id(), "log_configuration", err)
}

if err := d.Set("stop_condition", flattenExperimentTemplateStopConditions(experimentTemplate.StopConditions)); err != nil {
return create.DiagSettingError(names.FIS, ResNameExperimentTemplate, d.Id(), "stop_condition", err)
}
Expand All @@ -312,37 +360,44 @@ func resourceExperimentTemplateRead(ctx context.Context, d *schema.ResourceData,
func resourceExperimentTemplateUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).FISClient(ctx)

input := &fis.UpdateExperimentTemplateInput{
Id: aws.String(d.Id()),
}
if d.HasChangesExcept("tags", "tags_all") {
input := &fis.UpdateExperimentTemplateInput{
Id: aws.String(d.Id()),
}

if d.HasChange("action") {
input.Actions = expandExperimentTemplateActionsForUpdate(d.Get("action").(*schema.Set))
}
if d.HasChange("action") {
input.Actions = expandExperimentTemplateActionsForUpdate(d.Get("action").(*schema.Set))
}

if d.HasChange("description") {
input.Description = aws.String(d.Get("description").(string))
}
if d.HasChange("description") {
input.Description = aws.String(d.Get("description").(string))
}

if d.HasChange("role_arn") {
input.RoleArn = aws.String(d.Get("role_arn").(string))
}
if d.HasChange("log_configuration") {
config := expandExperimentTemplateLogConfigurationForUpdate(d.Get("log_configuration").([]interface{}))
input.LogConfiguration = config
}

if d.HasChange("stop_condition") {
input.StopConditions = expandExperimentTemplateStopConditionsForUpdate(d.Get("stop_condition").(*schema.Set))
}
if d.HasChange("role_arn") {
input.RoleArn = aws.String(d.Get("role_arn").(string))
}

if d.HasChange("stop_condition") {
input.StopConditions = expandExperimentTemplateStopConditionsForUpdate(d.Get("stop_condition").(*schema.Set))
}

if d.HasChange("target") {
targets, err := expandExperimentTemplateTargetsForUpdate(d.Get("target").(*schema.Set))
if d.HasChange("target") {
targets, err := expandExperimentTemplateTargetsForUpdate(d.Get("target").(*schema.Set))
if err != nil {
return create.DiagError(names.FIS, create.ErrActionUpdating, ResNameExperimentTemplate, d.Id(), err)
}
input.Targets = targets
}

_, err := conn.UpdateExperimentTemplate(ctx, input)
if err != nil {
return create.DiagError(names.FIS, create.ErrActionUpdating, ResNameExperimentTemplate, d.Id(), err)
}
input.Targets = targets
}

_, err := conn.UpdateExperimentTemplate(ctx, input)
if err != nil {
return create.DiagError(names.FIS, create.ErrActionUpdating, ResNameExperimentTemplate, d.Id(), err)
}

return resourceExperimentTemplateRead(ctx, d, meta)
Expand Down Expand Up @@ -473,6 +528,58 @@ func expandExperimentTemplateStopConditions(l *schema.Set) []types.CreateExperim
return items
}

func expandExperimentTemplateLogConfiguration(l []interface{}) *types.CreateExperimentTemplateLogConfigurationInput {
if len(l) == 0 {
return nil
}

raw := l[0].(map[string]interface{})

config := types.CreateExperimentTemplateLogConfigurationInput{
LogSchemaVersion: aws.Int32(int32(raw["log_schema_version"].(int))),
}

if v, ok := raw["cloudwatch_logs_configuration"].([]interface{}); ok && len(v) > 0 {
config.CloudWatchLogsConfiguration = expandExperimentTemplateCloudWatchLogsConfiguration(v)
}

if v, ok := raw["s3_configuration"].([]interface{}); ok && len(v) > 0 {
config.S3Configuration = expandExperimentTemplateS3Configuration(v)
}

return &config
}

func expandExperimentTemplateCloudWatchLogsConfiguration(l []interface{}) *types.ExperimentTemplateCloudWatchLogsLogConfigurationInput {
if len(l) == 0 {
return nil
}

raw := l[0].(map[string]interface{})

config := types.ExperimentTemplateCloudWatchLogsLogConfigurationInput{
LogGroupArn: aws.String(raw["log_group_arn"].(string)),
}
return &config
}

func expandExperimentTemplateS3Configuration(l []interface{}) *types.ExperimentTemplateS3LogConfigurationInput {
if len(l) == 0 {
return nil
}

raw := l[0].(map[string]interface{})

config := types.ExperimentTemplateS3LogConfigurationInput{
BucketName: aws.String(raw["bucket_name"].(string)),
}
if v, ok := raw["prefix"].(string); ok && v != "" {
config.Prefix = aws.String(v)
}

return &config
}

func expandExperimentTemplateStopConditionsForUpdate(l *schema.Set) []types.UpdateExperimentTemplateStopConditionInput {
if l.Len() == 0 {
return nil
Expand Down Expand Up @@ -603,6 +710,26 @@ func expandExperimentTemplateTargetsForUpdate(l *schema.Set) (map[string]types.U
return attrs, nil
}

func expandExperimentTemplateLogConfigurationForUpdate(l []interface{}) *types.UpdateExperimentTemplateLogConfigurationInput {
if len(l) == 0 {
return &types.UpdateExperimentTemplateLogConfigurationInput{}
}

raw := l[0].(map[string]interface{})
config := types.UpdateExperimentTemplateLogConfigurationInput{
LogSchemaVersion: aws.Int32(int32(raw["log_schema_version"].(int))),
}
if v, ok := raw["cloudwatch_logs_configuration"].([]interface{}); ok && len(v) > 0 {
config.CloudWatchLogsConfiguration = expandExperimentTemplateCloudWatchLogsConfiguration(v)
}

if v, ok := raw["s3_configuration"].([]interface{}); ok && len(v) > 0 {
config.S3Configuration = expandExperimentTemplateS3Configuration(v)
}

return &config
}

func expandExperimentTemplateActionParameteres(l *schema.Set) map[string]string {
if l.Len() == 0 {
return nil
Expand Down Expand Up @@ -734,6 +861,47 @@ func flattenExperimentTemplateTargets(configured map[string]types.ExperimentTemp
return dataResources
}

func flattenExperimentTemplateLogConfiguration(configured *types.ExperimentTemplateLogConfiguration) []map[string]interface{} {
if configured == nil {
return make([]map[string]interface{}, 0)
}

dataResources := make([]map[string]interface{}, 1)
dataResources[0] = make(map[string]interface{})
dataResources[0]["log_schema_version"] = configured.LogSchemaVersion
dataResources[0]["cloudwatch_logs_configuration"] = flattenCloudWatchLogsConfiguration(configured.CloudWatchLogsConfiguration)
dataResources[0]["s3_configuration"] = flattenS3Configuration(configured.S3Configuration)

return dataResources
}

func flattenCloudWatchLogsConfiguration(configured *types.ExperimentTemplateCloudWatchLogsLogConfiguration) []map[string]interface{} {
if configured == nil {
return make([]map[string]interface{}, 0)
}

dataResources := make([]map[string]interface{}, 1)
dataResources[0] = make(map[string]interface{})
dataResources[0]["log_group_arn"] = configured.LogGroupArn

return dataResources
}

func flattenS3Configuration(configured *types.ExperimentTemplateS3LogConfiguration) []map[string]interface{} {
if configured == nil {
return make([]map[string]interface{}, 0)
}

dataResources := make([]map[string]interface{}, 1)
dataResources[0] = make(map[string]interface{})
dataResources[0]["bucket_name"] = configured.BucketName
if aws.ToString(configured.Prefix) != "" {
dataResources[0]["prefix"] = configured.Prefix
}

return dataResources
}

func flattenExperimentTemplateActionParameters(configured map[string]string) []map[string]interface{} {
dataResources := make([]map[string]interface{}, 0, len(configured))

Expand Down
Loading