Skip to content

Commit

Permalink
fix(aiven_service_integration_endpoint): can't create external_postgr…
Browse files Browse the repository at this point in the history
…esql
  • Loading branch information
byashimov committed Mar 19, 2024
1 parent 1e083e6 commit 3e25910
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
8 changes: 2 additions & 6 deletions internal/schemautil/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
44 changes: 27 additions & 17 deletions internal/sdkprovider/userconfig/converters/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3e25910

Please sign in to comment.