Skip to content

Commit

Permalink
fix: user config marshal error omitted
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Oct 13, 2023
1 parent f707c99 commit 2b318ef
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 175 deletions.
81 changes: 40 additions & 41 deletions internal/plugin/service/serviceintegration/userconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/aiven/terraform-provider-aiven/internal/plugin/service/userconfig/integration/clickhousekafka"
"github.com/aiven/terraform-provider-aiven/internal/plugin/service/userconfig/integration/clickhousepostgresql"
Expand Down Expand Up @@ -74,63 +73,63 @@ func flattenUserConfig(ctx context.Context, diags *diag.Diagnostics, o *resource
// We set user config from Aiven only if it's been set in TF
// Otherwise it will produce invalid "after apply"
switch {
case isSet(o.ClickhouseKafkaUserConfig):
case schemautil.HasValue(o.ClickhouseKafkaUserConfig):
o.ClickhouseKafkaUserConfig = clickhousekafka.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.ClickhousePostgresqlUserConfig):
case schemautil.HasValue(o.ClickhousePostgresqlUserConfig):
o.ClickhousePostgresqlUserConfig = clickhousepostgresql.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.DatadogUserConfig):
case schemautil.HasValue(o.DatadogUserConfig):
o.DatadogUserConfig = datadog.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.ExternalAwsCloudwatchMetricsUserConfig):
case schemautil.HasValue(o.ExternalAwsCloudwatchMetricsUserConfig):
o.ExternalAwsCloudwatchMetricsUserConfig = externalawscloudwatchmetrics.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.KafkaConnectUserConfig):
case schemautil.HasValue(o.KafkaConnectUserConfig):
o.KafkaConnectUserConfig = kafkaconnect.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.KafkaLogsUserConfig):
case schemautil.HasValue(o.KafkaLogsUserConfig):
o.KafkaLogsUserConfig = kafkalogs.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.KafkaMirrormakerUserConfig):
case schemautil.HasValue(o.KafkaMirrormakerUserConfig):
o.KafkaMirrormakerUserConfig = kafkamirrormaker.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.LogsUserConfig):
case schemautil.HasValue(o.LogsUserConfig):
o.LogsUserConfig = logs.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.MetricsUserConfig):
case schemautil.HasValue(o.MetricsUserConfig):
o.MetricsUserConfig = metrics.Flatten(ctx, diags, dto.UserConfig)
}
}

// expandUserConfig from terraform to aiven
func expandUserConfig(ctx context.Context, diags *diag.Diagnostics, o *resourceModel, create bool) (map[string]any, error) {
var marshal func(any) (map[string]any, error)
if create {
marshal = schemautil.MarshalCreateUserConfig
} else {
marshal = schemautil.MarshalUpdateUserConfig
}
func expandUserConfig(ctx context.Context, diags *diag.Diagnostics, o *resourceModel, create bool) map[string]any {
var config any

// If invalid integration type is set
// If an invalid integration type is set
// This will send wrong config to Aiven
// Which is sort of a validation too
switch {
case isSet(o.ClickhouseKafkaUserConfig):
return marshal(clickhousekafka.Expand(ctx, diags, o.ClickhouseKafkaUserConfig))
case isSet(o.ClickhousePostgresqlUserConfig):
return marshal(clickhousepostgresql.Expand(ctx, diags, o.ClickhousePostgresqlUserConfig))
case isSet(o.DatadogUserConfig):
return marshal(datadog.Expand(ctx, diags, o.DatadogUserConfig))
case isSet(o.ExternalAwsCloudwatchMetricsUserConfig):
return marshal(externalawscloudwatchmetrics.Expand(ctx, diags, o.ExternalAwsCloudwatchMetricsUserConfig))
case isSet(o.KafkaConnectUserConfig):
return marshal(kafkaconnect.Expand(ctx, diags, o.KafkaConnectUserConfig))
case isSet(o.KafkaLogsUserConfig):
return marshal(kafkalogs.Expand(ctx, diags, o.KafkaLogsUserConfig))
case isSet(o.KafkaMirrormakerUserConfig):
return marshal(kafkamirrormaker.Expand(ctx, diags, o.KafkaMirrormakerUserConfig))
case isSet(o.LogsUserConfig):
return marshal(logs.Expand(ctx, diags, o.LogsUserConfig))
case isSet(o.MetricsUserConfig):
return marshal(metrics.Expand(ctx, diags, o.MetricsUserConfig))
default:
return nil, nil
case schemautil.HasValue(o.ClickhouseKafkaUserConfig):
config = clickhousekafka.Expand(ctx, diags, o.ClickhouseKafkaUserConfig)
case schemautil.HasValue(o.ClickhousePostgresqlUserConfig):
config = clickhousepostgresql.Expand(ctx, diags, o.ClickhousePostgresqlUserConfig)
case schemautil.HasValue(o.DatadogUserConfig):
config = datadog.Expand(ctx, diags, o.DatadogUserConfig)
case schemautil.HasValue(o.ExternalAwsCloudwatchMetricsUserConfig):
config = externalawscloudwatchmetrics.Expand(ctx, diags, o.ExternalAwsCloudwatchMetricsUserConfig)
case schemautil.HasValue(o.KafkaConnectUserConfig):
config = kafkaconnect.Expand(ctx, diags, o.KafkaConnectUserConfig)
case schemautil.HasValue(o.KafkaLogsUserConfig):
config = kafkalogs.Expand(ctx, diags, o.KafkaLogsUserConfig)
case schemautil.HasValue(o.KafkaMirrormakerUserConfig):
config = kafkamirrormaker.Expand(ctx, diags, o.KafkaMirrormakerUserConfig)
case schemautil.HasValue(o.LogsUserConfig):
config = logs.Expand(ctx, diags, o.LogsUserConfig)
case schemautil.HasValue(o.MetricsUserConfig):
config = metrics.Expand(ctx, diags, o.MetricsUserConfig)
}
}

func isSet(o types.Set) bool {
return !(o.IsUnknown() || o.IsNull())
if diags.HasError() {
return nil
}

dict, err := schemautil.MarshalUserConfig(config, create)
if err != nil {
diags.AddError("Failed to expand user config", err.Error())
return nil
}
return dict
}

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

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

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

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

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

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

Loading

0 comments on commit 2b318ef

Please sign in to comment.