From 3e259106566d926fc71bf2067e24cd0142b14b88 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 | 44 ++++++++++++------- 2 files changed, 29 insertions(+), 23 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..343a777fc 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: @@ -65,13 +66,22 @@ func getUserConfig(kind userConfigType, name string) *schema.Schema { panic(fmt.Sprintf("unknown user config name %q with kind %q", name, kind)) } +// 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) } -// 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) { + 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. +// 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) { key := userConfigKey(kind, name) state := &stateCompose{ key: key, @@ -115,7 +125,7 @@ type stateCompose struct { key string // state attribute name or schema.ResourceData key path string // schema.ResourceData path, i.e., foo.0.bar.0.baz to get the value schema *schema.Schema // tf schema - config cty.Value // tf file values + config cty.Value // tf file values, it knows if resource value is null resource *schema.ResourceData // tf resource that has both tf state and data that is received from the API } @@ -272,7 +282,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 +324,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 {