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 c49c95c commit 0638c6b
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (s *serviceIntegrationResource) Create(ctx context.Context, req resource.Cr
// all other integrations should be imported using `terraform import`
if o.IntegrationType.ValueString() == readReplicaType {
if preexisting, err := getSIByName(ctx, s.client, &o); err != nil {
resp.Diagnostics.AddError("unable to search for possible preexisting 'read_replica' service integration", err.Error())
resp.Diagnostics.AddError("Unable to search for possible preexisting 'read_replica' service integration", err.Error())
return
} else if preexisting != nil {
o.IntegrationID = types.StringValue(preexisting.ServiceIntegrationID)
Expand All @@ -170,11 +170,11 @@ func (s *serviceIntegrationResource) Create(ctx context.Context, req resource.Cr
}
}

userConfig, err := expandUserConfig(ctx, &resp.Diagnostics, &o, true)
if err != nil {
resp.Diagnostics.AddError("Failed to expand user config", err.Error())
userConfig := expandUserConfig(ctx, &resp.Diagnostics, &o, true)
if resp.Diagnostics.HasError() {
return
}

createReq := aiven.CreateServiceIntegrationRequest{
DestinationProject: getProjectPointer(o.DestinationEndpointID.ValueString()),
DestinationEndpointID: getEndpointIDPointer(o.DestinationEndpointID.ValueString()),
Expand Down Expand Up @@ -206,27 +206,27 @@ func (s *serviceIntegrationResource) Read(ctx context.Context, req resource.Read
}

func (s *serviceIntegrationResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var state resourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
var o resourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &o)...)
if resp.Diagnostics.HasError() {
return
}

var o resourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &o)...)
// We read state to get integration's ID
var state resourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

// Copies ID from the state
o.IntegrationID = state.IntegrationID
userConfig, err := expandUserConfig(ctx, &resp.Diagnostics, &o, false)
if err != nil {
resp.Diagnostics.AddError("Failed to expand user config", err.Error())
userConfig := expandUserConfig(ctx, &resp.Diagnostics, &o, false)
if resp.Diagnostics.HasError() {
return
}

_, err = s.client.ServiceIntegrations.Update(
_, err := s.client.ServiceIntegrations.Update(
ctx,
state.Project.ValueString(),
state.IntegrationID.ValueString(),
Expand Down
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.

Loading

0 comments on commit 0638c6b

Please sign in to comment.