From 4ad0490ebd00bf232543cb24b829480403083a06 Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Thu, 14 Mar 2024 14:31:21 +0100 Subject: [PATCH] fix(aiven_service_integration_endpoint): can't create external_postgresql --- internal/schemautil/service.go | 8 +--- .../userconfig/converters/converters.go | 40 +++++++++++-------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/internal/schemautil/service.go b/internal/schemautil/service.go index 05026d8aa..b28794903 100644 --- a/internal/schemautil/service.go +++ b/internal/schemautil/service.go @@ -445,9 +445,7 @@ func resourceServiceCreate(ctx context.Context, d *schema.ResourceData, m interf cuc, err := ExpandService(serviceType, d) if err != nil { - return diag.Errorf( - "error converting user config options for service type %s to API format: %s", serviceType, err, - ) + return diag.FromErr(err) } _, err = client.Services.Create( @@ -534,9 +532,7 @@ func ResourceServiceUpdate(ctx context.Context, d *schema.ResourceData, m interf serviceType := d.Get("service_type").(string) cuc, err := ExpandService(serviceType, d) if err != nil { - return diag.Errorf( - "error converting user config options for service type %s to API format: %s", serviceType, err, - ) + return diag.FromErr(err) } if _, err := client.Services.Update( diff --git a/internal/sdkprovider/userconfig/converters/converters.go b/internal/sdkprovider/userconfig/converters/converters.go index d052dd335..a6a57c79a 100644 --- a/internal/sdkprovider/userconfig/converters/converters.go +++ b/internal/sdkprovider/userconfig/converters/converters.go @@ -32,15 +32,15 @@ const ( AllowIPFilterPurge = "AIVEN_ALLOW_IP_FILTER_PURGE" ) -type userConfigType int +type userConfigType string const ( - ServiceUserConfig userConfigType = iota - ServiceIntegrationUserConfig - ServiceIntegrationEndpointUserConfig + ServiceUserConfig userConfigType = "service" + ServiceIntegrationUserConfig userConfigType = "service integration" + ServiceIntegrationEndpointUserConfig userConfigType = "service integration endpoint" ) -// userConfigKey provides a single source of truth for field naming +// userConfigKey provides a single source of truth for a field naming func userConfigKey(kind userConfigType, name string) string { switch kind { case ServiceIntegrationEndpointUserConfig: @@ -53,6 +53,7 @@ func userConfigKey(kind userConfigType, name string) string { return name + userConfigSuffix } +// getUserConfig returns user config for the given kind and name func getUserConfig(kind userConfigType, name string) *schema.Schema { switch kind { case ServiceUserConfig: @@ -68,10 +69,17 @@ func getUserConfig(kind userConfigType, name string) *schema.Schema { func SetUserConfig(kind userConfigType, name string, s map[string]*schema.Schema) { s[userConfigKey(kind, name)] = getUserConfig(kind, name) } +func Expand(kind userConfigType, name string, d *schema.ResourceData) (map[string]any, error) { + m, err := expand(kind, name, d) + if err != nil { + return nil, fmt.Errorf("error converting user config options for %s type %q to API format: %w", kind, name, err) + } + return m, nil +} -// Expand expands schema.ResourceData into a DTO map. +// 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) { +func expand(kind userConfigType, name string, d *schema.ResourceData) (map[string]any, error) { key := userConfigKey(kind, name) state := &stateCompose{ key: key, @@ -272,7 +280,15 @@ func expandAttr(state *stateCompose) (any, error) { return items, nil } -// flatten flattens DTO into a terraform compatible object +func Flatten(kind userConfigType, name string, d *schema.ResourceData, dto map[string]any) error { + err := flatten(kind, name, d, dto) + if err != nil { + return fmt.Errorf("error converting user config options for %s type %q from API format: %w", kind, name, err) + } + return nil +} + +// 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 { key := userConfigKey(kind, name) prefix := fmt.Sprintf("%s.0.", key) @@ -306,14 +322,6 @@ func flatten(kind userConfigType, name string, d *schema.ResourceData, dto map[s return d.Set(key, []map[string]any{tfo}) } -func Flatten(kind userConfigType, name string, d *schema.ResourceData, dto map[string]any) error { - err := flatten(kind, name, d, dto) - if err != nil { - return fmt.Errorf("cannot set user_config: %w", err) - } - return nil -} - func flattenObj(s map[string]*schema.Schema, dto map[string]any) (map[string]any, error) { tfo := make(map[string]any) for k, v := range s {