From 0638c6b1032f4afee7d9fa35d347481bb285706d Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Fri, 13 Oct 2023 11:11:13 +0200 Subject: [PATCH] fix: user config marshal error omitted --- .../service_integration_resource.go | 24 +++--- .../service/serviceintegration/userconfig.go | 81 +++++++++---------- .../clickhousekafka/clickhouse_kafka_test.go | 26 +++--- .../clickhouse_postgresql_test.go | 26 +++--- .../integration/datadog/datadog_test.go | 26 +++--- .../external_aws_cloudwatch_metrics_test.go | 26 +++--- .../kafkaconnect/kafka_connect_test.go | 26 +++--- .../integration/kafkalogs/kafka_logs_test.go | 26 +++--- .../kafka_mirrormaker_test.go | 26 +++--- .../userconfig/integration/logs/logs_test.go | 26 +++--- .../integration/metrics/metrics_test.go | 26 +++--- internal/schemautil/plugin.go | 15 ++-- .../dist/integration_endpoint_types.go | 3 +- .../userconfig/dist/integration_types.go | 3 +- .../userconfig/dist/service_types.go | 3 +- ucgenerator/tests.go | 8 +- 16 files changed, 184 insertions(+), 187 deletions(-) diff --git a/internal/plugin/service/serviceintegration/service_integration_resource.go b/internal/plugin/service/serviceintegration/service_integration_resource.go index 60a737bd3..6eb8883a9 100644 --- a/internal/plugin/service/serviceintegration/service_integration_resource.go +++ b/internal/plugin/service/serviceintegration/service_integration_resource.go @@ -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) @@ -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()), @@ -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(), diff --git a/internal/plugin/service/serviceintegration/userconfig.go b/internal/plugin/service/serviceintegration/userconfig.go index d398f18ba..70f3bced8 100644 --- a/internal/plugin/service/serviceintegration/userconfig.go +++ b/internal/plugin/service/serviceintegration/userconfig.go @@ -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" @@ -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 } diff --git a/internal/plugin/service/userconfig/integration/clickhousekafka/clickhouse_kafka_test.go b/internal/plugin/service/userconfig/integration/clickhousekafka/clickhouse_kafka_test.go index 77316cfac..0f1a9a6ee 100644 --- a/internal/plugin/service/userconfig/integration/clickhousekafka/clickhouse_kafka_test.go +++ b/internal/plugin/service/userconfig/integration/clickhousekafka/clickhouse_kafka_test.go @@ -73,22 +73,22 @@ const updateOnlyFields = `{ func TestUserConfig(t *testing.T) { cases := []struct { - name string - source string - expect string - marshal func(any) (map[string]any, error) + name string + source string + expect string + create bool }{ { - name: "fields to create resource", - source: allFields, - expect: allFields, - marshal: schemautil.MarshalCreateUserConfig, + name: "fields to create resource", + source: allFields, + expect: allFields, + create: true, }, { - name: "only fields to update resource", - source: allFields, - expect: updateOnlyFields, // usually, fewer fields - marshal: schemautil.MarshalUpdateUserConfig, + name: "only fields to update resource", + source: allFields, + expect: updateOnlyFields, // usually, fewer fields + create: false, }, } @@ -109,7 +109,7 @@ func TestUserConfig(t *testing.T) { require.Empty(t, diags) // Run specific marshal (create or update resource) - dtoConfig, err := opt.marshal(config) + dtoConfig, err := schemautil.MarshalUserConfig(config, opt.create) require.NoError(t, err) // Compares that output is strictly equal to the input diff --git a/internal/plugin/service/userconfig/integration/clickhousepostgresql/clickhouse_postgresql_test.go b/internal/plugin/service/userconfig/integration/clickhousepostgresql/clickhouse_postgresql_test.go index 217dea78b..330c56bc1 100644 --- a/internal/plugin/service/userconfig/integration/clickhousepostgresql/clickhouse_postgresql_test.go +++ b/internal/plugin/service/userconfig/integration/clickhousepostgresql/clickhouse_postgresql_test.go @@ -33,22 +33,22 @@ const updateOnlyFields = `{ func TestUserConfig(t *testing.T) { cases := []struct { - name string - source string - expect string - marshal func(any) (map[string]any, error) + name string + source string + expect string + create bool }{ { - name: "fields to create resource", - source: allFields, - expect: allFields, - marshal: schemautil.MarshalCreateUserConfig, + name: "fields to create resource", + source: allFields, + expect: allFields, + create: true, }, { - name: "only fields to update resource", - source: allFields, - expect: updateOnlyFields, // usually, fewer fields - marshal: schemautil.MarshalUpdateUserConfig, + name: "only fields to update resource", + source: allFields, + expect: updateOnlyFields, // usually, fewer fields + create: false, }, } @@ -69,7 +69,7 @@ func TestUserConfig(t *testing.T) { require.Empty(t, diags) // Run specific marshal (create or update resource) - dtoConfig, err := opt.marshal(config) + dtoConfig, err := schemautil.MarshalUserConfig(config, opt.create) require.NoError(t, err) // Compares that output is strictly equal to the input diff --git a/internal/plugin/service/userconfig/integration/datadog/datadog_test.go b/internal/plugin/service/userconfig/integration/datadog/datadog_test.go index 7dc27e200..04190b0f4 100644 --- a/internal/plugin/service/userconfig/integration/datadog/datadog_test.go +++ b/internal/plugin/service/userconfig/integration/datadog/datadog_test.go @@ -83,22 +83,22 @@ const updateOnlyFields = `{ func TestUserConfig(t *testing.T) { cases := []struct { - name string - source string - expect string - marshal func(any) (map[string]any, error) + name string + source string + expect string + create bool }{ { - name: "fields to create resource", - source: allFields, - expect: allFields, - marshal: schemautil.MarshalCreateUserConfig, + name: "fields to create resource", + source: allFields, + expect: allFields, + create: true, }, { - name: "only fields to update resource", - source: allFields, - expect: updateOnlyFields, // usually, fewer fields - marshal: schemautil.MarshalUpdateUserConfig, + name: "only fields to update resource", + source: allFields, + expect: updateOnlyFields, // usually, fewer fields + create: false, }, } @@ -119,7 +119,7 @@ func TestUserConfig(t *testing.T) { require.Empty(t, diags) // Run specific marshal (create or update resource) - dtoConfig, err := opt.marshal(config) + dtoConfig, err := schemautil.MarshalUserConfig(config, opt.create) require.NoError(t, err) // Compares that output is strictly equal to the input diff --git a/internal/plugin/service/userconfig/integration/externalawscloudwatchmetrics/external_aws_cloudwatch_metrics_test.go b/internal/plugin/service/userconfig/integration/externalawscloudwatchmetrics/external_aws_cloudwatch_metrics_test.go index 9cf794599..9795bc385 100644 --- a/internal/plugin/service/userconfig/integration/externalawscloudwatchmetrics/external_aws_cloudwatch_metrics_test.go +++ b/internal/plugin/service/userconfig/integration/externalawscloudwatchmetrics/external_aws_cloudwatch_metrics_test.go @@ -45,22 +45,22 @@ const updateOnlyFields = `{ func TestUserConfig(t *testing.T) { cases := []struct { - name string - source string - expect string - marshal func(any) (map[string]any, error) + name string + source string + expect string + create bool }{ { - name: "fields to create resource", - source: allFields, - expect: allFields, - marshal: schemautil.MarshalCreateUserConfig, + name: "fields to create resource", + source: allFields, + expect: allFields, + create: true, }, { - name: "only fields to update resource", - source: allFields, - expect: updateOnlyFields, // usually, fewer fields - marshal: schemautil.MarshalUpdateUserConfig, + name: "only fields to update resource", + source: allFields, + expect: updateOnlyFields, // usually, fewer fields + create: false, }, } @@ -81,7 +81,7 @@ func TestUserConfig(t *testing.T) { require.Empty(t, diags) // Run specific marshal (create or update resource) - dtoConfig, err := opt.marshal(config) + dtoConfig, err := schemautil.MarshalUserConfig(config, opt.create) require.NoError(t, err) // Compares that output is strictly equal to the input diff --git a/internal/plugin/service/userconfig/integration/kafkaconnect/kafka_connect_test.go b/internal/plugin/service/userconfig/integration/kafkaconnect/kafka_connect_test.go index 964993b87..69d9ae0cf 100644 --- a/internal/plugin/service/userconfig/integration/kafkaconnect/kafka_connect_test.go +++ b/internal/plugin/service/userconfig/integration/kafkaconnect/kafka_connect_test.go @@ -33,22 +33,22 @@ const updateOnlyFields = `{ func TestUserConfig(t *testing.T) { cases := []struct { - name string - source string - expect string - marshal func(any) (map[string]any, error) + name string + source string + expect string + create bool }{ { - name: "fields to create resource", - source: allFields, - expect: allFields, - marshal: schemautil.MarshalCreateUserConfig, + name: "fields to create resource", + source: allFields, + expect: allFields, + create: true, }, { - name: "only fields to update resource", - source: allFields, - expect: updateOnlyFields, // usually, fewer fields - marshal: schemautil.MarshalUpdateUserConfig, + name: "only fields to update resource", + source: allFields, + expect: updateOnlyFields, // usually, fewer fields + create: false, }, } @@ -69,7 +69,7 @@ func TestUserConfig(t *testing.T) { require.Empty(t, diags) // Run specific marshal (create or update resource) - dtoConfig, err := opt.marshal(config) + dtoConfig, err := schemautil.MarshalUserConfig(config, opt.create) require.NoError(t, err) // Compares that output is strictly equal to the input diff --git a/internal/plugin/service/userconfig/integration/kafkalogs/kafka_logs_test.go b/internal/plugin/service/userconfig/integration/kafkalogs/kafka_logs_test.go index 03a1c9ecc..166dd35a6 100644 --- a/internal/plugin/service/userconfig/integration/kafkalogs/kafka_logs_test.go +++ b/internal/plugin/service/userconfig/integration/kafkalogs/kafka_logs_test.go @@ -29,22 +29,22 @@ const updateOnlyFields = `{ func TestUserConfig(t *testing.T) { cases := []struct { - name string - source string - expect string - marshal func(any) (map[string]any, error) + name string + source string + expect string + create bool }{ { - name: "fields to create resource", - source: allFields, - expect: allFields, - marshal: schemautil.MarshalCreateUserConfig, + name: "fields to create resource", + source: allFields, + expect: allFields, + create: true, }, { - name: "only fields to update resource", - source: allFields, - expect: updateOnlyFields, // usually, fewer fields - marshal: schemautil.MarshalUpdateUserConfig, + name: "only fields to update resource", + source: allFields, + expect: updateOnlyFields, // usually, fewer fields + create: false, }, } @@ -65,7 +65,7 @@ func TestUserConfig(t *testing.T) { require.Empty(t, diags) // Run specific marshal (create or update resource) - dtoConfig, err := opt.marshal(config) + dtoConfig, err := schemautil.MarshalUserConfig(config, opt.create) require.NoError(t, err) // Compares that output is strictly equal to the input diff --git a/internal/plugin/service/userconfig/integration/kafkamirrormaker/kafka_mirrormaker_test.go b/internal/plugin/service/userconfig/integration/kafkamirrormaker/kafka_mirrormaker_test.go index 1e269a0d6..611545286 100644 --- a/internal/plugin/service/userconfig/integration/kafkamirrormaker/kafka_mirrormaker_test.go +++ b/internal/plugin/service/userconfig/integration/kafkamirrormaker/kafka_mirrormaker_test.go @@ -39,22 +39,22 @@ const updateOnlyFields = `{ func TestUserConfig(t *testing.T) { cases := []struct { - name string - source string - expect string - marshal func(any) (map[string]any, error) + name string + source string + expect string + create bool }{ { - name: "fields to create resource", - source: allFields, - expect: allFields, - marshal: schemautil.MarshalCreateUserConfig, + name: "fields to create resource", + source: allFields, + expect: allFields, + create: true, }, { - name: "only fields to update resource", - source: allFields, - expect: updateOnlyFields, // usually, fewer fields - marshal: schemautil.MarshalUpdateUserConfig, + name: "only fields to update resource", + source: allFields, + expect: updateOnlyFields, // usually, fewer fields + create: false, }, } @@ -75,7 +75,7 @@ func TestUserConfig(t *testing.T) { require.Empty(t, diags) // Run specific marshal (create or update resource) - dtoConfig, err := opt.marshal(config) + dtoConfig, err := schemautil.MarshalUserConfig(config, opt.create) require.NoError(t, err) // Compares that output is strictly equal to the input diff --git a/internal/plugin/service/userconfig/integration/logs/logs_test.go b/internal/plugin/service/userconfig/integration/logs/logs_test.go index 9635dcc31..bab47414f 100644 --- a/internal/plugin/service/userconfig/integration/logs/logs_test.go +++ b/internal/plugin/service/userconfig/integration/logs/logs_test.go @@ -31,22 +31,22 @@ const updateOnlyFields = `{ func TestUserConfig(t *testing.T) { cases := []struct { - name string - source string - expect string - marshal func(any) (map[string]any, error) + name string + source string + expect string + create bool }{ { - name: "fields to create resource", - source: allFields, - expect: allFields, - marshal: schemautil.MarshalCreateUserConfig, + name: "fields to create resource", + source: allFields, + expect: allFields, + create: true, }, { - name: "only fields to update resource", - source: allFields, - expect: updateOnlyFields, // usually, fewer fields - marshal: schemautil.MarshalUpdateUserConfig, + name: "only fields to update resource", + source: allFields, + expect: updateOnlyFields, // usually, fewer fields + create: false, }, } @@ -67,7 +67,7 @@ func TestUserConfig(t *testing.T) { require.Empty(t, diags) // Run specific marshal (create or update resource) - dtoConfig, err := opt.marshal(config) + dtoConfig, err := schemautil.MarshalUserConfig(config, opt.create) require.NoError(t, err) // Compares that output is strictly equal to the input diff --git a/internal/plugin/service/userconfig/integration/metrics/metrics_test.go b/internal/plugin/service/userconfig/integration/metrics/metrics_test.go index 07c3e6b2f..f20d69dd9 100644 --- a/internal/plugin/service/userconfig/integration/metrics/metrics_test.go +++ b/internal/plugin/service/userconfig/integration/metrics/metrics_test.go @@ -65,22 +65,22 @@ const updateOnlyFields = `{ func TestUserConfig(t *testing.T) { cases := []struct { - name string - source string - expect string - marshal func(any) (map[string]any, error) + name string + source string + expect string + create bool }{ { - name: "fields to create resource", - source: allFields, - expect: allFields, - marshal: schemautil.MarshalCreateUserConfig, + name: "fields to create resource", + source: allFields, + expect: allFields, + create: true, }, { - name: "only fields to update resource", - source: allFields, - expect: updateOnlyFields, // usually, fewer fields - marshal: schemautil.MarshalUpdateUserConfig, + name: "only fields to update resource", + source: allFields, + expect: updateOnlyFields, // usually, fewer fields + create: false, }, } @@ -101,7 +101,7 @@ func TestUserConfig(t *testing.T) { require.Empty(t, diags) // Run specific marshal (create or update resource) - dtoConfig, err := opt.marshal(config) + dtoConfig, err := schemautil.MarshalUserConfig(config, opt.create) require.NoError(t, err) // Compares that output is strictly equal to the input diff --git a/internal/schemautil/plugin.go b/internal/schemautil/plugin.go index 6f94a84e5..738624803 100644 --- a/internal/schemautil/plugin.go +++ b/internal/schemautil/plugin.go @@ -98,13 +98,10 @@ func marshalUserConfig(c any, groups ...string) (map[string]any, error) { return m, nil } -// MarshalCreateUserConfig returns marshaled user config for Create operation -func MarshalCreateUserConfig(c any) (map[string]any, error) { - return marshalUserConfig(c, "create", "update") -} - -// MarshalUpdateUserConfig returns marshaled user config for Update operation -func MarshalUpdateUserConfig(c any) (map[string]any, error) { +func MarshalUserConfig(c any, create bool) (map[string]any, error) { + if create { + return marshalUserConfig(c, "create", "update") + } return marshalUserConfig(c, "update") } @@ -151,3 +148,7 @@ func ValueFloat64Pointer(v types.Float64) *float64 { } return v.ValueFloat64Pointer() } + +func HasValue(o types.Set) bool { + return !(o.IsUnknown() || o.IsNull()) +} diff --git a/internal/schemautil/userconfig/dist/integration_endpoint_types.go b/internal/schemautil/userconfig/dist/integration_endpoint_types.go index 4369fec7f..55989ada9 100644 --- a/internal/schemautil/userconfig/dist/integration_endpoint_types.go +++ b/internal/schemautil/userconfig/dist/integration_endpoint_types.go @@ -3,9 +3,8 @@ package dist import ( - schema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - schemautil "github.com/aiven/terraform-provider-aiven/internal/schemautil" + schema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) // IntegrationEndpointTypeDatadog is a generated function returning the schema of the datadog IntegrationEndpointType. diff --git a/internal/schemautil/userconfig/dist/integration_types.go b/internal/schemautil/userconfig/dist/integration_types.go index de191b448..66b10740f 100644 --- a/internal/schemautil/userconfig/dist/integration_types.go +++ b/internal/schemautil/userconfig/dist/integration_types.go @@ -3,9 +3,8 @@ package dist import ( - schema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - schemautil "github.com/aiven/terraform-provider-aiven/internal/schemautil" + schema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) // IntegrationTypeClickhouseKafka is a generated function returning the schema of the clickhouse_kafka IntegrationType. diff --git a/internal/schemautil/userconfig/dist/service_types.go b/internal/schemautil/userconfig/dist/service_types.go index 337cbd298..cb772a8da 100644 --- a/internal/schemautil/userconfig/dist/service_types.go +++ b/internal/schemautil/userconfig/dist/service_types.go @@ -3,9 +3,8 @@ package dist import ( - schema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - schemautil "github.com/aiven/terraform-provider-aiven/internal/schemautil" + schema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) // ServiceTypeCassandra is a generated function returning the schema of the cassandra ServiceType. diff --git a/ucgenerator/tests.go b/ucgenerator/tests.go index fe63ec908..2cffe73e5 100644 --- a/ucgenerator/tests.go +++ b/ucgenerator/tests.go @@ -104,19 +104,19 @@ func Test%[3]s(t *testing.T) { name string source string expect string - marshal func (any) (map[string]any, error) + create bool }{ { name: "fields to create resource", source: allFields, expect: allFields, - marshal: schemautil.MarshalCreateUserConfig, + create: true, }, { name: "only fields to update resource", source: allFields, expect: updateOnlyFields, // usually, fewer fields - marshal: schemautil.MarshalUpdateUserConfig, + create: false, }, } @@ -137,7 +137,7 @@ func Test%[3]s(t *testing.T) { require.Empty(t, diags) // Run specific marshal (create or update resource) - dtoConfig, err := opt.marshal(config) + dtoConfig, err := schemautil.MarshalUserConfig(config, opt.create) require.NoError(t, err) // Compares that output is strictly equal to the input