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 69fbed4
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 176 deletions.
Original file line number Diff line number Diff line change
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):

Check failure on line 76 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / make_lint

undefined: schemautil.HasValue

Check failure on line 76 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / go_test

undefined: schemautil.HasValue

Check failure on line 76 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / check_docs_were_generated

undefined: schemautil.HasValue
o.ClickhouseKafkaUserConfig = clickhousekafka.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.ClickhousePostgresqlUserConfig):
case schemautil.HasValue(o.ClickhousePostgresqlUserConfig):

Check failure on line 78 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / make_lint

undefined: schemautil.HasValue

Check failure on line 78 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / go_test

undefined: schemautil.HasValue

Check failure on line 78 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / check_docs_were_generated

undefined: schemautil.HasValue
o.ClickhousePostgresqlUserConfig = clickhousepostgresql.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.DatadogUserConfig):
case schemautil.HasValue(o.DatadogUserConfig):

Check failure on line 80 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / make_lint

undefined: schemautil.HasValue

Check failure on line 80 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / go_test

undefined: schemautil.HasValue

Check failure on line 80 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / check_docs_were_generated

undefined: schemautil.HasValue
o.DatadogUserConfig = datadog.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.ExternalAwsCloudwatchMetricsUserConfig):
case schemautil.HasValue(o.ExternalAwsCloudwatchMetricsUserConfig):

Check failure on line 82 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / make_lint

undefined: schemautil.HasValue

Check failure on line 82 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / go_test

undefined: schemautil.HasValue

Check failure on line 82 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / check_docs_were_generated

undefined: schemautil.HasValue
o.ExternalAwsCloudwatchMetricsUserConfig = externalawscloudwatchmetrics.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.KafkaConnectUserConfig):
case schemautil.HasValue(o.KafkaConnectUserConfig):

Check failure on line 84 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / make_lint

undefined: schemautil.HasValue

Check failure on line 84 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / go_test

undefined: schemautil.HasValue

Check failure on line 84 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / check_docs_were_generated

undefined: schemautil.HasValue
o.KafkaConnectUserConfig = kafkaconnect.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.KafkaLogsUserConfig):
case schemautil.HasValue(o.KafkaLogsUserConfig):

Check failure on line 86 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / make_lint

undefined: schemautil.HasValue

Check failure on line 86 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / go_test

undefined: schemautil.HasValue

Check failure on line 86 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / check_docs_were_generated

undefined: schemautil.HasValue
o.KafkaLogsUserConfig = kafkalogs.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.KafkaMirrormakerUserConfig):
case schemautil.HasValue(o.KafkaMirrormakerUserConfig):

Check failure on line 88 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / make_lint

undefined: schemautil.HasValue

Check failure on line 88 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / go_test

undefined: schemautil.HasValue

Check failure on line 88 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / check_docs_were_generated

undefined: schemautil.HasValue
o.KafkaMirrormakerUserConfig = kafkamirrormaker.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.LogsUserConfig):
case schemautil.HasValue(o.LogsUserConfig):

Check failure on line 90 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / make_lint

undefined: schemautil.HasValue

Check failure on line 90 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / go_test

undefined: schemautil.HasValue

Check failure on line 90 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / check_docs_were_generated

undefined: schemautil.HasValue
o.LogsUserConfig = logs.Flatten(ctx, diags, dto.UserConfig)
case isSet(o.MetricsUserConfig):
case schemautil.HasValue(o.MetricsUserConfig):

Check failure on line 92 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / make_lint

undefined: schemautil.HasValue

Check failure on line 92 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / go_test

undefined: schemautil.HasValue

Check failure on line 92 in internal/plugin/service/serviceintegration/userconfig.go

View workflow job for this annotation

GitHub Actions / check_docs_were_generated

undefined: schemautil.HasValue
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 69fbed4

Please sign in to comment.