Skip to content

Commit

Permalink
refactor: amend ResourceDataSet signature
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Feb 11, 2025
1 parent 73281a6 commit de50a50
Show file tree
Hide file tree
Showing 18 changed files with 23 additions and 39 deletions.
4 changes: 2 additions & 2 deletions internal/schemautil/schemautil.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,8 @@ var errMissingForceNew = fmt.Errorf("missing required ForceNew field")
//
// Use:
//
// err := ResourceDataSet(s, d, dto)
func ResourceDataSet(s map[string]*schema.Schema, d ResourceData, dto any, fns ...MapModifier) error {
// err := ResourceDataSet(d, dto, s)
func ResourceDataSet(d ResourceData, dto any, s map[string]*schema.Schema, fns ...MapModifier) error {
var m map[string]any
err := Remarshal(dto, &m)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/schemautil/schemautil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestResourceDataSetAddForceNew(t *testing.T) {

// Missing force_new field
d := mocks.NewMockResourceData(t)
err := ResourceDataSet(s, d, dto)
err := ResourceDataSet(d, dto, s)
require.Error(t, err, fmt.Errorf("%w: %q", errMissingForceNew, "force_new"))

// Expects to set all fields
Expand All @@ -121,7 +121,7 @@ func TestResourceDataSetAddForceNew(t *testing.T) {
d.EXPECT().Set("force_new", "hey!").Return(nil) // set force_new

err = ResourceDataSet(
s, d, dto,
d, dto, s,
AddForceNew("force_new", "hey!"),
RenameAlias("to_rename", "set_of_objects"),
)
Expand Down
4 changes: 1 addition & 3 deletions internal/sdkprovider/service/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ func resourceAccountRead(ctx context.Context, d *schema.ResourceData, client avn
}

if err = schemautil.ResourceDataSet(
aivenAccountSchema,
d,
resp,
d, resp, aivenAccountSchema,
schemautil.RenameAliases(map[string]string{
"account_name": "name",
"account_owner_team_id": "owner_team_id",
Expand Down
4 changes: 1 addition & 3 deletions internal/sdkprovider/service/account/account_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ func resourceAccountTeamRead(ctx context.Context, d *schema.ResourceData, client
}

if err = schemautil.ResourceDataSet(
aivenAccountTeamSchema,
d,
resp,
d, resp, aivenAccountTeamSchema,
schemautil.RenameAlias("team_name", "name"),
schemautil.AddForceNew("account_id", accountID),
); err != nil {
Expand Down
8 changes: 2 additions & 6 deletions internal/sdkprovider/service/account/account_team_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ func resourceAccountTeamMemberRead(ctx context.Context, d *schema.ResourceData,
for _, invite := range resp {
if invite.UserEmail == userEmail {
if err = schemautil.ResourceDataSet(
aivenAccountTeamMemberSchema,
d,
invite,
d, invite, aivenAccountTeamMemberSchema,
schemautil.AddForceNew("account_id", accountID),
); err != nil {
return err
Expand All @@ -142,9 +140,7 @@ func resourceAccountTeamMemberRead(ctx context.Context, d *schema.ResourceData,
for _, member := range respTI {
if member.UserEmail == userEmail {
if err = schemautil.ResourceDataSet(
aivenAccountTeamMemberSchema,
d,
member,
d, member, aivenAccountTeamMemberSchema,
schemautil.AddForceNew("account_id", accountID),
); err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func resourceAlloyDBOmniDatabaseRead(ctx context.Context, d *schema.ResourceData
}

return schemautil.ResourceDataSet(
aivenAlloyDBOmniDatabaseSchema, d, db,
d, db, aivenAlloyDBOmniDatabaseSchema,
schemautil.AddForceNew("project", projectName),
schemautil.AddForceNew("service_name", serviceName),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func flinkJarApplicationRead(ctx context.Context, d *schema.ResourceData, client
}

return schemautil.ResourceDataSet(
flinkJarApplicationSchema(), d, rsp,
d, rsp, flinkJarApplicationSchema(),
schemautil.RenameAliasesReverse(flinkJarApplicationRename()),
schemautil.AddForceNew("project", projectName),
schemautil.AddForceNew("service_name", serviceName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func flinkApplicationDeploymentRead(ctx context.Context, d *schema.ResourceData,
}

return schemautil.ResourceDataSet(
flinkJarApplicationDeploymentSchema(), d, rsp,
d, rsp, flinkJarApplicationDeploymentSchema(),
schemautil.RenameAliasesReverse(flinkJarApplicationDeploymentRename()),
schemautil.AddForceNew("project", projectName),
schemautil.AddForceNew("service_name", serviceName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func flinkJarApplicationVersionRead(ctx context.Context, d *schema.ResourceData,
}

return schemautil.ResourceDataSet(
flinkJarApplicationVersionSchema(), d, rsp,
d, rsp, flinkJarApplicationVersionSchema(),
schemautil.RenameAliasesReverse(flinkJarApplicationVersionRename()),
schemautil.AddForceNew("project", projectName),
schemautil.AddForceNew("service_name", serviceName),
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 @@ -105,7 +105,7 @@ func resourceKafkaNativeACLCreate(ctx context.Context, d *schema.ResourceData, c
}

err = schemautil.ResourceDataSet(
aivenKafkaNativeACLSchema, d, acl,
d, acl, aivenKafkaNativeACLSchema,
schemautil.AddForceNew("project", projectName),
schemautil.AddForceNew("service_name", serviceName),
)
Expand Down Expand Up @@ -134,7 +134,7 @@ func resourceKafkaNativeACLRead(ctx context.Context, d *schema.ResourceData, cli
}

err = schemautil.ResourceDataSet(
aivenKafkaNativeACLSchema, d, acl,
d, acl, aivenKafkaNativeACLSchema,
schemautil.AddForceNew("project", projectName),
schemautil.AddForceNew("service_name", serviceName),
)
Expand Down
4 changes: 1 addition & 3 deletions internal/sdkprovider/service/kafka/kafka_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ func resourceKafkaQuotaRead(ctx context.Context, d *schema.ResourceData, client
}

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

err = schemautil.ResourceDataSet(
aivenMirrorMakerReplicationFlowSchema, d, dto,
d, dto, aivenMirrorMakerReplicationFlowSchema,
schemautil.AddForceNew("project", project),
schemautil.AddForceNew("service_name", serviceName),
schemautil.RenameAliasesReverse(dtoFieldsAliases),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func resourceOrganizationApplicationUserRead(ctx context.Context, d *schema.Reso

// Sets name and user_id
err = schemautil.ResourceDataSet(
aivenOrganizationApplicationUserSchema, d, user,
d, user, aivenOrganizationApplicationUserSchema,
schemautil.RenameAlias("user_email", "email"),
schemautil.AddForceNew("organization_id", orgID),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func resourceOrganizationApplicationUserTokenCreate(ctx context.Context, d *sche
}

err = schemautil.ResourceDataSet(
aivenOrganizationApplicationUserTokenSchema, d, token,
d, token, aivenOrganizationApplicationUserTokenSchema,
schemautil.AddForceNew("organization_id", orgID),
schemautil.AddForceNew("user_id", userID),
)
Expand Down Expand Up @@ -178,7 +178,7 @@ func resourceOrganizationApplicationUserTokenRead(ctx context.Context, d *schema
}

err = schemautil.ResourceDataSet(
aivenOrganizationApplicationUserTokenSchema, d, token,
d, token, aivenOrganizationApplicationUserTokenSchema,
schemautil.AddForceNew("organization_id", orgID),
schemautil.AddForceNew("user_id", userID),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ func resourceOrganizationUserGroupRead(ctx context.Context, d *schema.ResourceDa
}

if err = schemautil.ResourceDataSet(
aivenOrganizationUserGroupSchema,
d,
resp,
d, resp, aivenOrganizationUserGroupSchema,
schemautil.RenameAlias("user_group_name", "name"),
schemautil.RenameAlias("user_group_id", "group_id"),
schemautil.AddForceNew("organization_id", orgID),
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, datasourceOrganizationUserListSchema())
}

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,11 +109,7 @@ func resourceOrganizationalUnitRead(ctx context.Context, d *schema.ResourceData,
}
}

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

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 @@ -191,7 +191,7 @@ func ResourcePGUserRead(ctx context.Context, d schemautil.ResourceData, client a
}

err = schemautil.ResourceDataSet(
ResourcePGUserSchema, d, user,
d, user, ResourcePGUserSchema,
schemautil.AddForceNew("project", projectName),
schemautil.AddForceNew("service_name", serviceName),
)
Expand Down

0 comments on commit de50a50

Please sign in to comment.