diff --git a/coderd/coderdtest/coderdtest.go b/coderd/coderdtest/coderdtest.go index ed56cbb39a..29ab950b23 100644 --- a/coderd/coderdtest/coderdtest.go +++ b/coderd/coderdtest/coderdtest.go @@ -62,7 +62,6 @@ import ( "github.com/coder/coder/v2/coderd/connectionlog" "github.com/coder/coder/v2/coderd/cryptokeys" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/database/dbrollup" "github.com/coder/coder/v2/coderd/database/dbtestutil" @@ -86,6 +85,7 @@ import ( "github.com/coder/coder/v2/coderd/usage" "github.com/coder/coder/v2/coderd/util/namesgenerator" "github.com/coder/coder/v2/coderd/util/ptr" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/coderd/webpush" "github.com/coder/coder/v2/coderd/workspaceapps" "github.com/coder/coder/v2/coderd/workspaceapps/appurl" @@ -934,7 +934,7 @@ func createAnotherUserRetry(t testing.TB, client *codersdk.Client, organizationI return role.Name } - user, err = client.UpdateUserRoles(context.Background(), user.ID.String(), codersdk.UpdateRoles{Roles: db2sdk.List(siteRoles, onlyName)}) + user, err = client.UpdateUserRoles(context.Background(), user.ID.String(), codersdk.UpdateRoles{Roles: slice.List(siteRoles, onlyName)}) require.NoError(t, err, "update site roles") // isMember keeps track of which orgs the user was added to as a member @@ -953,7 +953,7 @@ func createAnotherUserRetry(t testing.TB, client *codersdk.Client, organizationI } _, err = client.UpdateOrganizationMemberRoles(context.Background(), orgID, user.ID.String(), - codersdk.UpdateRoles{Roles: db2sdk.List(roles, onlyName)}) + codersdk.UpdateRoles{Roles: slice.List(roles, onlyName)}) require.NoError(t, err, "update org membership roles") isMember[orgID] = true } diff --git a/coderd/database/db2sdk/db2sdk.go b/coderd/database/db2sdk/db2sdk.go index 0696dfb51c..0e28304ccf 100644 --- a/coderd/database/db2sdk/db2sdk.go +++ b/coderd/database/db2sdk/db2sdk.go @@ -31,16 +31,6 @@ import ( previewtypes "github.com/coder/preview/types" ) -// Deprecated: use slice.List -func List[F any, T any](list []F, convert func(F) T) []T { - return slice.List[F, T](list, convert) -} - -// Deprecated: use slice.ListLazy -func ListLazy[F any, T any](convert func(F) T) func(list []F) []T { - return slice.ListLazy[F, T](convert) -} - func APIAllowListTarget(entry rbac.AllowListElement) codersdk.APIAllowListTarget { return codersdk.APIAllowListTarget{ Type: codersdk.RBACResource(entry.Type), @@ -81,7 +71,7 @@ func WorkspaceBuildParameter(p database.WorkspaceBuildParameter) codersdk.Worksp } func WorkspaceBuildParameters(params []database.WorkspaceBuildParameter) []codersdk.WorkspaceBuildParameter { - return List(params, WorkspaceBuildParameter) + return slice.List(params, WorkspaceBuildParameter) } func TemplateVersionParameters(params []database.TemplateVersionParameter) ([]codersdk.TemplateVersionParameter, error) { @@ -115,7 +105,7 @@ func TemplateVersionParameterFromPreview(param previewtypes.Parameter) (codersdk Icon: param.Icon, Required: param.Required, Ephemeral: param.Ephemeral, - Options: List(param.Options, TemplateVersionParameterOptionFromPreview), + Options: slice.List(param.Options, TemplateVersionParameterOptionFromPreview), // Validation set after } if len(param.Validations) > 0 { @@ -237,11 +227,11 @@ func ReducedUserFromGroupMember(member database.GroupMember) codersdk.ReducedUse } func ReducedUsersFromGroupMembers(members []database.GroupMember) []codersdk.ReducedUser { - return List(members, ReducedUserFromGroupMember) + return slice.List(members, ReducedUserFromGroupMember) } func ReducedUsers(users []database.User) []codersdk.ReducedUser { - return List(users, ReducedUser) + return slice.List(users, ReducedUser) } func User(user database.User, organizationIDs []uuid.UUID) codersdk.User { @@ -255,7 +245,7 @@ func User(user database.User, organizationIDs []uuid.UUID) codersdk.User { } func Users(users []database.User, organizationIDs map[uuid.UUID][]uuid.UUID) []codersdk.User { - return List(users, func(user database.User) codersdk.User { + return slice.List(users, func(user database.User) codersdk.User { return User(user, organizationIDs[user.ID]) }) } @@ -388,7 +378,7 @@ func OAuth2ProviderApp(accessURL *url.URL, dbApp database.OAuth2ProviderApp) cod } func OAuth2ProviderApps(accessURL *url.URL, dbApps []database.OAuth2ProviderApp) []codersdk.OAuth2ProviderApp { - return List(dbApps, func(dbApp database.OAuth2ProviderApp) codersdk.OAuth2ProviderApp { + return slice.List(dbApps, func(dbApp database.OAuth2ProviderApp) codersdk.OAuth2ProviderApp { return OAuth2ProviderApp(accessURL, dbApp) }) } @@ -607,7 +597,7 @@ func Apps(dbApps []database.WorkspaceApp, statuses []database.WorkspaceAppStatus } func WorkspaceAppStatuses(statuses []database.WorkspaceAppStatus) []codersdk.WorkspaceAppStatus { - return List(statuses, WorkspaceAppStatus) + return slice.List(statuses, WorkspaceAppStatus) } func WorkspaceAppStatus(status database.WorkspaceAppStatus) codersdk.WorkspaceAppStatus { @@ -728,10 +718,10 @@ func RBACRole(role rbac.Role) codersdk.Role { Name: slim.Name, OrganizationID: slim.OrganizationID, DisplayName: slim.DisplayName, - SitePermissions: List(role.Site, RBACPermission), - UserPermissions: List(role.User, RBACPermission), - OrganizationPermissions: List(orgPerms.Org, RBACPermission), - OrganizationMemberPermissions: List(orgPerms.Member, RBACPermission), + SitePermissions: slice.List(role.Site, RBACPermission), + UserPermissions: slice.List(role.User, RBACPermission), + OrganizationPermissions: slice.List(orgPerms.Org, RBACPermission), + OrganizationMemberPermissions: slice.List(orgPerms.Member, RBACPermission), } } @@ -745,9 +735,9 @@ func Role(role database.CustomRole) codersdk.Role { Name: role.Name, OrganizationID: orgID, DisplayName: role.DisplayName, - SitePermissions: List(role.SitePermissions, Permission), - UserPermissions: List(role.UserPermissions, Permission), - OrganizationPermissions: List(role.OrgPermissions, Permission), + SitePermissions: slice.List(role.SitePermissions, Permission), + UserPermissions: slice.List(role.UserPermissions, Permission), + OrganizationPermissions: slice.List(role.OrgPermissions, Permission), } } @@ -783,7 +773,7 @@ func Organization(organization database.Organization) codersdk.Organization { } func CryptoKeys(keys []database.CryptoKey) []codersdk.CryptoKey { - return List(keys, CryptoKey) + return slice.List(keys, CryptoKey) } func CryptoKey(key database.CryptoKey) codersdk.CryptoKey { @@ -894,8 +884,8 @@ func PreviewParameter(param previewtypes.Parameter) codersdk.PreviewParameter { Mutable: param.Mutable, DefaultValue: PreviewHCLString(param.DefaultValue), Icon: param.Icon, - Options: List(param.Options, PreviewParameterOption), - Validations: List(param.Validations, PreviewParameterValidation), + Options: slice.List(param.Options, PreviewParameterOption), + Validations: slice.List(param.Validations, PreviewParameterValidation), Required: param.Required, Order: param.Order, Ephemeral: param.Ephemeral, @@ -911,7 +901,7 @@ func HCLDiagnostics(d hcl.Diagnostics) []codersdk.FriendlyDiagnostic { func PreviewDiagnostics(d previewtypes.Diagnostics) []codersdk.FriendlyDiagnostic { f := d.FriendlyDiagnostics() - return List(f, func(f previewtypes.FriendlyDiagnostic) codersdk.FriendlyDiagnostic { + return slice.List(f, func(f previewtypes.FriendlyDiagnostic) codersdk.FriendlyDiagnostic { return codersdk.FriendlyDiagnostic{ Severity: codersdk.DiagnosticSeverityString(f.Severity), Summary: f.Summary, @@ -959,17 +949,17 @@ func PreviewParameterValidation(v *previewtypes.ParameterValidation) codersdk.Pr } func AIBridgeInterception(interception database.AIBridgeInterception, initiator database.VisibleUser, tokenUsages []database.AIBridgeTokenUsage, userPrompts []database.AIBridgeUserPrompt, toolUsages []database.AIBridgeToolUsage) codersdk.AIBridgeInterception { - sdkTokenUsages := List(tokenUsages, AIBridgeTokenUsage) + sdkTokenUsages := slice.List(tokenUsages, AIBridgeTokenUsage) sort.Slice(sdkTokenUsages, func(i, j int) bool { // created_at ASC return sdkTokenUsages[i].CreatedAt.Before(sdkTokenUsages[j].CreatedAt) }) - sdkUserPrompts := List(userPrompts, AIBridgeUserPrompt) + sdkUserPrompts := slice.List(userPrompts, AIBridgeUserPrompt) sort.Slice(sdkUserPrompts, func(i, j int) bool { // created_at ASC return sdkUserPrompts[i].CreatedAt.Before(sdkUserPrompts[j].CreatedAt) }) - sdkToolUsages := List(toolUsages, AIBridgeToolUsage) + sdkToolUsages := slice.List(toolUsages, AIBridgeToolUsage) sort.Slice(sdkToolUsages, func(i, j int) bool { // created_at ASC return sdkToolUsages[i].CreatedAt.Before(sdkToolUsages[j].CreatedAt) diff --git a/coderd/database/dbauthz/customroles_test.go b/coderd/database/dbauthz/customroles_test.go index 790541f47e..b848065b76 100644 --- a/coderd/database/dbauthz/customroles_test.go +++ b/coderd/database/dbauthz/customroles_test.go @@ -10,11 +10,11 @@ import ( "cdr.dev/slog/v3" "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/database/dbtestutil" "github.com/coder/coder/v2/coderd/rbac" "github.com/coder/coder/v2/coderd/rbac/policy" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/testutil" ) @@ -227,10 +227,10 @@ func TestInsertCustomRoles(t *testing.T) { Name: "test-role", DisplayName: "", OrganizationID: uuid.NullUUID{UUID: tc.organizationID, Valid: true}, - SitePermissions: db2sdk.List(tc.site, convertSDKPerm), - OrgPermissions: db2sdk.List(tc.org, convertSDKPerm), - UserPermissions: db2sdk.List(tc.user, convertSDKPerm), - MemberPermissions: db2sdk.List(tc.member, convertSDKPerm), + SitePermissions: slice.List(tc.site, convertSDKPerm), + OrgPermissions: slice.List(tc.org, convertSDKPerm), + UserPermissions: slice.List(tc.user, convertSDKPerm), + MemberPermissions: slice.List(tc.member, convertSDKPerm), }) if tc.errorContains != "" { require.ErrorContains(t, err, tc.errorContains) diff --git a/coderd/database/dbauthz/dbauthz_test.go b/coderd/database/dbauthz/dbauthz_test.go index 7764f69b7d..9b9140e998 100644 --- a/coderd/database/dbauthz/dbauthz_test.go +++ b/coderd/database/dbauthz/dbauthz_test.go @@ -22,7 +22,6 @@ import ( "cdr.dev/slog/v3/sloggers/slogtest" "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/database/dbgen" "github.com/coder/coder/v2/coderd/database/dbmock" @@ -1630,11 +1629,11 @@ func (s *MethodTestSuite) TestUser() { Name: "", OrganizationID: uuid.NullUUID{UUID: uuid.Nil, Valid: false}, DisplayName: "Test Name", - SitePermissions: db2sdk.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ + SitePermissions: slice.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ codersdk.ResourceTemplate: {codersdk.ActionCreate, codersdk.ActionRead, codersdk.ActionUpdate, codersdk.ActionDelete, codersdk.ActionViewInsights}, }), convertSDKPerm), OrgPermissions: nil, - UserPermissions: db2sdk.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ + UserPermissions: slice.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ codersdk.ResourceWorkspace: {codersdk.ActionRead}, }), convertSDKPerm), } @@ -1646,7 +1645,7 @@ func (s *MethodTestSuite) TestUser() { Name: "name", DisplayName: "Test Name", OrganizationID: uuid.NullUUID{UUID: orgID, Valid: true}, - OrgPermissions: db2sdk.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ + OrgPermissions: slice.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ codersdk.ResourceTemplate: {codersdk.ActionCreate, codersdk.ActionRead}, }), convertSDKPerm), } @@ -1668,11 +1667,11 @@ func (s *MethodTestSuite) TestUser() { arg := database.InsertCustomRoleParams{ Name: "test", DisplayName: "Test Name", - SitePermissions: db2sdk.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ + SitePermissions: slice.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ codersdk.ResourceTemplate: {codersdk.ActionCreate, codersdk.ActionRead, codersdk.ActionUpdate, codersdk.ActionDelete, codersdk.ActionViewInsights}, }), convertSDKPerm), OrgPermissions: nil, - UserPermissions: db2sdk.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ + UserPermissions: slice.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ codersdk.ResourceWorkspace: {codersdk.ActionRead}, }), convertSDKPerm), } @@ -1684,7 +1683,7 @@ func (s *MethodTestSuite) TestUser() { Name: "test", DisplayName: "Test Name", OrganizationID: uuid.NullUUID{UUID: orgID, Valid: true}, - OrgPermissions: db2sdk.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ + OrgPermissions: slice.List(codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ codersdk.ResourceTemplate: {codersdk.ActionCreate, codersdk.ActionRead}, }), convertSDKPerm), } diff --git a/coderd/database/querier_test.go b/coderd/database/querier_test.go index 1f53a4ced7..e9ec935f84 100644 --- a/coderd/database/querier_test.go +++ b/coderd/database/querier_test.go @@ -23,7 +23,6 @@ import ( "cdr.dev/slog/v3/sloggers/slogtest" "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/database/dbfake" "github.com/coder/coder/v2/coderd/database/dbgen" @@ -2022,8 +2021,8 @@ func TestWorkspaceQuotas(t *testing.T) { }) require.NoError(t, err) - require.ElementsMatch(t, db2sdk.List(everyoneMembers, groupMemberIDs), - db2sdk.List([]database.OrganizationMember{memOne, memTwo}, orgMemberIDs)) + require.ElementsMatch(t, slice.List(everyoneMembers, groupMemberIDs), + slice.List([]database.OrganizationMember{memOne, memTwo}, orgMemberIDs)) // Check the quota is correct. allowance, err := db.GetQuotaAllowanceForUser(ctx, database.GetQuotaAllowanceForUserParams{ @@ -2204,7 +2203,7 @@ func TestReadCustomRoles(t *testing.T) { { Name: "AllRolesByLookup", Params: database.CustomRolesParams{ - LookupRoles: db2sdk.List(allRoles, roleToLookup), + LookupRoles: slice.List(allRoles, roleToLookup), }, Match: func(role database.CustomRole) bool { return true @@ -2270,8 +2269,8 @@ func TestReadCustomRoles(t *testing.T) { } } - a := db2sdk.List(filtered, normalizedRoleName) - b := db2sdk.List(found, normalizedRoleName) + a := slice.List(filtered, normalizedRoleName) + b := slice.List(found, normalizedRoleName) require.Equal(t, a, b) }) } @@ -4260,7 +4259,7 @@ func TestGroupRemovalTrigger(t *testing.T) { require.ElementsMatch(t, []uuid.UUID{ orgA.ID, orgB.ID, // Everyone groups groupA1.ID, groupA2.ID, groupB1.ID, groupB2.ID, // Org groups - }, db2sdk.List(userGroups, onlyGroupIDs)) + }, slice.List(userGroups, onlyGroupIDs)) // Remove the user from org A err = db.DeleteOrganizationMember(ctx, database.DeleteOrganizationMemberParams{ @@ -4277,7 +4276,7 @@ func TestGroupRemovalTrigger(t *testing.T) { require.ElementsMatch(t, []uuid.UUID{ orgB.ID, // Everyone group groupB1.ID, groupB2.ID, // Org groups - }, db2sdk.List(userGroups, onlyGroupIDs)) + }, slice.List(userGroups, onlyGroupIDs)) // Verify extra user is unchanged extraUserGroups, err := db.GetGroups(ctx, database.GetGroupsParams{ @@ -4287,7 +4286,7 @@ func TestGroupRemovalTrigger(t *testing.T) { require.ElementsMatch(t, []uuid.UUID{ orgA.ID, orgB.ID, // Everyone groups groupA1.ID, groupA2.ID, groupB1.ID, groupB2.ID, // Org groups - }, db2sdk.List(extraUserGroups, onlyGroupIDs)) + }, slice.List(extraUserGroups, onlyGroupIDs)) } func TestGetUserStatusCounts(t *testing.T) { diff --git a/coderd/database/sdk2db/sdk2db.go b/coderd/database/sdk2db/sdk2db.go index 02fe857817..ee9066b444 100644 --- a/coderd/database/sdk2db/sdk2db.go +++ b/coderd/database/sdk2db/sdk2db.go @@ -3,7 +3,7 @@ package sdk2db import ( "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" ) @@ -12,5 +12,5 @@ func ProvisionerDaemonStatus(status codersdk.ProvisionerDaemonStatus) database.P } func ProvisionerDaemonStatuses(params []codersdk.ProvisionerDaemonStatus) []database.ProvisionerDaemonStatus { - return db2sdk.List(params, ProvisionerDaemonStatus) + return slice.List(params, ProvisionerDaemonStatus) } diff --git a/coderd/dynamicparameters/static.go b/coderd/dynamicparameters/static.go index fec5de2581..46682d3378 100644 --- a/coderd/dynamicparameters/static.go +++ b/coderd/dynamicparameters/static.go @@ -9,8 +9,8 @@ import ( "golang.org/x/xerrors" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/util/ptr" + "github.com/coder/coder/v2/coderd/util/slice" sdkproto "github.com/coder/coder/v2/provisionersdk/proto" "github.com/coder/preview" previewtypes "github.com/coder/preview/types" @@ -27,7 +27,7 @@ func (r *loader) staticRender(ctx context.Context, db database.Store) (*staticRe return nil, xerrors.Errorf("template version parameters: %w", err) } - params := db2sdk.List(dbTemplateVersionParameters, TemplateVersionParameter) + params := slice.List(dbTemplateVersionParameters, TemplateVersionParameter) for i, param := range params { // Update the diagnostics to validate the 'default' value. diff --git a/coderd/idpsync/group.go b/coderd/idpsync/group.go index f8875b9d17..ec82a021ae 100644 --- a/coderd/idpsync/group.go +++ b/coderd/idpsync/group.go @@ -12,7 +12,6 @@ import ( "cdr.dev/slog/v3" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/runtimeconfig" "github.com/coder/coder/v2/coderd/util/ptr" @@ -202,7 +201,7 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat // determine if we have to do any group updates to sync the user's // state. existingGroups := userOrgs[orgID] - existingGroupsTyped := db2sdk.List(existingGroups, func(f database.GetGroupsRow) ExpectedGroup { + existingGroupsTyped := slice.List(existingGroups, func(f database.GetGroupsRow) ExpectedGroup { return ExpectedGroup{ OrganizationID: orgID, GroupID: &f.Group.ID, diff --git a/coderd/idpsync/group_test.go b/coderd/idpsync/group_test.go index d9c6ddbc01..cefa07d055 100644 --- a/coderd/idpsync/group_test.go +++ b/coderd/idpsync/group_test.go @@ -15,13 +15,13 @@ import ( "cdr.dev/slog/v3/sloggers/slogtest" "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/database/dbgen" "github.com/coder/coder/v2/coderd/database/dbtestutil" "github.com/coder/coder/v2/coderd/idpsync" "github.com/coder/coder/v2/coderd/runtimeconfig" "github.com/coder/coder/v2/coderd/util/ptr" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/testutil" ) @@ -590,7 +590,7 @@ func TestApplyGroupDifference(t *testing.T) { require.NoError(t, err) // assert - found := db2sdk.List(userGroups, func(g database.GetGroupsRow) uuid.UUID { + found := slice.List(userGroups, func(g database.GetGroupsRow) uuid.UUID { return g.Group.ID }) @@ -910,14 +910,14 @@ func (o *orgGroupAssert) Assert(t *testing.T, orgID uuid.UUID, db database.Store }) if len(o.ExpectedGroupNames) > 0 { - found := db2sdk.List(userGroups, func(g database.GetGroupsRow) string { + found := slice.List(userGroups, func(g database.GetGroupsRow) string { return g.Group.Name }) require.ElementsMatch(t, o.ExpectedGroupNames, found, "user groups by name") require.Len(t, o.ExpectedGroups, 0, "ExpectedGroups should be empty") } else { // Check by ID, recommended - found := db2sdk.List(userGroups, func(g database.GetGroupsRow) uuid.UUID { + found := slice.List(userGroups, func(g database.GetGroupsRow) uuid.UUID { return g.Group.ID }) require.ElementsMatch(t, o.ExpectedGroups, found, "user groups") diff --git a/coderd/idpsync/organization.go b/coderd/idpsync/organization.go index 18d18dfd64..c83c1b8911 100644 --- a/coderd/idpsync/organization.go +++ b/coderd/idpsync/organization.go @@ -11,7 +11,6 @@ import ( "cdr.dev/slog/v3" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/database/dbtime" "github.com/coder/coder/v2/coderd/runtimeconfig" @@ -107,7 +106,7 @@ func (s AGPLIDPSync) SyncOrganizations(ctx context.Context, tx database.Store, u return xerrors.Errorf("failed to get user organizations: %w", err) } - existingOrgIDs := db2sdk.List(existingOrgs, func(org database.Organization) uuid.UUID { + existingOrgIDs := slice.List(existingOrgs, func(org database.Organization) uuid.UUID { return org.ID }) @@ -127,7 +126,7 @@ func (s AGPLIDPSync) SyncOrganizations(ctx context.Context, tx database.Store, u if err != nil { return xerrors.Errorf("failed to get expected organizations: %w", err) } - finalExpected = db2sdk.List(expectedOrganizations, func(org database.Organization) uuid.UUID { + finalExpected = slice.List(expectedOrganizations, func(org database.Organization) uuid.UUID { return org.ID }) } diff --git a/coderd/idpsync/organizations_test.go b/coderd/idpsync/organizations_test.go index 02f3768585..5054dc988f 100644 --- a/coderd/idpsync/organizations_test.go +++ b/coderd/idpsync/organizations_test.go @@ -11,12 +11,12 @@ import ( "cdr.dev/slog/v3/sloggers/slogtest" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbfake" "github.com/coder/coder/v2/coderd/database/dbgen" "github.com/coder/coder/v2/coderd/database/dbtestutil" "github.com/coder/coder/v2/coderd/idpsync" "github.com/coder/coder/v2/coderd/runtimeconfig" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/testutil" ) @@ -173,7 +173,7 @@ func TestSyncOrganizations(t *testing.T) { // Verify the user only exists in 2 orgs. The one they stayed, and the one they // joined. - inIDs := db2sdk.List(orgs, func(org database.Organization) uuid.UUID { + inIDs := slice.List(orgs, func(org database.Organization) uuid.UUID { return org.ID }) require.ElementsMatch(t, []uuid.UUID{stays.Org.ID, joins.Org.ID}, inIDs) diff --git a/coderd/members.go b/coderd/members.go index c27245f98a..a98ba9c6fc 100644 --- a/coderd/members.go +++ b/coderd/members.go @@ -17,6 +17,7 @@ import ( "github.com/coder/coder/v2/coderd/httpmw" "github.com/coder/coder/v2/coderd/rbac" "github.com/coder/coder/v2/coderd/searchquery" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" ) @@ -370,7 +371,7 @@ func convertOrganizationMembers(ctx context.Context, db database.Store, mems []d OrganizationID: m.OrganizationID, CreatedAt: m.CreatedAt, UpdatedAt: m.UpdatedAt, - Roles: db2sdk.List(m.Roles, func(r string) codersdk.SlimRole { + Roles: slice.List(m.Roles, func(r string) codersdk.SlimRole { // If it is a built-in role, no lookups are needed. rbacRole, err := rbac.RoleByName(rbac.RoleIdentifier{Name: r, OrganizationID: m.OrganizationID}) if err == nil { diff --git a/coderd/members_test.go b/coderd/members_test.go index 8cfb8be30a..17612185cc 100644 --- a/coderd/members_test.go +++ b/coderd/members_test.go @@ -9,8 +9,8 @@ import ( "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbgen" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/testutil" ) @@ -76,7 +76,7 @@ func TestListMembers(t *testing.T) { require.Len(t, members, 3) require.ElementsMatch(t, []uuid.UUID{owner.UserID, orgMember.ID, orgAdmin.ID}, - db2sdk.List(members, onlyIDs)) + slice.List(members, onlyIDs)) }) t.Run("UserID", func(t *testing.T) { @@ -88,7 +88,7 @@ func TestListMembers(t *testing.T) { require.Len(t, members, 1) require.ElementsMatch(t, []uuid.UUID{orgMember.ID}, - db2sdk.List(members, onlyIDs)) + slice.List(members, onlyIDs)) }) t.Run("IncludeSystem", func(t *testing.T) { @@ -100,7 +100,7 @@ func TestListMembers(t *testing.T) { require.Len(t, members, 4) require.ElementsMatch(t, []uuid.UUID{owner.UserID, orgMember.ID, orgAdmin.ID, database.PrebuildsSystemUserID}, - db2sdk.List(members, onlyIDs)) + slice.List(members, onlyIDs)) }) t.Run("GithubUserID", func(t *testing.T) { @@ -112,7 +112,7 @@ func TestListMembers(t *testing.T) { require.Len(t, members, 1) require.ElementsMatch(t, []uuid.UUID{anotherUser.ID}, - db2sdk.List(members, onlyIDs)) + slice.List(members, onlyIDs)) }) } diff --git a/coderd/organizations.go b/coderd/organizations.go index 5f05099507..fb3b18a83f 100644 --- a/coderd/organizations.go +++ b/coderd/organizations.go @@ -7,6 +7,7 @@ import ( "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/httpapi" "github.com/coder/coder/v2/coderd/httpmw" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" ) @@ -32,7 +33,7 @@ func (api *API) organizations(rw http.ResponseWriter, r *http.Request) { return } - httpapi.Write(ctx, rw, http.StatusOK, db2sdk.List(organizations, db2sdk.Organization)) + httpapi.Write(ctx, rw, http.StatusOK, slice.List(organizations, db2sdk.Organization)) } // @Summary Get organization by ID diff --git a/coderd/parameters.go b/coderd/parameters.go index 08d571267a..00a0e0369c 100644 --- a/coderd/parameters.go +++ b/coderd/parameters.go @@ -12,6 +12,7 @@ import ( "github.com/coder/coder/v2/coderd/dynamicparameters" "github.com/coder/coder/v2/coderd/httpapi" "github.com/coder/coder/v2/coderd/httpmw" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/codersdk/wsjson" "github.com/coder/websocket" @@ -121,7 +122,7 @@ func (*API) handleParameterEvaluate(rw http.ResponseWriter, r *http.Request, ini Diagnostics: db2sdk.HCLDiagnostics(diagnostics), } if result != nil { - response.Parameters = db2sdk.List(result.Parameters, db2sdk.PreviewParameter) + response.Parameters = slice.List(result.Parameters, db2sdk.PreviewParameter) } httpapi.Write(ctx, rw, http.StatusOK, response) @@ -155,7 +156,7 @@ func (api *API) handleParameterWebsocket(rw http.ResponseWriter, r *http.Request Diagnostics: db2sdk.HCLDiagnostics(diagnostics), } if result != nil { - response.Parameters = db2sdk.List(result.Parameters, db2sdk.PreviewParameter) + response.Parameters = slice.List(result.Parameters, db2sdk.PreviewParameter) } err = stream.Send(response) if err != nil { @@ -192,7 +193,7 @@ func (api *API) handleParameterWebsocket(rw http.ResponseWriter, r *http.Request Diagnostics: db2sdk.HCLDiagnostics(diagnostics), } if result != nil { - response.Parameters = db2sdk.List(result.Parameters, db2sdk.PreviewParameter) + response.Parameters = slice.List(result.Parameters, db2sdk.PreviewParameter) } err = stream.Send(response) if err != nil { diff --git a/coderd/provisionerdaemons.go b/coderd/provisionerdaemons.go index 67a40b88f6..9c08ed16db 100644 --- a/coderd/provisionerdaemons.go +++ b/coderd/provisionerdaemons.go @@ -13,6 +13,7 @@ import ( "github.com/coder/coder/v2/coderd/rbac" "github.com/coder/coder/v2/coderd/rbac/policy" "github.com/coder/coder/v2/coderd/util/ptr" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" ) @@ -81,7 +82,7 @@ func (api *API) provisionerDaemons(rw http.ResponseWriter, r *http.Request) { return } - httpapi.Write(ctx, rw, http.StatusOK, db2sdk.List(daemons, func(dbDaemon database.GetProvisionerDaemonsWithStatusByOrganizationRow) codersdk.ProvisionerDaemon { + httpapi.Write(ctx, rw, http.StatusOK, slice.List(daemons, func(dbDaemon database.GetProvisionerDaemonsWithStatusByOrganizationRow) codersdk.ProvisionerDaemon { pd := db2sdk.ProvisionerDaemon(dbDaemon.ProvisionerDaemon) var currentJob, previousJob *codersdk.ProvisionerDaemonJob if dbDaemon.CurrentJobID.Valid { diff --git a/coderd/provisionerjobs.go b/coderd/provisionerjobs.go index 1814f932d5..a710a96286 100644 --- a/coderd/provisionerjobs.go +++ b/coderd/provisionerjobs.go @@ -87,7 +87,7 @@ func (api *API) provisionerJobs(rw http.ResponseWriter, r *http.Request) { return } - httpapi.Write(ctx, rw, http.StatusOK, db2sdk.List(jobs, convertProvisionerJobWithQueuePosition)) + httpapi.Write(ctx, rw, http.StatusOK, slice.List(jobs, convertProvisionerJobWithQueuePosition)) } // handleAuthAndFetchProvisionerJobs is an internal method shared by diff --git a/coderd/users.go b/coderd/users.go index 8de2697e56..1655dfd3a1 100644 --- a/coderd/users.go +++ b/coderd/users.go @@ -1445,7 +1445,7 @@ func (api *API) organizationsByUser(rw http.ResponseWriter, r *http.Request) { return } - httpapi.Write(ctx, rw, http.StatusOK, db2sdk.List(organizations, db2sdk.Organization)) + httpapi.Write(ctx, rw, http.StatusOK, slice.List(organizations, db2sdk.Organization)) } // @Summary Get organization by user and organization name @@ -1669,6 +1669,6 @@ func convertAPIKey(k database.APIKey) codersdk.APIKey { Scopes: scopes, LifetimeSeconds: k.LifetimeSeconds, TokenName: k.TokenName, - AllowList: db2sdk.List(k.AllowList, db2sdk.APIAllowListTarget), + AllowList: slice.List(k.AllowList, db2sdk.APIAllowListTarget), } } diff --git a/coderd/wsbuilder/wsbuilder.go b/coderd/wsbuilder/wsbuilder.go index e870c89ee3..917030029a 100644 --- a/coderd/wsbuilder/wsbuilder.go +++ b/coderd/wsbuilder/wsbuilder.go @@ -30,6 +30,7 @@ import ( "github.com/coder/coder/v2/coderd/rbac/policy" "github.com/coder/coder/v2/coderd/tracing" "github.com/coder/coder/v2/coderd/util/ptr" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/provisioner/terraform/tfparse" "github.com/coder/coder/v2/provisionersdk" @@ -947,7 +948,7 @@ func (b *Builder) getTemplateVersionParameters() ([]previewtypes.Parameter, erro if err != nil && !xerrors.Is(err, sql.ErrNoRows) { return nil, xerrors.Errorf("get template version %s parameters: %w", tvID, err) } - b.templateVersionParameters = ptr.Ref(db2sdk.List(tvp, dynamicparameters.TemplateVersionParameter)) + b.templateVersionParameters = ptr.Ref(slice.List(tvp, dynamicparameters.TemplateVersionParameter)) return *b.templateVersionParameters, nil } diff --git a/enterprise/coderd/enidpsync/organizations_test.go b/enterprise/coderd/enidpsync/organizations_test.go index 47423dc588..be951e6926 100644 --- a/enterprise/coderd/enidpsync/organizations_test.go +++ b/enterprise/coderd/enidpsync/organizations_test.go @@ -12,7 +12,6 @@ import ( "cdr.dev/slog/v3/sloggers/slogtest" "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/database/dbfake" "github.com/coder/coder/v2/coderd/database/dbgen" @@ -21,6 +20,7 @@ import ( "github.com/coder/coder/v2/coderd/idpsync" "github.com/coder/coder/v2/coderd/rbac" "github.com/coder/coder/v2/coderd/runtimeconfig" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/enterprise/coderd/enidpsync" "github.com/coder/coder/v2/testutil" @@ -61,7 +61,7 @@ func TestOrganizationSync(t *testing.T) { }) require.NoError(t, err) - foundIDs := db2sdk.List(members, func(m database.OrganizationMembersRow) uuid.UUID { + foundIDs := slice.List(members, func(m database.OrganizationMembersRow) uuid.UUID { return m.OrganizationMember.OrganizationID }) require.ElementsMatch(t, expected, foundIDs, "match user organizations") diff --git a/enterprise/coderd/groups_test.go b/enterprise/coderd/groups_test.go index 967f927d60..4f300de766 100644 --- a/enterprise/coderd/groups_test.go +++ b/enterprise/coderd/groups_test.go @@ -12,12 +12,12 @@ import ( "github.com/coder/coder/v2/coderd/audit" "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/database/dbtestutil" "github.com/coder/coder/v2/coderd/rbac" "github.com/coder/coder/v2/coderd/rbac/rolestore" "github.com/coder/coder/v2/coderd/util/ptr" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/enterprise/coderd/coderdenttest" "github.com/coder/coder/v2/enterprise/coderd/license" @@ -893,7 +893,7 @@ func TestGroup(t *testing.T) { }) require.NoError(t, err) - foundIDs := db2sdk.List(found, func(g codersdk.Group) uuid.UUID { + foundIDs := slice.List(found, func(g codersdk.Group) uuid.UUID { return g.ID }) @@ -1009,7 +1009,7 @@ func TestGroups(t *testing.T) { // disabled, but group membership is limited to the requesting user. // TODO(geokat): add another test with workspace sharing disabled. require.Len(t, user5View, 3) - user5ViewIDs := db2sdk.List(user5View, func(g codersdk.Group) uuid.UUID { + user5ViewIDs := slice.List(user5View, func(g codersdk.Group) uuid.UUID { return g.ID }) diff --git a/enterprise/coderd/roles.go b/enterprise/coderd/roles.go index 5df6550495..0f7fcf0aa2 100644 --- a/enterprise/coderd/roles.go +++ b/enterprise/coderd/roles.go @@ -15,6 +15,7 @@ import ( "github.com/coder/coder/v2/coderd/httpmw" "github.com/coder/coder/v2/coderd/rbac" "github.com/coder/coder/v2/coderd/rbac/policy" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" ) @@ -62,9 +63,9 @@ func (api *API) postOrgRoles(rw http.ResponseWriter, r *http.Request) { UUID: organization.ID, Valid: true, }, - SitePermissions: db2sdk.List(req.SitePermissions, sdkPermissionToDB), - OrgPermissions: db2sdk.List(req.OrganizationPermissions, sdkPermissionToDB), - UserPermissions: db2sdk.List(req.UserPermissions, sdkPermissionToDB), + SitePermissions: slice.List(req.SitePermissions, sdkPermissionToDB), + OrgPermissions: slice.List(req.OrganizationPermissions, sdkPermissionToDB), + UserPermissions: slice.List(req.UserPermissions, sdkPermissionToDB), // Satisfy the linter (we don't support member permissions in non-system roles). MemberPermissions: database.CustomRolePermissions{}, IsSystem: false, @@ -154,9 +155,9 @@ func (api *API) putOrgRoles(rw http.ResponseWriter, r *http.Request) { // to throw an error, then the story of a previously valid role // now being invalid has to be addressed. Coder can change permissions, // objects, and actions at any time. - SitePermissions: db2sdk.List(filterInvalidPermissions(req.SitePermissions), sdkPermissionToDB), - OrgPermissions: db2sdk.List(filterInvalidPermissions(req.OrganizationPermissions), sdkPermissionToDB), - UserPermissions: db2sdk.List(filterInvalidPermissions(req.UserPermissions), sdkPermissionToDB), + SitePermissions: slice.List(filterInvalidPermissions(req.SitePermissions), sdkPermissionToDB), + OrgPermissions: slice.List(filterInvalidPermissions(req.OrganizationPermissions), sdkPermissionToDB), + UserPermissions: slice.List(filterInvalidPermissions(req.UserPermissions), sdkPermissionToDB), // Satisfy the linter (we don't support member permissions in non-system roles). MemberPermissions: database.CustomRolePermissions{}, }) diff --git a/enterprise/coderd/roles_test.go b/enterprise/coderd/roles_test.go index 8db6e59921..ccacb10a22 100644 --- a/enterprise/coderd/roles_test.go +++ b/enterprise/coderd/roles_test.go @@ -13,6 +13,7 @@ import ( "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/rbac" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/enterprise/coderd/coderdenttest" "github.com/coder/coder/v2/enterprise/coderd/license" @@ -63,7 +64,7 @@ func TestCustomOrganizationRole(t *testing.T) { // Changing this might mess up the UI in how it renders the roles on the // users page. When the users endpoint is updated, this should be uncommented. // roleNamesF := func(role codersdk.SlimRole) string { return role.Name } - // require.Contains(t, db2sdk.List(user.Roles, roleNamesF), role.Name) + // require.Contains(t, slice.List(user.Roles, roleNamesF), role.Name) // Try to create a template version coderdtest.CreateTemplateVersion(t, tmplAdmin, first.OrganizationID, nil) @@ -594,8 +595,8 @@ func TestListRoles(t *testing.T) { BuiltIn: true, } } - expected := db2sdk.List(c.ExpectedRoles, ignorePerms) - found := db2sdk.List(roles, ignorePerms) + expected := slice.List(c.ExpectedRoles, ignorePerms) + found := slice.List(roles, ignorePerms) require.ElementsMatch(t, expected, found) } }) diff --git a/enterprise/coderd/userauth_test.go b/enterprise/coderd/userauth_test.go index fd4706a25e..4dde31c625 100644 --- a/enterprise/coderd/userauth_test.go +++ b/enterprise/coderd/userauth_test.go @@ -15,7 +15,6 @@ import ( "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/coderd/coderdtest/oidctest" "github.com/coder/coder/v2/coderd/database" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/database/dbgen" "github.com/coder/coder/v2/coderd/database/dbtestutil" @@ -1122,7 +1121,7 @@ func (r *oidcTestRunner) AssertOrganizations(t *testing.T, userIdent string, inc cpy := make([]uuid.UUID, 0, len(expected)) cpy = append(cpy, expected...) hasDefault := false - userOrgIDs := db2sdk.List(userOrgs, func(o codersdk.Organization) uuid.UUID { + userOrgIDs := slice.List(userOrgs, func(o codersdk.Organization) uuid.UUID { if o.IsDefault { hasDefault = true cpy = append(cpy, o.ID) diff --git a/enterprise/members_test.go b/enterprise/members_test.go index 0180f323da..5fc072874a 100644 --- a/enterprise/members_test.go +++ b/enterprise/members_test.go @@ -7,8 +7,8 @@ import ( "github.com/stretchr/testify/require" "github.com/coder/coder/v2/coderd/coderdtest" - "github.com/coder/coder/v2/coderd/database/db2sdk" "github.com/coder/coder/v2/coderd/rbac" + "github.com/coder/coder/v2/coderd/util/slice" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/enterprise/coderd/coderdenttest" "github.com/coder/coder/v2/enterprise/coderd/license" @@ -56,7 +56,7 @@ func TestEnterpriseMembers(t *testing.T) { require.Len(t, members, 3) require.ElementsMatch(t, []uuid.UUID{first.UserID, user.ID, orgAdmin.ID}, - db2sdk.List(members, onlyIDs)) + slice.List(members, onlyIDs)) // Add the member to some groups _, err = orgAdminClient.PatchGroup(ctx, g1.ID, codersdk.PatchGroupRequest{ @@ -86,7 +86,7 @@ func TestEnterpriseMembers(t *testing.T) { require.Len(t, members, 2) require.ElementsMatch(t, []uuid.UUID{first.UserID, orgAdmin.ID}, - db2sdk.List(members, onlyIDs)) + slice.List(members, onlyIDs)) // User should now belong to 0 groups userGroups, err = orgAdminClient.Groups(ctx, codersdk.GroupArguments{ @@ -130,7 +130,7 @@ func TestEnterpriseMembers(t *testing.T) { require.Len(t, members, 3) require.ElementsMatch(t, []uuid.UUID{first.UserID, user.ID, userAdmin.ID}, - db2sdk.List(members, onlyIDs)) + slice.List(members, onlyIDs)) }) t.Run("PostUserNotExists", func(t *testing.T) {