Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make pending requests more explicit #8

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion model/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ func (svc *mockWorkspaces) CreateWorkspace(ctx context.Context, ws *model.Worksp
newWS.ID = svc.nextID
svc.nextID++
newWS.Enabled = false
newWS.Request = nil
newWS.Request = &model.WorkspaceUpdate{
WorkspaceID: newWS.ID,
ByUser: newWS.Users[0],
Enabled: true,
Nodegroup: newWS.Nodegroup,
Userdata: newWS.Userdata,
Quotas: maps.Clone(newWS.Quotas),
Users: slices.Clone(newWS.Users),
}
slices.Sort(newWS.Users)

svc.data[newWS.ID] = newWS
Expand Down
9 changes: 9 additions & 0 deletions model/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ func (svc *workspacesRepository) CreateWorkspace(ctx context.Context, ws *model.
}
}

upd := ws.InitialRequest()
upd.WorkspaceID = id
tx.Exec(ctx, `
INSERT INTO workspaces_updaterequests (workspace_id, by_user, data)
VALUES ($1, $2, $3)
ON CONFLICT (workspace_id) DO UPDATE
SET by_user = EXCLUDED.by_user, data = EXCLUDED.data`,
upd.WorkspaceID, upd.ByUser, upd)

// we could reconstruct the ws here, but it's easier to just query it
newWs, err = queryWorkspace(ctx, tx, id)
return err
Expand Down
6 changes: 5 additions & 1 deletion model/test/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestWorkspace(t *testing.T, wsf func() model.WorkspaceService) {
Users: []string{"user1"},
}
want.ID = testWorkspaceCreate(t, wsSvc, &want, nil)
want.Request = want.InitialRequest()

testWorkspaceListAll(t, wsSvc, []*model.Workspace{&want})
testWorkspaceListUser(t, wsSvc, "user1", []*model.Workspace{&want})
Expand Down Expand Up @@ -209,18 +210,21 @@ func TestWorkspace(t *testing.T, wsf func() model.WorkspaceService) {
Users: []string{"user1"},
}
ws1.ID = testWorkspaceCreate(t, wsSvc, &ws1, nil)
ws1.Request = ws1.InitialRequest()

ws2 := model.Workspace{
Nodegroup: model.NodegroupGraduate,
Users: []string{"user2"},
}
ws2.ID = testWorkspaceCreate(t, wsSvc, &ws2, nil)
ws2.Request = ws2.InitialRequest()

wsAll := model.Workspace{
Nodegroup: model.NodegroupUndergraduate,
Users: []string{"user1", "user2"},
}
wsAll.ID = testWorkspaceCreate(t, wsSvc, &wsAll, nil)
wsAll.Request = wsAll.InitialRequest()

testWorkspaceListAll(t, wsSvc, []*model.Workspace{&ws1, &ws2, &wsAll})
testWorkspaceListUser(t, wsSvc, "user1", []*model.Workspace{&ws1, &wsAll})
Expand Down Expand Up @@ -291,7 +295,7 @@ func testWorkspaceCreate(t *testing.T, wsSvc model.WorkspaceService, ws *model.W
want := *ws
want.ID = got.ID
want.Enabled = false
want.Request = nil
want.Request = want.InitialRequest()
if diff := cmp.Diff(got, &want, cmpopts.EquateEmpty()); diff != "" {
t.Fatalf("CreateWorkspace(%#v) = mismatch\n%s", ws, diff)
}
Expand Down
14 changes: 14 additions & 0 deletions model/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ func (ws Workspace) Valid() bool {
return true
}

// InitialRequest returns the initial request for a new workspace. It has the
// same attributes as the workspace itself, but enabled.
func (ws *Workspace) InitialRequest() *WorkspaceUpdate {
return &WorkspaceUpdate{
WorkspaceID: ws.ID,
ByUser: ws.Users[0],
Enabled: true,
Nodegroup: ws.Nodegroup,
Userdata: ws.Userdata,
Quotas: ws.Quotas,
Users: ws.Users,
}
}

type WorkspaceUpdate struct {
WorkspaceID ID
ByUser string
Expand Down
31 changes: 19 additions & 12 deletions view/workspace.templ
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,25 @@ templ PageWorkspaceDetails(ws *model.Workspace, kubeconfig string) {

// Render the status badge
templ wsStatusButton(ws *model.Workspace) {
if ws.Enabled && ws.Request == nil {
// enabled (and created) and no pending requests: all good
<span class="ml-4 rounded-full border border-green-700 bg-green-200 text-green-700 px-2 py-1">Enabled</span>
} else if !ws.Created {
// hasn't ever been enabled: pending approval
<span class="ml-4 rounded-full border border-amber-700 bg-amber-200 text-amber-700 px-2 py-1">Pending approval</span>
} else if !ws.Enabled {
// created && !enabled: disabled by admin
<span class="ml-4 rounded-full border border-amber-700 bg-red-200 text-red-700 px-2 py-1">Disabled</span>
} else if ws.Enabled && ws.Request != nil {
// otherwise, enabled but has pending changes
<span class="ml-4 rounded-full border border-amber-700 bg-amber-200 text-amber-700 px-2 py-1">Pending changes</span>
switch true {
case !ws.Created && ws.Request != nil:
// pending approval (initial state)
<span class="ml-4 rounded-full border border-amber-700 bg-amber-200 text-amber-700 px-2 py-1">Pending</span>
case !ws.Created && ws.Request == nil:
// request has been rejected
<span class="ml-4 rounded-full border border-amber-700 bg-red-200 text-red-700 px-2 py-1">Rejected</span>
case ws.Enabled && ws.Request == nil:
// enabled
<span class="ml-4 rounded-full border border-green-700 bg-green-200 text-green-700 px-2 py-1">Enabled</span>
case !ws.Enabled && ws.Request == nil:
// disabled
<span class="ml-4 rounded-full border border-amber-700 bg-red-200 text-red-700 px-2 py-1">Disabled</span>
case ws.Enabled && ws.Request != nil:
// enabled, changes requested
<span class="ml-4 rounded-full border border-green-700 bg-green-200 text-green-700 px-2 py-1">Enabled, pending request</span>
case !ws.Enabled && ws.Request != nil:
// disabled, changes requested
<span class="ml-4 rounded-full border border-amber-700 bg-red-200 text-red-700 px-2 py-1">Disabled, pending request</span>
}
}

Expand Down
65 changes: 38 additions & 27 deletions view/workspace_templ.go

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