Skip to content

Commit a8dd9a1

Browse files
committed
wip
1 parent 0f505de commit a8dd9a1

File tree

6 files changed

+43
-28
lines changed

6 files changed

+43
-28
lines changed

cli/provisionerjobs.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"fmt"
5+
"slices"
56

67
"golang.org/x/xerrors"
78

@@ -28,7 +29,7 @@ func (r *RootCmd) provisionerJobs() *serpent.Command {
2829

2930
func (r *RootCmd) provisionerJobsList() *serpent.Command {
3031
type provisionerJobRow struct {
31-
codersdk.ProvisionerJob `table:"provisioner_job,recursive_inline"`
32+
codersdk.ProvisionerJob `table:"provisioner_job,recursive_inline,nosort"`
3233
OrganizationName string `json:"organization_name" table:"organization"`
3334
Queue string `json:"-" table:"queue"`
3435
}
@@ -83,6 +84,11 @@ func (r *RootCmd) provisionerJobsList() *serpent.Command {
8384
}
8485
rows = append(rows, row)
8586
}
87+
// Sort manually because the cliui table truncates timestamps and
88+
// produces an unstable sort with timestamps that are all the same.
89+
slices.SortStableFunc(rows, func(a provisionerJobRow, b provisionerJobRow) int {
90+
return a.CreatedAt.Compare(b.CreatedAt)
91+
})
8692

8793
out, err := formatter.Format(ctx, rows)
8894
if err != nil {

cli/provisioners_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/coder/coder/v2/coderd/database/dbgen"
2222
"github.com/coder/coder/v2/coderd/database/dbtestutil"
2323
"github.com/coder/coder/v2/coderd/database/dbtime"
24+
"github.com/coder/coder/v2/coderd/rbac"
2425
"github.com/coder/coder/v2/codersdk"
2526
)
2627

@@ -69,7 +70,8 @@ func TestProvisioners_Golden(t *testing.T) {
6970
Pubsub: ps,
7071
})
7172
owner := coderdtest.CreateFirstUser(t, client)
72-
member, memberUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
73+
templateAdminClient, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.ScopedRoleOrgTemplateAdmin(owner.OrganizationID))
74+
memberClient, member := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
7375

