Skip to content

Commit c432d49

Browse files
committed
chore: add organization_id column to provisioner daemons
1 parent 5c6974e commit c432d49

File tree

9 files changed

+62
-22
lines changed

9 files changed

+62
-22
lines changed

coderd/coderd.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,10 +1239,18 @@ func (api *API) CreateInMemoryProvisionerDaemon(dialCtx context.Context, name st
12391239
}
12401240
}()
12411241

1242+
// All in memory provisioners will be apart of the default org for now.
1243+
//nolint:gocritic // in-memory provisioners are owned by system
1244+
defaultOrg, err := api.Database.GetDefaultOrganization(dbauthz.AsSystemRestricted(dialCtx))
1245+
if err != nil {
1246+
return nil, xerrors.Errorf("unable to fetch default org for in memory provisioner: %w", err)
1247+
}
1248+
12421249
//nolint:gocritic // in-memory provisioners are owned by system
12431250
daemon, err := api.Database.UpsertProvisionerDaemon(dbauthz.AsSystemRestricted(dialCtx), database.UpsertProvisionerDaemonParams{
1244-
Name: name,
1245-
CreatedAt: dbtime.Now(),
1251+
Name: name,
1252+
OrganizationID: defaultOrg.ID,
1253+
CreatedAt: dbtime.Now(),
12461254
Provisioners: []database.ProvisionerType{
12471255
database.ProvisionerTypeEcho, database.ProvisionerTypeTerraform,
12481256
},

coderd/database/dump.sql

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/foreign_key_constraint.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE provisioner_daemons
2+
DROP COLUMN organization_id;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- At the time of this migration, only 1 org is expected in a deployment.
2+
-- In the future when multi-org is more common, there might be a use case
3+
-- to allow a provisioner to be associated with multiple orgs.
4+
ALTER TABLE provisioner_daemons
5+
ADD COLUMN organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE;
6+
7+
UPDATE
8+
provisioner_daemons
9+
SET
10+
-- Default to the first org
11+
organization_id = (SELECT id FROM organizations ORDER BY organizations.created_at ASC LIMIT 1 );
12+
13+
ALTER TABLE provisioner_daemons
14+
ALTER COLUMN organization_id SET NOT NULL;

coderd/database/models.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 17 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/provisionerdaemons.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ INSERT INTO
2424
tags,
2525
last_seen_at,
2626
"version",
27+
organization_id,
2728
api_version
2829
)
2930
VALUES (
@@ -34,6 +35,7 @@ VALUES (
3435
@tags,
3536
@last_seen_at,
3637
@version,
38+
@organization_id,
3739
@api_version
3840
) ON CONFLICT("name", LOWER(COALESCE(tags ->> 'owner'::text, ''::text))) DO UPDATE SET
3941
provisioners = @provisioners,

enterprise/coderd/provisionerdaemons.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func (p *provisionerDaemonAuth) authorize(r *http.Request, tags map[string]strin
134134
// @Router /organizations/{organization}/provisionerdaemons/serve [get]
135135
func (api *API) provisionerDaemonServe(rw http.ResponseWriter, r *http.Request) {
136136
ctx := r.Context()
137+
organization := httpmw.OrganizationParam(r)
137138

138139
tags := map[string]string{}
139140
if r.URL.Query().Has("tag") {
@@ -246,13 +247,14 @@ func (api *API) provisionerDaemonServe(rw http.ResponseWriter, r *http.Request)
246247
// Create the daemon in the database.
247248
now := dbtime.Now()
248249
daemon, err := api.Database.UpsertProvisionerDaemon(authCtx, database.UpsertProvisionerDaemonParams{
249-
Name: name,
250-
Provisioners: provisioners,
251-
Tags: tags,
252-
CreatedAt: now,
253-
LastSeenAt: sql.NullTime{Time: now, Valid: true},
254-
Version: versionHdrVal,
255-
APIVersion: apiVersion,
250+
Name: name,
251+
Provisioners: provisioners,
252+
Tags: tags,
253+
CreatedAt: now,
254+
LastSeenAt: sql.NullTime{Time: now, Valid: true},
255+
Version: versionHdrVal,
256+
APIVersion: apiVersion,
257+
OrganizationID: organization.ID,
256258
})
257259
if err != nil {
258260
if !xerrors.Is(err, context.Canceled) {

0 commit comments

Comments
 (0)