8000 refactor: move PrebuildsSystemUserID constant to database package to … · coder/coder@f24e4ab · GitHub
[go: up one dir, main page]

Skip to content

Commit f24e4ab

Browse files
committed
refactor: move PrebuildsSystemUserID constant to database package to resolve import cycle
1 parent b2c7bdd commit f24e4ab

File tree

13 files changed

+40
-50
lines changed

13 files changed

+40
-50
lines changed

coderd/database/constants.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package database
2+
3+
import "github.com/google/uuid"
4+
5+
var PrebuildsSystemUserID = uuid.MustParse("c42fdf75-3097-471c-8c33-fb52454d81c0")

coderd/database/dbauthz/dbauthz.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/coder/coder/v2/coderd/database/dbtime"
2222
"github.com/coder/coder/v2/coderd/httpapi/httpapiconstraints"
2323
"github.com/coder/coder/v2/coderd/httpmw/loggermw"
24-
"github.com/coder/coder/v2/coderd/prebuilds"
2524
"github.com/coder/coder/v2/coderd/rbac"
2625
"github.com/coder/coder/v2/coderd/rbac/policy"
2726
"github.com/coder/coder/v2/coderd/rbac/rolestore"
@@ -423,7 +422,7 @@ var (
423422
subjectPrebuildsOrchestrator = rbac.Subject{
424423
Type: rbac.SubjectTypePrebuildsOrchestrator,
425424
FriendlyName: "Prebuilds Orchestrator",
426-
ID: prebuilds.SystemUserID.String(),
425+
ID: database.PrebuildsSystemUserID.String(),
427426
Roles: rbac.Roles([]rbac.Role{
428427
{
429428
Identifier: rbac.RoleIdentifier{Name: "prebuilds-orchestrator"},

coderd/database/dbmem/dbmem.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ import (
2323
"golang.org/x/exp/maps"
2424
"golang.org/x/xerrors"
2525

26-
"github.com/coder/coder/v2/coderd/notifications/types"
27-
"github.com/coder/coder/v2/coderd/prebuilds"
28-
2926
"github.com/coder/coder/v2/coderd/database"
3027
"github.com/coder/coder/v2/coderd/database/dbtime"
28+
"github.com/coder/coder/v2/coderd/notifications/types"
3129
"github.com/coder/coder/v2/coderd/rbac"
3230
"github.com/coder/coder/v2/coderd/rbac/regosql"
3331
"github.com/coder/coder/v2/coderd/util/slice"
@@ -159,7 +157,7 @@ func New() database.Store {
159157
q.mutex.Lock()
160158
// We can't insert this user using the interface, because it's a system user.
161159
q.data.users = append(q.data.users, database.User{
162-
ID: prebuilds.SystemUserID,
160+
ID: database.PrebuildsSystemUserID,
163161
Email: "prebuilds@coder.com",
164162
Username: "prebuilds",
165163
CreatedAt: dbtime.Now(),

coderd/database/modelmethods.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ func (w Workspace) RBACObject() rbac.Object {
235235
// IsPrebuild returns true if the workspace is a prebuild workspace.
236236
// A workspace is considered a prebuild if its owner is the prebuild system user.
237237
func (w Workspace) IsPrebuild() bool {
238-
// TODO: avoid import cycle
239-
return w.OwnerID == uuid.MustParse("c42fdf75-3097-471c-8c33-fb52454d81c0")
238+
return w.OwnerID == PrebuildsSystemUserID
240239
}
241240

242241
// AsPrebuild returns the RBAC object corresponding to the workspace type.

coderd/database/querier_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/coder/coder/v2/coderd/database/dbtime"
2828
"github.com/coder/coder/v2/coderd/database/migrations"
2929
"github.com/coder/coder/v2/coderd/httpmw"
30-
"github.com/coder/coder/v2/coderd/prebuilds"
3130
"github.com/coder/coder/v2/coderd/provisionerdserver"
3231
"github.com/coder/coder/v2/coderd/rbac"
3332
"github.com/coder/coder/v2/coderd/rbac/policy"
@@ -1418,7 +1417,7 @@ func TestGetUsers_IncludeSystem(t *testing.T) {
14181417
for _, u := range users {
14191418
if u.IsSystem {
14201419
foundSystemUser = true
1421-
require.Equal(t, prebuilds.SystemUserID, u.ID)
1420+
require.Equal(t, database.PrebuildsSystemUserID, u.ID)
14221421
} else {
14231422
foundRegularUser = true
14241423
require.Equalf(t, other.ID.String(), u.ID.String(), "found unexpected regular user")

coderd/prebuilds/id.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

enterprise/coderd/groups_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"testing"
77
"time"
88

9-
"github.com/coder/coder/v2/coderd/prebuilds"
10-
119
"github.com/google/uuid"
1210
"github.com/stretchr/testify/require"
1311

@@ -833,7 +831,7 @@ func TestGroup(t *testing.T) {
833831
ctx := testutil.Context(t, testutil.WaitLong)
834832

835833
// nolint:gocritic // "This client is operating as the owner user" is fine in this case.
836-
prebuildsUser, err := client.User(ctx, prebuilds.SystemUserID.String())
834+
prebuildsUser, err := client.User(ctx, database.PrebuildsSystemUserID.String())
837835
require.NoError(t, err)
838836
// The 'Everyone' group always has an ID that matches the organization ID.
839837
group, err := userAdminClient.Group(ctx, user.OrganizationID)

enterprise/coderd/prebuilds/claim.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (c EnterpriseClaimer) Claim(
4747
}
4848

4949
func (EnterpriseClaimer) Initiator() uuid.UUID {
50-
return prebuilds.SystemUserID
50+
return database.PrebuildsSystemUserID
5151
}
5252

5353
var _ prebuilds.Claimer = &EnterpriseClaimer{}

enterprise/coderd/prebuilds/membership_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/coder/coder/v2/coderd/database"
1313
"github.com/coder/coder/v2/coderd/database/dbgen"
1414
"github.com/coder/coder/v2/coderd/database/dbtestutil"
15-
agplprebuilds "github.com/coder/coder/v2/coderd/prebuilds"
1615
"github.com/coder/coder/v2/enterprise/coderd/prebuilds"
1716
)
1817

@@ -74,14 +73,14 @@ func TestReconcileAll(t *testing.T) {
7473
// dbmem doesn't ensure membership to the default organization
7574
dbgen.OrganizationMember(t, db, database.OrganizationMember{
7675
OrganizationID: defaultOrg.ID,
77-
UserID: agplprebuilds.SystemUserID,
76+
UserID: database.PrebuildsSystemUserID,
7877
})
7978
}
8079

81-
dbgen.OrganizationMember(t, db, database.OrganizationMember{OrganizationID: unrelatedOrg.ID, UserID: agplprebuilds.SystemUserID})
80+
dbgen.OrganizationMember(t, db, database.OrganizationMember{OrganizationID: unrelatedOrg.ID, UserID: database.PrebuildsSystemUserID})
8281
if tc.preExistingMembership {
8382
// System user already a member of both orgs.
84-
dbgen.OrganizationMember(t, db, database.OrganizationMember{OrganizationID: targetOrg.ID, UserID: agplprebuilds.SystemUserID})
83+
dbgen.OrganizationMember(t, db, database.OrganizationMember{OrganizationID: targetOrg.ID, UserID: database.PrebuildsSystemUserID})
8584
}
8685

8786
presets := []database.GetTemplatePresetsWithPrebuildsRow{newPresetRow(unrelatedOrg.ID)}
@@ -91,7 +90,7 @@ func TestReconcileAll(t *testing.T) {
9190

9291
// Verify memberships before reconciliation.
9392
preReconcileMemberships, err := db.GetOrganizationsByUserID(ctx, database.GetOrganizationsByUserIDParams{
94-
UserID: agplprebuilds.SystemUserID,
93+
UserID: database.PrebuildsSystemUserID,
9594
})
9695
require.NoError(t, err)
9796
expectedMembershipsBefore := []uuid.UUID{defaultOrg.ID, unrelatedOrg.ID}
@@ -102,11 +101,11 @@ func TestReconcileAll(t *testing.T) {
102101

103102
// Reconcile
104103
reconciler := prebuilds.NewStoreMembershipReconciler(db, clock)
105-
require.NoError(t, reconciler.ReconcileAll(ctx, agplprebuilds.SystemUserID, presets))
104+
require.NoError(t, reconciler.ReconcileAll(ctx, database.PrebuildsSystemUserID, presets))
106105

107106
// Verify memberships after reconciliation.
108107
postReconcileMemberships, err := db.GetOrganizationsByUserID(ctx, database.GetOrganizationsByUserIDParams{
109-
UserID: agplprebuilds.SystemUserID,
108+
UserID: database.PrebuildsSystemUserID,
110109
})
111110
require.NoError(t, err)
112111
expectedMembershipsAfter := expectedMembershipsBefore

enterprise/coderd/prebuilds/metricscollector_test.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/coder/coder/v2/coderd/database/dbgen"
2121
"github.com/coder/coder/v2/coderd/database/dbtestutil"
2222
"github.com/coder/coder/v2/coderd/database/dbtime"
23-
agplprebuilds "github.com/coder/coder/v2/coderd/prebuilds"
2423
"github.com/coder/coder/v2/codersdk"
2524
"github.com/coder/coder/v2/enterprise/coderd/prebuilds"
2625
"github.com/coder/coder/v2/testutil"
@@ -55,8 +54,8 @@ func TestMetricsCollector(t *testing.T) {
5554
name: "prebuild provisioned but not completed",
5655
transitions: allTransitions,
5756
jobStatuses: allJobStatusesExcept(database.ProvisionerJobStatusPending, database.ProvisionerJobStatusRunning, database.ProvisionerJobStatusCanceling),
58-
initiatorIDs: []uuid.UUID{agplprebuilds.SystemUserID},
59-
ownerIDs: []uuid.UUID{agplprebuilds.SystemUserID},
57+
initiatorIDs: []uuid.UUID{database.PrebuildsSystemUserID},
58+
ownerIDs: []uuid.UUID{database.PrebuildsSystemUserID},
6059
metrics: []metricCheck{
6160
{prebuilds.MetricCreatedCount, ptr.To(1.0), true},
6261
{prebuilds.MetricClaimedCount, ptr.To(0.0), true},
@@ -72,8 +71,8 @@ func TestMetricsCollector(t *testing.T) {
7271
name: "prebuild running",
7372
transitions: []database.WorkspaceTransition{database.WorkspaceTransitionStart},
7473
jobStatuses: []database.ProvisionerJobStatus{database.ProvisionerJobStatusSucceeded},
75-
initiatorIDs: []uuid.UUID{agplprebuilds.SystemUserID},
76-
ownerIDs: []uuid.UUID{agplprebuilds.SystemUserID},
74+
initiatorIDs: []uuid.UUID{database.PrebuildsSystemUserID},
75+
ownerIDs: []uuid.UUID{database.PrebuildsSystemUserID},
7776
metrics: []metricCheck{
7877
{prebuilds.MetricCreatedCount, ptr.To(1.0), true},
7978
{prebuilds.MetricClaimedCount, ptr.To(0.0), true},
@@ -89,8 +88,8 @@ func TestMetricsCollector(t *testing.T) {
8988
name: "prebuild failed",
9089
transitions: allTransitions,
9190
jobStatuses: []database.ProvisionerJobStatus{database.ProvisionerJobStatusFailed},
92-
initiatorIDs: []uuid.UUID{agplprebuilds.SystemUserID},
93-
ownerIDs: []uuid.UUID{agplprebuilds.SystemUserID, uuid.New()},
91+
initiatorIDs: []uuid.UUID{database.PrebuildsSystemUserID},
92+
ownerIDs: []uuid.UUID{database.PrebuildsSystemUserID, uuid.New()},
9493
metrics: []metricCheck{
9594
{prebuilds.MetricCreatedCount, ptr.To(1.0), true},
9695
{prebuilds.MetricFailedCount, ptr.To(1.0), true},
@@ -105,8 +104,8 @@ func TestMetricsCollector(t *testing.T) {
105104
name: "prebuild eligible",
106105
transitions: []database.WorkspaceTransition{database.WorkspaceTransitionStart},
107106
jobStatuses: []database.ProvisionerJobStatus{database.ProvisionerJobStatusSucceeded},
108-
initiatorIDs: []uuid.UUID{agplprebuilds.SystemUserID},
109-
ownerIDs: []uuid.UUID{agplprebuilds.SystemUserID},
107+
initiatorIDs: []uuid.UUID{database.PrebuildsSystemUserID},
108+
ownerIDs: []uuid.UUID{database.PrebuildsSystemUserID},
110109
metrics: []metricCheck{
111110
{prebuilds.MetricCreatedCount, ptr.To(1.0), true},
112111
{prebuilds.MetricClaimedCount, ptr.To(0.0), true},
@@ -122,8 +121,8 @@ func TestMetricsCollector(t *testing.T) {
122121
name: "prebuild ineligible",
123122
transitions: allTransitions,
124123
jobStatuses: allJobStatusesExcept(database.ProvisionerJobStatusSucceeded),
125-
initiatorIDs: []uuid.UUID{agplprebuilds.SystemUserID},
126-
ownerIDs: []uuid.UUID{agplprebuilds.SystemUserID},
124+
initiatorIDs: []uuid.UUID{database.PrebuildsSystemUserID},
125+
ownerIDs: []uuid.UUID{database.PrebuildsSystemUserID},
127126
metrics: []metricCheck{
128127
{prebuilds.MetricCreatedCount, ptr.To(1.0), true},
129128
{prebuilds.MetricClaimedCount, ptr.To(0.0), true},
@@ -139,7 +138,7 @@ func TestMetricsCollector(t *testing.T) {
139138
name: "prebuild claimed",
140139
transitions: allTransitions,
141140
jobStatuses: allJobStatuses,
142-
initiatorIDs: []uuid.UUID{agplprebuilds.SystemUserID},
141+
initiatorIDs: []uuid.UUID{database.PrebuildsSystemUserID},
143142
ownerIDs: []uuid.UUID{uuid.New()},
144143
metrics: []metricCheck{
145144
{prebuilds.MetricCreatedCount, ptr.To(1.0), true},
@@ -169,8 +168,8 @@ func TestMetricsCollector(t *testing.T) {
169168
name: "deleted templates should not be included in exported metrics",
170169
transitions: allTransitions,
171170
jobStatuses: allJobStatuses,
172-
initiatorIDs: []uuid.UUID{agplprebuilds.SystemUserID},
173-
ownerIDs: []uuid.UUID{agplprebuilds.SystemUserID, uuid.New()},
171+
initiatorIDs: []uuid.UUID{database.PrebuildsSystemUserID},
172+
ownerIDs: []uuid.UUID{database.PrebuildsSystemUserID, uuid.New()},
174173
metrics: nil,
175174
templateDeleted: []bool{true},
176175
eligible: []bool{false},
@@ -209,7 +208,7 @@ func TestMetricsCollector(t *testing.T) {
209208
reconciler := prebuilds.NewStoreReconciler(db, pubsub, codersdk.PrebuildsConfig{}, logger, quartz.NewMock(t), prometheus.NewRegistry(), newNoopEnqueuer())
210209
ctx := testutil.Context(t, testutil.WaitLong)
211210

212-
createdUsers := []uuid.UUID{agplprebuilds.SystemUserID}
211+
createdUsers := []uuid.UUID{database.PrebuildsSystemUserID}
213212
for _, user := range slices.Concat(test.ownerIDs, test.initiatorIDs) {
214213
if !slices.Contains(createdUsers, user) {
215214
dbgen.User(t, db, database.User{
@@ -327,8 +326,8 @@ func TestMetricsCollector_DuplicateTemplateNames(t *testing.T) {
327326
test := testCase{
328327
transition: database.WorkspaceTransitionStart,
329328
jobStatus: database.ProvisionerJobStatusSucceeded,
330-
initiatorID: agplprebuilds.SystemUserID,
331-
ownerID: agplprebuilds.SystemUserID,
329+
initiatorID: database.PrebuildsSystemUserID,
330+
ownerID: database.PrebuildsSystemUserID,
332331
metrics: []metricCheck{
333332
{prebuilds.MetricCreatedCount, ptr.To(1.0), true},
334333
{prebuilds.MetricClaimedCount, ptr.To(0.0), true},

enterprise/coderd/prebuilds/reconcile.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func (c *StoreReconciler) ReconcileAll(ctx context.Context) error {
265265
}
266266

267267
membershipReconciler := NewStoreMembershipReconciler(c.store, c.clock)
268-
err = membershipReconciler.ReconcileAll(ctx, prebuilds.SystemUserID, snapshot.Presets)
268+
err = membershipReconciler.ReconcileAll(ctx, database.PrebuildsSystemUserID, snapshot.Presets)
269269
if err != nil {
270270
return xerrors.Errorf("reconcile prebuild membership: %w", err)
271271
}
@@ -667,7 +667,7 @@ func (c *StoreReconciler) createPrebuiltWorkspace(ctx context.Context, prebuiltW
667667
ID: prebuiltWorkspaceID,
668668
CreatedAt: now,
669669
UpdatedAt: now,
670-
OwnerID: prebuilds.SystemUserID,
670+
OwnerID: database.PrebuildsSystemUserID,
671671
OrganizationID: template.OrganizationID,
672672
TemplateID: template.ID,
673673
Name: name,
@@ -709,7 +709,7 @@ func (c *StoreReconciler) deletePrebuiltWorkspace(ctx context.Context, prebuiltW
709709
return xerrors.Errorf("failed to get template: %w", err)
710710
}
711711

712-
if workspace.OwnerID != prebuilds.SystemUserID {
712+
if workspace.OwnerID != database.PrebuildsSystemUserID {
713713
return xerrors.Errorf("prebuilt workspace is not owned by prebuild user anymore, probably it was claimed")
714714
}
715715

@@ -752,7 +752,7 @@ func (c *StoreReconciler) provision(
752752

753753
builder := wsbuilder.New(workspace, transition).
754754
Reason(database.BuildReasonInitiator).
755-
Initiator(prebuilds.SystemUserID).
755+
Initiator(database.PrebuildsSystemUserID).
756756
MarkPrebuild()
757757

758758
if transition != 107D1 database.WorkspaceTransitionDelete {

enterprise/coderd/prebuilds/reconcile_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ func setupTestDBPrebuild(
16681668
templateVersionID uuid.UUID,
16691669
) (database.WorkspaceTable, database.WorkspaceBuild) {
16701670
t.Helper()
1671-
return setupTestDBWorkspace(t, clock, db, ps, transition, prebuildStatus, orgID, preset, templateID, templateVersionID, agplprebuilds.SystemUserID, agplprebuilds.SystemUserID)
1671+
return setupTestDBWorkspace(t, clock, db, ps, transition, prebuildStatus, orgID, preset, templateID, templateVersionID, database.PrebuildsSystemUserID, database.PrebuildsSystemUserID)
16721672
}
16731673

16741674
func setupTestDBWorkspace(

enterprise/coderd/workspaces_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/coder/coder/v2/coderd/database/dbtime"
3333
"github.com/coder/coder/v2/coderd/httpmw"
3434
"github.com/coder/coder/v2/coderd/notifications"
35-
"github.com/coder/coder/v2/coderd/prebuilds"
3635
"github.com/coder/coder/v2/coderd/provisionerdserver"
3736
"github.com/coder/coder/v2/coderd/rbac"
3837
"github.com/coder/coder/v2/coderd/rbac/policy"
@@ -496,7 +495,7 @@ func TestCreateUserWorkspace(t *testing.T) {
496495
}).Do()
497496

498497
r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
499-
OwnerID: prebuilds.SystemUserID,
498+
OwnerID: database.PrebuildsSystemUserID,
500499
TemplateID: tv.Template.ID,
501500
}).Seed(database.WorkspaceBuild{
502501
TemplateVersionID: tv.TemplateVersion.ID,

0 commit comments

Comments
 (0)
0