7476
// Create initial resources with a running provisioner.
7577
firstProvisioner := coderdtest.NewProvisionerDaemon(t, coderdAPI)
@@ -78,7 +80,6 @@ func TestProvisioners_Golden(t *testing.T) {
7880
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
7981
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
8082

81-
time.Sleep(1500 * time.Millisecond) // Ensure the workspace build job has a different timestamp for sorting.
8283
workspace := coderdtest.CreateWorkspace(t, client, template.ID)
8384
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
8485

@@ -94,9 +95,10 @@ func TestProvisioners_Golden(t *testing.T) {
9495
Name: "provisioner-1",
9596
CreatedAt: dbtime.Now().Add(1 * time.Second),
9697
KeyID: uuid.MustParse(codersdk.ProvisionerKeyIDBuiltIn),
98+
Tags: database.StringMap{"owner": "", "scope": "organization", "foo": "bar"},
9799
})
98100
w1 := dbgen.Workspace(t, coderdAPI.Database, database.WorkspaceTable{
99-
OwnerID: memberUser.ID,
101+
OwnerID: member.ID,
100102
TemplateID: template.ID,
101103
})
102104
wb1ID := uuid.MustParse("00000000-0000-0000-dddd-000000000001")
@@ -105,7 +107,7 @@ func TestProvisioners_Golden(t *testing.T) {
105107
Input: json.RawMessage(`{"workspace_build_id":"` + wb1ID.String() + `"}`),
106108
CreatedAt: dbtime.Now().Add(2 * time.Second),
107109
StartedAt: sql.NullTime{Time: coderdAPI.Clock.Now(), Valid: true},
108-
Tags: database.StringMap{"owner": "", "scope": "organization"},
110+
Tags: database.StringMap{"owner": "", "scope": "organization", "foo": "bar"},
109111
})
110112
dbgen.WorkspaceBuild(t, coderdAPI.Database, database.WorkspaceBuild{
111113
ID: wb1ID,
@@ -120,9 +122,10 @@ func TestProvisioners_Golden(t *testing.T) {
120122
CreatedAt: dbtime.Now().Add(2 * time.Second),
121123
LastSeenAt: sql.NullTime{Time: coderdAPI.Clock.Now().Add(-time.Hour), Valid: true},
122124
KeyID: uuid.MustParse(codersdk.ProvisionerKeyIDBuiltIn),
125+
Tags: database.StringMap{"owner": "", "scope": "organization"},
123126
})
124127
w2 := dbgen.Workspace(t, coderdAPI.Database, database.WorkspaceTable{
125-
OwnerID: memberUser.ID,
128+
OwnerID: member.ID,
126129
TemplateID: template.ID,
127130
})
128131
wb2ID := uuid.MustParse("00000000-0000-0000-dddd-000000000002")
@@ -143,7 +146,7 @@ func TestProvisioners_Golden(t *testing.T) {
143146

144147
// Create a pending job.
145148
w3 := dbgen.Workspace(t, coderdAPI.Database, database.WorkspaceTable{
146-
OwnerID: memberUser.ID,
149+
OwnerID: member.ID,
147150
TemplateID: template.ID,
148151
})
149152
wb3ID := uuid.MustParse("00000000-0000-0000-dddd-000000000003")
@@ -164,6 +167,7 @@ func TestProvisioners_Golden(t *testing.T) {
164167
Name: "provisioner-3",
165168
CreatedAt: dbtime.Now().Add(3 * time.Second),
166169
KeyID: uuid.MustParse(codersdk.ProvisionerKeyIDBuiltIn),
170+
Tags: database.StringMap{"owner": "", "scope": "organization"},
167171
})
168172

169173
updateReplaceUUIDs(coderdAPI)
@@ -172,23 +176,28 @@ func TestProvisioners_Golden(t *testing.T) {
172176
t.Logf("replace[%q] = %q", id, replaceID)
173177
}
174178

179+
// Test provisioners list with member as members can access
180+
// provisioner daemons.
175181
t.Run("list", func(t *testing.T) {
176182
t.Parallel()
177183

178184
var got bytes.Buffer
179185
inv, root := clitest.New(t,
180186
"provisioners",
181187
"list",
182-
"--column", "id,created at,last seen at,name,version,api version,tags,status,current job id,previous job id,previous job status,organization",
188+
"--column", "id,created at,last seen at,name,version,tags,key name,status,current job id,current job status,previous job id,previous job status,organization",
183189
)
184190
inv.Stdout = &got
185-
clitest.SetupConfig(t, member, root)
191+
clitest.SetupConfig(t, memberClient, root)
186192
err := inv.Run()
187193
require.NoError(t, err)
188194

189195
clitest.TestGoldenFile(t, t.Name(), got.Bytes(), replace)
190196
})
191197

198+
// Test jobs list with template admin as members are currently
199+
// unable to access provisioner jobs. In the future (with RBAC
200+
// changes), we may allow them to view _their_ jobs.
192201
t.Run("jobs list", func(t *testing.T) {
193202
t.Parallel()
194203

@@ -200,7 +209,7 @@ func TestProvisioners_Golden(t *testing.T) {
200209
"--column", "id,created at,status,worker id,tags,template version id,workspace build id,type,available workers,organization,queue",
201210
)
202211
inv.Stdout = &got
203-
clitest.SetupConfig(t, member, root)
212+
clitest.SetupConfig(t, templateAdminClient, root)
204213
err := inv.Run()
205214
require.NoError(t, err)
206215

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
ID CREATED AT STATUS WORKER ID TAGS TEMPLATE VERSION ID WORKSPACE BUILD ID TYPE AVAILABLE WORKERS ORGANIZATION QUEUE
2-
00000000-0000-0000-bbbb-000000000000 ====[timestamp]===== succeeded 00000000-0000-0000-aaaa-000000000000 map[owner: scope:organization] 00000000-0000-0000-cccc-000000000000 <nil> template_version_import [] Coder
3-
00000000-0000-0000-bbbb-000000000001 ====[timestamp]===== succeeded 00000000-0000-0000-aaaa-000000000000 map[owner: scope:organization] <nil> 00000000-0000-0000-dddd-000000000000 workspace_build [] Coder
4-
00000000-0000-0000-bbbb-000000000002 ====[timestamp]===== running 00000000-0000-0000-aaaa-000000000001 map[00000000-0000-0000-bbbb-000000000002:true owner: scope:organization] <nil> 00000000-0000-0000-dddd-000000000001 workspace_build [] Coder
5-
00000000-0000-0000-bbbb-000000000003 ====[timestamp]===== succeeded 00000000-0000-0000-aaaa-000000000002 map[00000000-0000-0000-bbbb-000000000003:true owner: scope:organization] <nil> 00000000-0000-0000-dddd-000000000002 workspace_build [] Coder
6-
00000000-0000-0000-bbbb-000000000004 ====[timestamp]===== pending <nil> map[owner: scope:organization] <nil> 00000000-0000-0000-dddd-000000000003 workspace_build [00000000-0000-0000-aaaa-000000000000] Coder 1/1
1+
ID CREATED AT STATUS WORKER ID TAGS TEMPLATE VERSION ID WORKSPACE BUILD ID TYPE AVAILABLE WORKERS ORGANIZATION QUEUE
2+
00000000-0000-0000-bbbb-000000000000 ====[timestamp]===== succeeded 00000000-0000-0000-aaaa-000000000000 map[owner: scope:organization] 00000000-0000-0000-cccc-000000000000 <nil> template_version_import [] Coder
3+
00000000-0000-0000-bbbb-000000000001 ====[timestamp]===== succeeded 00000000-0000-0000-aaaa-000000000000 map[owner: scope:organization] <nil> 00000000-0000-0000-dddd-000000000000 workspace_build [] Coder
4+
00000000-0000-0000-bbbb-000000000002 ====[timestamp]===== running 00000000-0000-0000-aaaa-000000000001 map[00000000-0000-0000-bbbb-000000000002:true foo:bar owner: scope:organization] <nil> 00000000-0000-0000-dddd-000000000001 workspace_build [] Coder
5+
00000000-0000-0000-bbbb-000000000003 ====[timestamp]===== succeeded 00000000-0000-0000-aaaa-000000000002 map[00000000-0000-0000-bbbb-000000000003:true owner: scope:organization] <nil> 00000000-0000-0000-dddd-000000000002 workspace_build [] Coder
6+
00000000-0000-0000-bbbb-000000000004 ====[timestamp]===== pending <nil> map[owner: scope:organization] <nil> 00000000-0000-0000-dddd-000000000003 workspace_build [00000000-0000-0000-aaaa-000000000000, 00000000-0000-0000-aaaa-000000000002, 00000000-0000-0000-aaaa-000000000003] Coder 1/1
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
ID CREATED AT LAST SEEN AT NAME VERSION API VERSION TAGS STATUS CURRENT JOB ID PREVIOUS JOB ID PREVIOUS JOB STATUS ORGANIZATION
2-
00000000-0000-0000-aaaa-000000000001 ====[timestamp]===== ====[timestamp]===== provisioner-1 v0.0.0 1.1 map[] busy 00000000-0000-0000-bbbb-000000000002 <nil> <nil> Coder
3-
00000000-0000-0000-aaaa-000000000002 ====[timestamp]===== ====[timestamp]===== provisioner-2 v0.0.0 1.1 map[] offline <nil> 00000000-0000-0000-bbbb-000000000003 succeeded Coder
4-
00000000-0000-0000-aaaa-000000000003 ====[timestamp]===== ====[timestamp]===== provisioner-3 v0.0.0 1.1 map[] idle <nil> <nil> <nil> Coder
5-
00000000-0000-0000-aaaa-000000000000 ====[timestamp]===== ====[timestamp]===== test v0.0.0-devel 1.2 map[owner: scope:organization] idle <nil> 00000000-0000-0000-bbbb-000000000001 succeeded Coder
1+
ID CREATED AT LAST SEEN AT NAME VERSION TAGS KEY NAME STATUS CURRENT JOB ID CURRENT JOB STATUS PREVIOUS JOB ID PREVIOUS JOB STATUS ORGANIZATION
2+
00000000-0000-0000-aaaa-000000000001 ====[timestamp]===== ====[timestamp]===== provisioner-1 v0.0.0 map[foo:bar owner: scope:organization] built-in busy 00000000-0000-0000-bbbb-000000000002 running <nil> <nil> Coder
3+
00000000-0000-0000-aaaa-000000000002 ====[timestamp]===== ====[timestamp]===== provisioner-2 v0.0.0 map[owner: scope:organization] built-in offline <nil> <nil> 00000000-0000-0000-bbbb-000000000003 succeeded Coder
4+
00000000-0000-0000-aaaa-000000000003 ====[timestamp]===== ====[timestamp]===== provisioner-3 v0.0.0 map[owner: scope:organization] built-in idle <nil> <nil> <nil> <nil> Coder
5+
00000000-0000-0000-aaaa-000000000000 ====[timestamp]===== ====[timestamp]===== test v0.0.0-devel map[owner: scope:organization] built-in idle <nil> <nil> 00000000-0000-0000-bbbb-000000000001 succeeded Coder

cli/testdata/coder_provisioner_jobs_list_--output_json.golden

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"id": "======[workspace build job ID]======",
3+
"id": "==========[version job ID]==========",
44
"created_at": "====[timestamp]=====",
55
"started_at": "====[timestamp]=====",
66
"completed_at": "====[timestamp]=====",
@@ -15,13 +15,13 @@
1515
"queue_size": 0,
1616
"organization_id": "===========[first org ID]===========",
1717
"input": {
18-
"workspace_build_id": "========[workspace build ID]========"
18+
"template_version_id": "============[version ID]============"
1919
},
20-
"type": "workspace_build",
20+
"type": "template_version_import",
2121
"organization_name": "Coder"
2222
},
2323
{
24-
"id": "==========[version job ID]==========",
24+
"id": "======[workspace build job ID]======",
2525
"created_at": "====[timestamp]=====",
2626
"started_at": "====[timestamp]=====",
2727
"completed_at": "====[timestamp]=====",
@@ -36,9 +36,9 @@
3636
"queue_size": 0,
3737
"organization_id": "===========[first org ID]===========",
3838
"input": {
39-
"template_version_id": "============[version ID]============"
39+
"workspace_build_id": "========[workspace build ID]========"
4040
},
41-
"type": "template_version_import",
41+
"type": "workspace_build",
4242
"organization_name": "Coder"
4343
}
4444
]

codersdk/provisionerdaemons.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func JobIsMissingParameterErrorCode(code JobErrorCode) bool {
156156
// ProvisionerJob describes the job executed by the provisioning daemon.
157157
type ProvisionerJob struct {
158158
ID uuid.UUID `json:"id" format:"uuid" table:"id"`
159-
CreatedAt time.Time `json:"created_at" format:"date-time" table:"created at,default_sort"`
159+
CreatedAt time.Time `json:"created_at" format:"date-time" table:"created at"`
160160
StartedAt *time.Time `json:"started_at,omitempty" format:"date-time" table:"started at"`
161161
CompletedAt *time.Time `json:"completed_at,omitempty" format:"date-time" table:"completed at"`
162162
CanceledAt *time.Time `json:"canceled_at,omitempty" format:"date-time" table:"canceled at"`

0 commit comments

Comments
 (0)