Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: upsertcoder_app resources in case they are persistent#18509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
dannykopping merged 2 commits intomainfromdk/upsert-app
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletionscoderd/agentapi/subagent.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,12 +12,13 @@ import (
"golang.org/x/xerrors"

"cdr.dev/slog"
"github.com/coder/quartz"

agentproto "github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisioner"
"github.com/coder/quartz"
)

type SubAgentAPI struct {
Expand DownExpand Up@@ -164,8 +165,8 @@ func (a *SubAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.Create
}
}

_, err := a.Database.InsertWorkspaceApp(ctx, database.InsertWorkspaceAppParams{
ID: uuid.New(),
_, err := a.Database.UpsertWorkspaceApp(ctx, database.UpsertWorkspaceAppParams{
ID: uuid.New(), // NOTE: we may need to maintain the app's ID here for stability, but for now we'll leave this as-is.
CreatedAt: createdAt,
AgentID: subAgent.ID,
Slug: app.Slug,
Expand Down
34 changes: 17 additions & 17 deletionscoderd/database/dbauthz/dbauthz.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3938,23 +3938,6 @@ func (q *querier) InsertWorkspaceAgentStats(ctx context.Context, arg database.In
return q.db.InsertWorkspaceAgentStats(ctx, arg)
}

func (q *querier) InsertWorkspaceApp(ctx context.Context, arg database.InsertWorkspaceAppParams) (database.WorkspaceApp, error) {
// NOTE(DanielleMaywood):
// It is possible for there to exist an agent without a workspace.
// This means that we want to allow execution to continue if
// there isn't a workspace found to allow this behavior to continue.
workspace, err := q.db.GetWorkspaceByAgentID(ctx, arg.AgentID)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return database.WorkspaceApp{}, err
}

if err := q.authorizeContext(ctx, policy.ActionUpdate, workspace); err != nil {
return database.WorkspaceApp{}, err
}

return q.db.InsertWorkspaceApp(ctx, arg)
}

func (q *querier) InsertWorkspaceAppStats(ctx context.Context, arg database.InsertWorkspaceAppStatsParams) error {
if err := q.authorizeContext(ctx, policy.ActionCreate, rbac.ResourceSystem); err != nil {
return err
Expand DownExpand Up@@ -5181,6 +5164,23 @@ func (q *querier) UpsertWorkspaceAgentPortShare(ctx context.Context, arg databas
return q.db.UpsertWorkspaceAgentPortShare(ctx, arg)
}

func (q *querier) UpsertWorkspaceApp(ctx context.Context, arg database.UpsertWorkspaceAppParams) (database.WorkspaceApp, error) {
// NOTE(DanielleMaywood):
// It is possible for there to exist an agent without a workspace.
// This means that we want to allow execution to continue if
// there isn't a workspace found to allow this behavior to continue.
workspace, err := q.db.GetWorkspaceByAgentID(ctx, arg.AgentID)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return database.WorkspaceApp{}, err
}

if err := q.authorizeContext(ctx, policy.ActionUpdate, workspace); err != nil {
return database.WorkspaceApp{}, err
}

return q.db.UpsertWorkspaceApp(ctx, arg)
}

func (q *querier) UpsertWorkspaceAppAuditSession(ctx context.Context, arg database.UpsertWorkspaceAppAuditSessionParams) (bool, error) {
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceSystem); err != nil {
return false, err
Expand Down
4 changes: 2 additions & 2 deletionscoderd/database/dbauthz/dbauthz_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4114,7 +4114,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
APIKeyScope: database.AgentKeyScopeEnumAll,
}).Asserts(ws, policy.ActionCreateAgent)
}))
s.Run("InsertWorkspaceApp", s.Subtest(func(db database.Store, check *expects) {
s.Run("UpsertWorkspaceApp", s.Subtest(func(db database.Store, check *expects) {
_ = dbgen.User(s.T(), db, database.User{})
u := dbgen.User(s.T(), db, database.User{})
o := dbgen.Organization(s.T(), db, database.Organization{})
Expand All@@ -4130,7 +4130,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
_ = dbgen.WorkspaceBuild(s.T(), db, database.WorkspaceBuild{WorkspaceID: ws.ID, JobID: j.ID, TemplateVersionID: tv.ID})
res := dbgen.WorkspaceResource(s.T(), db, database.WorkspaceResource{JobID: j.ID})
agent := dbgen.WorkspaceAgent(s.T(), db, database.WorkspaceAgent{ResourceID: res.ID})
check.Args(database.InsertWorkspaceAppParams{
check.Args(database.UpsertWorkspaceAppParams{
ID: uuid.New(),
AgentID: agent.ID,
Health: database.WorkspaceAppHealthDisabled,
Expand Down
2 changes: 1 addition & 1 deletioncoderd/database/dbgen/dbgen.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -778,7 +778,7 @@ func ProvisionerKey(t testing.TB, db database.Store, orig database.ProvisionerKe
}

funcWorkspaceApp(t testing.TB,db database.Store,orig database.WorkspaceApp) database.WorkspaceApp {
resource,err:=db.InsertWorkspaceApp(genCtx, database.InsertWorkspaceAppParams{
resource,err:=db.UpsertWorkspaceApp(genCtx, database.UpsertWorkspaceAppParams{
ID:takeFirst(orig.ID,uuid.New()),
CreatedAt:takeFirst(orig.CreatedAt,dbtime.Now()),
AgentID:takeFirst(orig.AgentID,uuid.New()),
Expand Down
94 changes: 52 additions & 42 deletionscoderd/database/dbmem/dbmem.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10018,48 +10018,6 @@ func (q *FakeQuerier) InsertWorkspaceAgentStats(_ context.Context, arg database.
return nil
}

func (q *FakeQuerier) InsertWorkspaceApp(_ context.Context, arg database.InsertWorkspaceAppParams) (database.WorkspaceApp, error) {
if err := validateDatabaseType(arg); err != nil {
return database.WorkspaceApp{}, err
}

q.mutex.Lock()
defer q.mutex.Unlock()

if arg.SharingLevel == "" {
arg.SharingLevel = database.AppSharingLevelOwner
}

if arg.OpenIn == "" {
arg.OpenIn = database.WorkspaceAppOpenInSlimWindow
}

// nolint:gosimple
workspaceApp := database.WorkspaceApp{
ID: arg.ID,
AgentID: arg.AgentID,
CreatedAt: arg.CreatedAt,
Slug: arg.Slug,
DisplayName: arg.DisplayName,
Icon: arg.Icon,
Command: arg.Command,
Url: arg.Url,
External: arg.External,
Subdomain: arg.Subdomain,
SharingLevel: arg.SharingLevel,
HealthcheckUrl: arg.HealthcheckUrl,
HealthcheckInterval: arg.HealthcheckInterval,
HealthcheckThreshold: arg.HealthcheckThreshold,
Health: arg.Health,
Hidden: arg.Hidden,
DisplayOrder: arg.DisplayOrder,
OpenIn: arg.OpenIn,
DisplayGroup: arg.DisplayGroup,
}
q.workspaceApps = append(q.workspaceApps, workspaceApp)
return workspaceApp, nil
}

func (q *FakeQuerier) InsertWorkspaceAppStats(_ context.Context, arg database.InsertWorkspaceAppStatsParams) error {
err := validateDatabaseType(arg)
if err != nil {
Expand DownExpand Up@@ -13192,6 +13150,58 @@ func (q *FakeQuerier) UpsertWorkspaceAgentPortShare(_ context.Context, arg datab
return psl, nil
}

func (q *FakeQuerier) UpsertWorkspaceApp(ctx context.Context, arg database.UpsertWorkspaceAppParams) (database.WorkspaceApp, error) {
err := validateDatabaseType(arg)
if err != nil {
return database.WorkspaceApp{}, err
}

q.mutex.Lock()
defer q.mutex.Unlock()

if arg.SharingLevel == "" {
arg.SharingLevel = database.AppSharingLevelOwner
}
if arg.OpenIn == "" {
arg.OpenIn = database.WorkspaceAppOpenInSlimWindow
}

buildApp := func(id uuid.UUID, createdAt time.Time) database.WorkspaceApp {
return database.WorkspaceApp{
ID: id,
CreatedAt: createdAt,
AgentID: arg.AgentID,
Slug: arg.Slug,
DisplayName: arg.DisplayName,
Icon: arg.Icon,
Command: arg.Command,
Url: arg.Url,
External: arg.External,
Subdomain: arg.Subdomain,
SharingLevel: arg.SharingLevel,
HealthcheckUrl: arg.HealthcheckUrl,
HealthcheckInterval: arg.HealthcheckInterval,
HealthcheckThreshold: arg.HealthcheckThreshold,
Health: arg.Health,
Hidden: arg.Hidden,
DisplayOrder: arg.DisplayOrder,
OpenIn: arg.OpenIn,
DisplayGroup: arg.DisplayGroup,
}
}

for i, app := range q.workspaceApps {
if app.ID == arg.ID {
q.workspaceApps[i] = buildApp(app.ID, app.CreatedAt)
return q.workspaceApps[i], nil
}
}

workspaceApp := buildApp(arg.ID, arg.CreatedAt)
q.workspaceApps = append(q.workspaceApps, workspaceApp)
return workspaceApp, nil
}

func (q *FakeQuerier) UpsertWorkspaceAppAuditSession(_ context.Context, arg database.UpsertWorkspaceAppAuditSessionParams) (bool, error) {
err := validateDatabaseType(arg)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletionscoderd/database/dbmetrics/querymetrics.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

30 changes: 15 additions & 15 deletionscoderd/database/dbmock/dbmock.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

2 changes: 1 addition & 1 deletioncoderd/database/querier.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp