Skip to content

Commit

Permalink
refactor: ResourceDataSet should not take the schema
Browse files Browse the repository at this point in the history
Resources like service users can't share the code,
when the schema is mandatory.
  • Loading branch information
byashimov committed Jan 10, 2025
1 parent cc0445d commit 86f603e
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 40 deletions.
53 changes: 38 additions & 15 deletions internal/schemautil/schemautil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aiven/aiven-go-client/v2"
"github.com/aiven/go-client-codegen/handler/service"
"github.com/docker/go-units"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -344,7 +345,12 @@ func CopyServiceUserGenPropertiesFromAPIResponseToTerraform(
//
// Warning: doesn't support nested sets.
// Warning: not tested with nested objects.
func ResourceDataGet(d *schema.ResourceData, dto any, fns ...KVModifier) error {
func ResourceDataGet(d *schema.ResourceData, dto any, fns ...KVModifier) (err error) {
defer func() {
// cty.Value might panic
err = recoverPanic("failed to get resource data")
}()

rawConfig := d.GetRawConfig()
if rawConfig.IsNull() {
return nil
Expand Down Expand Up @@ -408,10 +414,15 @@ type KVModifier func(k string, v any) (string, any)
//
// Use:
//
// err := ResourceDataSet(s, d, dto)
func ResourceDataSet(s map[string]*schema.Schema, d *schema.ResourceData, dto any, fns ...KVModifier) error {
// err := ResourceDataSet(d, dto)
func ResourceDataSet(d *schema.ResourceData, dto any, fns ...KVModifier) (err error) {
defer func() {
// cty.Value might panic
err = recoverPanic("failed to set resource data")
}()

var m map[string]any
err := Remarshal(dto, &m)
err = Remarshal(dto, &m)
if err != nil {
return err
}
Expand All @@ -424,8 +435,9 @@ func ResourceDataSet(s map[string]*schema.Schema, d *schema.ResourceData, dto an
}
}

m = serializeSet(s, m)
for k := range s {
c := d.GetRawConfig()
m = serializeSet(c, m)
for k := range c.AsValueMap() {
if v, ok := m[k]; ok {
if err = d.Set(k, v); err != nil {
return err
Expand All @@ -435,28 +447,27 @@ func ResourceDataSet(s map[string]*schema.Schema, d *schema.ResourceData, dto an
return nil
}

func serializeSet(s map[string]*schema.Schema, m map[string]any) map[string]any {
for k, prop := range s {
func serializeSet(c cty.Value, m map[string]any) map[string]any {
for k, prop := range c.AsValueMap() {
value, ok := m[k]
if !ok {
continue
}

res, ok := prop.Elem.(*schema.Resource)
if !ok {
continue
}

// When we have an object, we need to convert it to a list.
// So there is no difference between a single object and a list of objects.
var items []any
switch element := value.(type) {
case map[string]any:
items = append(items, serializeSet(res.Schema, element))
items = append(items, serializeSet(prop, element))
case []any:
for _, v := range element {
items = append(items, serializeSet(res.Schema, v.(map[string]any)))
items = append(items, serializeSet(prop, v.(map[string]any)))
}
default:
// Scalar types
m[k] = value
continue
}

m[k] = items
Expand Down Expand Up @@ -503,3 +514,15 @@ func Remarshal(in, out any) error {
}
return json.Unmarshal(b, out)
}

func recoverPanic(msg string) error {
r := recover()
switch v := r.(type) {
case nil:
return nil
case error:
return fmt.Errorf("%s: %w", msg, v)
default:
return fmt.Errorf("%s: %v", msg, v)
}
}
1 change: 0 additions & 1 deletion internal/sdkprovider/service/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func resourceAccountRead(ctx context.Context, d *schema.ResourceData, client avn
}

if err = schemautil.ResourceDataSet(
aivenAccountSchema,
d,
resp,
schemautil.RenameAliases(map[string]string{
Expand Down
1 change: 0 additions & 1 deletion internal/sdkprovider/service/account/account_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func resourceAccountTeamRead(ctx context.Context, d *schema.ResourceData, client
}

if err = schemautil.ResourceDataSet(
aivenAccountTeamSchema,
d,
resp,
schemautil.RenameAlias("team_name", "name"),
Expand Down
2 changes: 0 additions & 2 deletions internal/sdkprovider/service/account/account_team_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func resourceAccountTeamMemberRead(ctx context.Context, d *schema.ResourceData,
for _, invite := range resp {
if invite.UserEmail == userEmail {
if err = schemautil.ResourceDataSet(
aivenAccountTeamMemberSchema,
d,
invite,
schemautil.RenameAliases(map[string]string{}),
Expand Down Expand Up @@ -146,7 +145,6 @@ func resourceAccountTeamMemberRead(ctx context.Context, d *schema.ResourceData,
for _, member := range respTI {
if member.UserEmail == userEmail {
if err = schemautil.ResourceDataSet(
aivenAccountTeamMemberSchema,
d,
member,
); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func resourceAlloyDBOmniDatabaseRead(ctx context.Context, d *schema.ResourceData
return err
}

return schemautil.ResourceDataSet(aivenAlloyDBOmniDatabaseSchema, d, db)
return schemautil.ResourceDataSet(d, db)
}

func resourceAlloyDBOmniDatabaseDelete(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func resourceAlloyDBOmniUserRead(ctx context.Context, d *schema.ResourceData, cl
return schemautil.ResourceReadHandleNotFound(err, d)
}

err = schemautil.ResourceDataSet(aivenAlloyDBOmniUserSchema, d, user)
err = schemautil.ResourceDataSet(d, user)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/sdkprovider/service/kafka/kafka_native_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func resourceKafkaNativeACLCreate(ctx context.Context, d *schema.ResourceData, c
return err
}

err = schemautil.ResourceDataSet(aivenKafkaNativeACLSchema, d, acl)
err = schemautil.ResourceDataSet(d, acl)
if err != nil {
return err
}
Expand All @@ -129,7 +129,7 @@ func resourceKafkaNativeACLRead(ctx context.Context, d *schema.ResourceData, cli
return err
}

err = schemautil.ResourceDataSet(aivenKafkaNativeACLSchema, d, acl)
err = schemautil.ResourceDataSet(d, acl)
return err
}

Expand Down
1 change: 0 additions & 1 deletion internal/sdkprovider/service/kafka/kafka_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ func resourceKafkaQuotaRead(ctx context.Context, d *schema.ResourceData, client
}

return schemautil.ResourceDataSet(
aivenKafkaQuotaSchema,
d,
resp,
schemautil.RenameAliasesReverse(quotaFieldsAliases),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func resourceMirrorMakerReplicationFlowRead(ctx context.Context, d *schema.Resou
}

err = schemautil.ResourceDataSet(
aivenMirrorMakerReplicationFlowSchema, d, dto, schemautil.RenameAliasesReverse(dtoFieldsAliases),
d, dto, schemautil.RenameAliasesReverse(dtoFieldsAliases),
func(k string, v any) (string, any) {
if k == configPropsKey {
// This field is received as a string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func resourceOrganizationApplicationUserRead(ctx context.Context, d *schema.Reso
}

// Sets name and user_id
err = schemautil.ResourceDataSet(aivenOrganizationApplicationUserSchema, d, user)
err = schemautil.ResourceDataSet(d, user)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func resourceOrganizationApplicationUserTokenCreate(ctx context.Context, d *sche
return err
}

err = schemautil.ResourceDataSet(aivenOrganizationApplicationUserTokenSchema, d, token)
err = schemautil.ResourceDataSet(d, token)
if err != nil {
return err
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func resourceOrganizationApplicationUserTokenRead(ctx context.Context, d *schema
return fmt.Errorf("application user token not found")
}

err = schemautil.ResourceDataSet(aivenOrganizationApplicationUserTokenSchema, d, token)
err = schemautil.ResourceDataSet(d, token)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func resourceOrganizationUserGroupRead(ctx context.Context, d *schema.ResourceDa
}

if err = schemautil.ResourceDataSet(
aivenOrganizationUserGroupSchema,
d,
resp,
schemautil.RenameAliases(map[string]string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func datasourceOrganizationUserListRead(ctx context.Context, d *schema.ResourceD

d.SetId(organizationID)
users := map[string]any{"users": list}
return schemautil.ResourceDataSet(datasourceOrganizationUserListSchema(), d, users)
return schemautil.ResourceDataSet(d, users)
}

func GetOrganizationByName(ctx context.Context, client avngen.Client, name string) (string, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,7 @@ func resourceOrganizationalUnitRead(ctx context.Context, d *schema.ResourceData,
}
}

if err = schemautil.ResourceDataSet(
aivenOrganizationalUnitSchema,
d,
resp,
); err != nil {
return err
}

return nil
return schemautil.ResourceDataSet(d, resp)
}

func determineMixedOrganizationConstraintIDToStore(
Expand Down
2 changes: 1 addition & 1 deletion internal/sdkprovider/service/pg/pg_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func ReadResourcePGUser(ctx context.Context, d *schema.ResourceData, client avng
return schemautil.ResourceReadHandleNotFound(err, d)
}

err = schemautil.ResourceDataSet(SchemaResourcePGUser, d, user)
err = schemautil.ResourceDataSet(d, user)
if err != nil {
return err
}
Expand Down

0 comments on commit 86f603e

Please sign in to comment.