Skip to content

Commit c7d2d3a

Browse files
chore: maybe init workspace from server (#686)
1 parent 58f706a commit c7d2d3a

File tree

9 files changed

+97
-39
lines changed

9 files changed

+97
-39
lines changed

apps/workspace-engine/pkg/db/workspaces.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,22 @@ func GetWorkspaceIDs(ctx context.Context) ([]string, error) {
3333
}
3434
return workspaceIDs, nil
3535
}
36+
37+
const WORKSPACE_EXISTS_QUERY = `
38+
SELECT EXISTS(SELECT 1 FROM workspace WHERE id = $1)
39+
`
40+
41+
func WorkspaceExists(ctx context.Context, workspaceId string) (bool, error) {
42+
db, err := GetDB(ctx)
43+
if err != nil {
44+
return false, err
45+
}
46+
defer db.Release()
47+
48+
var exists bool
49+
err = db.QueryRow(ctx, WORKSPACE_EXISTS_QUERY, workspaceId).Scan(&exists)
50+
if err != nil {
51+
return false, err
52+
}
53+
return exists, nil
54+
}

apps/workspace-engine/pkg/db/workspaces_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,34 @@ package db
22

33
import (
44
"testing"
5+
6+
"github.com/google/uuid"
57
)
68

9+
func TestDBWorkspaces_WorkspaceExists(t *testing.T) {
10+
workspaceID, conn := setupTestWithWorkspace(t)
11+
_ = conn // Avoid "declared but not used" error
12+
13+
// Test that the workspace exists
14+
exists, err := WorkspaceExists(t.Context(), workspaceID)
15+
if err != nil {
16+
t.Fatalf("expected no error, got %v", err)
17+
}
18+
if !exists {
19+
t.Fatalf("expected workspace %s to exist", workspaceID)
20+
}
21+
22+
// Test that a non-existent workspace returns false
23+
nonExistentID := uuid.New().String()
24+
exists, err = WorkspaceExists(t.Context(), nonExistentID)
25+
if err != nil {
26+
t.Fatalf("expected no error for non-existent workspace, got %v", err)
27+
}
28+
if exists {
29+
t.Fatalf("expected workspace %s to not exist, but it does", nonExistentID)
30+
}
31+
}
32+
733
func TestDBWorkspaces_GetWorkspaceIDs(t *testing.T) {
834
// Create multiple workspaces
935
workspaceID1, conn1 := setupTestWithWorkspace(t)

apps/workspace-engine/pkg/events/handler/handler.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (el *EventListener) ListenAndRoute(ctx context.Context, msg *kafka.Message)
143143
changeSet := changeset.NewChangeSet[any]()
144144
if !wsExists {
145145
ws = workspace.New(rawEvent.WorkspaceID)
146-
if err := loadWorkspaceWithInitialState(ctx, ws); err != nil {
146+
if err := workspace.PopulateWorkspaceWithInitialState(ctx, ws); err != nil {
147147
span.RecordError(err)
148148
span.SetStatus(codes.Error, "failed to load workspace")
149149
log.Error("Failed to load workspace", "error", err, "workspaceID", rawEvent.WorkspaceID)
@@ -154,7 +154,14 @@ func (el *EventListener) ListenAndRoute(ctx context.Context, msg *kafka.Message)
154154
}
155155
ctx = changeset.WithChangeSet(ctx, changeSet)
156156

157-
err := handler(ctx, ws, rawEvent)
157+
if err := handler(ctx, ws, rawEvent); err != nil {
158+
span.RecordError(err)
159+
span.SetStatus(codes.Error, "handler failed")
160+
log.Error("Handler failed to process event",
161+
"eventType", rawEvent.EventType,
162+
"error", err)
163+
return fmt.Errorf("handler failed to process event %s: %w", rawEvent.EventType, err)
164+
}
158165

159166
releaseTargetChanges, err := ws.ReleaseManager().ProcessChanges(ctx, changeSet)
160167
if err != nil {
@@ -173,15 +180,6 @@ func (el *EventListener) ListenAndRoute(ctx context.Context, msg *kafka.Message)
173180
return fmt.Errorf("failed to flush changeset: %w", err)
174181
}
175182

176-
if err != nil {
177-
span.RecordError(err)
178-
span.SetStatus(codes.Error, "handler failed")
179-
log.Error("Handler failed to process event",
180-
"eventType", rawEvent.EventType,
181-
"error", err)
182-
return fmt.Errorf("handler failed to process event %s: %w", rawEvent.EventType, err)
183-
}
184-
185183
span.SetStatus(codes.Ok, "event processed successfully")
186184
log.Debug("Successfully processed event",
187185
"eventType", rawEvent.EventType)

apps/workspace-engine/pkg/server/openapi/deployments/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import (
1111
type Deployments struct{}
1212

1313
func (s *Deployments) GetDeploymentResources(c *gin.Context, workspaceId string, deploymentId string) {
14-
ws := utils.GetWorkspace(c, workspaceId)
15-
if ws == nil {
14+
ws, err := utils.GetWorkspace(c, workspaceId)
15+
if err != nil {
16+
c.JSON(http.StatusInternalServerError, gin.H{
17+
"error": err.Error(),
18+
})
1619
return
1720
}
1821

apps/workspace-engine/pkg/server/openapi/environments/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ import (
1212
type Environments struct{}
1313

1414
func (s *Environments) GetEnvironmentResources(c *gin.Context, workspaceId string, environmentId string) {
15-
ws := utils.GetWorkspace(c, workspaceId)
16-
if ws == nil {
15+
ws, err := utils.GetWorkspace(c, workspaceId)
16+
if err != nil {
17+
c.JSON(http.StatusInternalServerError, gin.H{
18+
"error": err.Error(),
19+
})
1720
return
1821
}
1922

apps/workspace-engine/pkg/server/openapi/policies/policies.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
type Policies struct{}
1313

1414
func (p *Policies) GetReleaseTargetsForPolicy(c *gin.Context, workspaceId oapi.WorkspaceId, policyId oapi.PolicyId) {
15-
ws := utils.GetWorkspace(c, workspaceId)
16-
if ws == nil {
17-
c.JSON(http.StatusNotFound, gin.H{
18-
"error": "Workspace not found",
15+
ws, err := utils.GetWorkspace(c, workspaceId)
16+
if err != nil {
17+
c.JSON(http.StatusInternalServerError, gin.H{
18+
"error": "Failed to get workspace: " + err.Error(),
1919
})
2020
return
2121
}

apps/workspace-engine/pkg/server/openapi/releasetargets/releasetargets.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ type ReleaseTargets struct {
1515
// EvaluateReleaseTarget implements oapi.ServerInterface.
1616
func (s *ReleaseTargets) EvaluateReleaseTarget(c *gin.Context, workspaceId oapi.WorkspaceId) {
1717
// Get workspace
18-
ws := utils.GetWorkspace(c, workspaceId)
19-
if ws == nil {
20-
c.JSON(http.StatusNotFound, gin.H{
21-
"error": "Workspace not found",
18+
ws, err := utils.GetWorkspace(c, workspaceId)
19+
if err != nil {
20+
c.JSON(http.StatusInternalServerError, gin.H{
21+
"error": err.Error(),
2222
})
2323
return
2424
}
@@ -69,10 +69,10 @@ func (s *ReleaseTargets) EvaluateReleaseTarget(c *gin.Context, workspaceId oapi.
6969

7070
// GetPoliciesForReleaseTarget implements oapi.ServerInterface.
7171
func (s *ReleaseTargets) GetPoliciesForReleaseTarget(c *gin.Context, workspaceId oapi.WorkspaceId, releaseTargetId oapi.ReleaseTargetId) {
72-
ws := utils.GetWorkspace(c, workspaceId)
73-
if ws == nil {
74-
c.JSON(http.StatusNotFound, gin.H{
75-
"error": "Workspace not found",
72+
ws, err := utils.GetWorkspace(c, workspaceId)
73+
if err != nil {
74+
c.JSON(http.StatusInternalServerError, gin.H{
75+
"error": "Failed to get workspace: " + err.Error(),
7676
})
7777
return
7878
}
Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
package utils
22

33
import (
4-
"net/http"
4+
"fmt"
5+
"workspace-engine/pkg/db"
56
"workspace-engine/pkg/workspace"
67

78
"github.com/gin-gonic/gin"
89
)
910

10-
func GetWorkspace(c *gin.Context, workspaceId string) *workspace.Workspace {
11-
ok := workspace.HasWorkspace(workspaceId)
12-
if !ok {
13-
c.JSON(http.StatusNotFound, gin.H{
14-
"error": "Workspace not found",
15-
})
16-
return nil
11+
func GetWorkspace(c *gin.Context, workspaceId string) (*workspace.Workspace, error) {
12+
existsInDB, err := db.WorkspaceExists(c.Request.Context(), workspaceId)
13+
if err != nil {
14+
return nil, err
15+
}
16+
if !existsInDB {
17+
return nil, fmt.Errorf("workspace %s not found in database", workspaceId)
18+
}
19+
20+
if workspace.Exists(workspaceId) {
21+
return workspace.GetWorkspace(workspaceId), nil
1722
}
1823

19-
return workspace.GetWorkspace(workspaceId)
24+
ws := workspace.New(workspaceId)
25+
if err := workspace.PopulateWorkspaceWithInitialState(c.Request.Context(), ws); err != nil {
26+
return nil, fmt.Errorf("failed to populate workspace with initial state: %w", err)
27+
}
28+
workspace.Set(workspaceId, ws)
29+
return ws, nil
2030
}

apps/workspace-engine/pkg/events/handler/workspace.go renamed to apps/workspace-engine/pkg/workspace/populate_workspace.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package handler
1+
package workspace
22

33
import (
44
"context"
55
"workspace-engine/pkg/db"
6-
"workspace-engine/pkg/workspace"
76
)
87

9-
func loadWorkspaceWithInitialState(ctx context.Context, ws *workspace.Workspace) error {
8+
func PopulateWorkspaceWithInitialState(ctx context.Context, ws *Workspace) error {
109
initialWorkspaceState, err := db.LoadWorkspace(ctx, ws.ID)
1110
if err != nil {
1211
return err

0 commit comments

Comments
 (0)