diff --git a/internal/sdkprovider/service/serviceintegration/service_integration.go b/internal/sdkprovider/service/serviceintegration/service_integration.go index 7818f94d7..93710bfe3 100644 --- a/internal/sdkprovider/service/serviceintegration/service_integration.go +++ b/internal/sdkprovider/service/serviceintegration/service_integration.go @@ -13,12 +13,9 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" - "golang.org/x/exp/slices" "github.com/aiven/terraform-provider-aiven/internal/common" "github.com/aiven/terraform-provider-aiven/internal/schemautil" - "github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig" - "github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/apiconvert" "github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader" "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters" "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/serviceintegration" @@ -26,10 +23,6 @@ import ( const serviceIntegrationEndpointRegExp = "^[a-zA-Z0-9_-]*\\/{1}[a-zA-Z0-9_-]*$" -func hasIntegrationConfig(kind string) bool { - return slices.Contains(serviceintegration.UserConfigTypes(), kind) -} - func aivenServiceIntegrationSchema() map[string]*schema.Schema { s := map[string]*schema.Schema{ "integration_id": { @@ -143,14 +136,13 @@ func resourceServiceIntegrationCreate(ctx context.Context, d *schema.ResourceDat SourceService: schemautil.OptionalStringPointer(d, "source_service_name"), } - if hasIntegrationConfig(integrationType) { - uc, err := converters.Expand(converters.ServiceIntegrationUserConfig, integrationType, d) - if err != nil { - return diag.FromErr(err) - } - req.UserConfig = uc + uc, err := converters.Expand(converters.ServiceIntegrationUserConfig, integrationType, d) + if err != nil { + return diag.FromErr(err) } + req.UserConfig = uc + res, err := client.ServiceIntegrations.Create(ctx, projectName, req) if err != nil { return diag.Errorf("error creating service integration: %s", err) @@ -195,7 +187,8 @@ func resourceServiceIntegrationUpdate(ctx context.Context, d *schema.ResourceDat return diag.FromErr(err) } - userConfig, err := resourceServiceIntegrationUserConfigFromSchemaToAPI(d) + integrationType := d.Get("integration_type").(string) + userConfig, err := converters.Expand(converters.ServiceIntegrationUserConfig, integrationType, d) if err != nil { return diag.FromErr(err) } @@ -318,11 +311,6 @@ func resourceServiceIntegrationWaitUntilActive(ctx context.Context, d *schema.Re return nil } -func resourceServiceIntegrationUserConfigFromSchemaToAPI(d *schema.ResourceData) (map[string]interface{}, error) { - integrationType := d.Get("integration_type").(string) - return apiconvert.ToAPI(userconfig.IntegrationTypes, integrationType, d) -} - func resourceServiceIntegrationCopyAPIResponseToTerraform( d *schema.ResourceData, res *aiven.ServiceIntegration, @@ -358,12 +346,5 @@ func resourceServiceIntegrationCopyAPIResponseToTerraform( return err } - if hasIntegrationConfig(integrationType) { - err := converters.Flatten(converters.ServiceIntegrationUserConfig, integrationType, d, res.UserConfig) - if err != nil { - return err - } - } - - return nil + return converters.Flatten(converters.ServiceIntegrationUserConfig, integrationType, d, res.UserConfig) } diff --git a/internal/sdkprovider/userconfig/converters/converters.go b/internal/sdkprovider/userconfig/converters/converters.go index 343a777fc..614e30f36 100644 --- a/internal/sdkprovider/userconfig/converters/converters.go +++ b/internal/sdkprovider/userconfig/converters/converters.go @@ -63,12 +63,16 @@ func getUserConfig(kind userConfigType, name string) *schema.Schema { case ServiceIntegrationEndpointUserConfig: return serviceintegrationendpoint.GetUserConfig(name) } - panic(fmt.Sprintf("unknown user config name %q with kind %q", name, kind)) + return nil } // SetUserConfig sets user config schema for given kind and name func SetUserConfig(kind userConfigType, name string, s map[string]*schema.Schema) { - s[userConfigKey(kind, name)] = getUserConfig(kind, name) + userConfig := getUserConfig(kind, name) + if userConfig == nil { + panic(fmt.Sprintf("unknown user config for %s type %q", kind, name)) + } + s[userConfigKey(kind, name)] = userConfig } func Expand(kind userConfigType, name string, d *schema.ResourceData) (map[string]any, error) { @@ -82,6 +86,11 @@ func Expand(kind userConfigType, name string, d *schema.ResourceData) (map[strin // expand expands schema.ResourceData into a DTO map. // It takes schema.Schema to know how to turn a TF item into json. func expand(kind userConfigType, name string, d *schema.ResourceData) (map[string]any, error) { + if getUserConfig(kind, name) == nil { + // does not have a user config for given kind and name + return nil, nil + } + key := userConfigKey(kind, name) state := &stateCompose{ key: key, @@ -292,6 +301,11 @@ func Flatten(kind userConfigType, name string, d *schema.ResourceData, dto map[s // flatten flattens DTO into a terraform compatible object and sets value to the field func flatten(kind userConfigType, name string, d *schema.ResourceData, dto map[string]any) error { + if getUserConfig(kind, name) == nil { + // does not have a user config for given kind and name + return nil + } + key := userConfigKey(kind, name) prefix := fmt.Sprintf("%s.0.", key) diff --git a/internal/sdkprovider/userconfig/service/service.go b/internal/sdkprovider/userconfig/service/service.go index 66293a2b4..46b84fd35 100644 --- a/internal/sdkprovider/userconfig/service/service.go +++ b/internal/sdkprovider/userconfig/service/service.go @@ -37,7 +37,7 @@ func GetUserConfig(kind string) *schema.Schema { case "redis": return redisUserConfig() default: - panic("unknown user config type: " + kind) + return nil } } func UserConfigTypes() []string { diff --git a/internal/sdkprovider/userconfig/serviceintegration/serviceintegration.go b/internal/sdkprovider/userconfig/serviceintegration/serviceintegration.go index 8710e5cb5..99e43589a 100644 --- a/internal/sdkprovider/userconfig/serviceintegration/serviceintegration.go +++ b/internal/sdkprovider/userconfig/serviceintegration/serviceintegration.go @@ -33,7 +33,7 @@ func GetUserConfig(kind string) *schema.Schema { case "prometheus": return prometheusUserConfig() default: - panic("unknown user config type: " + kind) + return nil } } func UserConfigTypes() []string { diff --git a/internal/sdkprovider/userconfig/serviceintegrationendpoint/serviceintegrationendpoint.go b/internal/sdkprovider/userconfig/serviceintegrationendpoint/serviceintegrationendpoint.go index c7d8635f9..4c2752993 100644 --- a/internal/sdkprovider/userconfig/serviceintegrationendpoint/serviceintegrationendpoint.go +++ b/internal/sdkprovider/userconfig/serviceintegrationendpoint/serviceintegrationendpoint.go @@ -33,7 +33,7 @@ func GetUserConfig(kind string) *schema.Schema { case "rsyslog": return rsyslogUserConfig() default: - panic("unknown user config type: " + kind) + return nil } } func UserConfigTypes() []string { diff --git a/main.go b/main.go index d1fa01e9c..9ceaeaf08 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ import ( "github.com/aiven/terraform-provider-aiven/internal/server" ) -//go:generate go test -tags userconfig ./internal/schemautil/userconfig //go:generate go run ./ucgenerator/... --excludeServices elasticsearch // registryPrefix is the registry prefix for the provider. diff --git a/ucgenerator/main.go b/ucgenerator/main.go index 67badd672..ea7d19c88 100644 --- a/ucgenerator/main.go +++ b/ucgenerator/main.go @@ -126,7 +126,7 @@ func generate(kind string, data []byte, exclude []string) error { } // Panics if unknown kind requested - cases = append(cases, jen.Default().Block(jen.Panic(jen.Lit("unknown user config type: ").Op("+").Id("kind")))) + cases = append(cases, jen.Default().Block(jen.Return(jen.Nil()))) f := jen.NewFile(kind) f.HeaderComment(codeGenerated)