Skip to content

Commit 5d8ca26

Browse files
committed
add agent names
1 parent 3926a66 commit 5d8ca26

File tree

10 files changed

+51
-12
lines changed

10 files changed

+51
-12
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11133,7 +11133,7 @@ func (q *FakeQuerier) GetAuthorizedWorkspacesAndAgents(ctx context.Context, prep
1113311133
return nil, xerrors.Errorf("get provisioner job: %w", err)
1113411134
}
1113511135

11136-
agentIDs := make([]uuid.UUID, 0)
11136+
outAgents := make([]database.AgentIDNamePair, 0)
1113711137
resources, err := q.getWorkspaceResourcesByJobIDNoLock(ctx, job.ID)
1113811138
if err != nil {
1113911139
return nil, xerrors.Errorf("get workspace resources: %w", err)
@@ -11144,7 +11144,10 @@ func (q *FakeQuerier) GetAuthorizedWorkspacesAndAgents(ctx context.Context, prep
1114411144
return nil, xerrors.Errorf("get workspace agents: %w", err)
1114511145
}
1114611146
for _, a := range agents {
11147-
agentIDs = append(agentIDs, a.ID)
11147+
outAgents = append(outAgents, database.AgentIDNamePair{
11148+
ID: a.ID,
11149+
Name: a.Name,
11150+
})
1114811151
}
1114911152
}
1115011153

@@ -11153,7 +11156,7 @@ func (q *FakeQuerier) GetAuthorizedWorkspacesAndAgents(ctx context.Context, prep
1115311156
WorkspaceName: w.Name,
1115411157
JobStatus: job.JobStatus,
1115511158
Transition: build.Transition,
11156-
AgentIds: agentIDs,
11159+
Agents: outAgents,
1115711160
})
1115811161
}
1115911162

coderd/database/dump.sql

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TYPE agent_id_name_pair;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TYPE agent_id_name_pair AS (
2+
id uuid,
3+
name text
4+
);

coderd/database/modelqueries.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspacesAndAgents(ctx context.Context, prepa
337337
&i.WorkspaceName,
338338
&i.JobStatus,
339339
&i.Transition,
340-
pq.Array(&i.AgentIds),
340+
pq.Array(&i.Agents),
341341
); err != nil {
342342
return nil, err
343343
}

coderd/database/querier_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,17 +691,17 @@ func TestGetAuthorizedWorkspacesAndAgents(t *testing.T) {
691691
for _, row := range ownerRows {
692692
switch row.WorkspaceID {
693693
case pendingID:
694-
require.Len(t, row.AgentIds, 1)
694+
require.Len(t, row.Agents, 1)
695695
require.Equal(t, database.ProvisionerJobStatusPending, row.JobStatus)
696696
case failedID:
697-
require.Len(t, row.AgentIds, 1)
697+
require.Len(t, row.Agents, 1)
698698
require.Equal(t, database.ProvisionerJobStatusFailed, row.JobStatus)
699699
case succeededID:
700-
require.Len(t, row.AgentIds, 2)
700+
require.Len(t, row.Agents, 2)
701701
require.Equal(t, database.ProvisionerJobStatusSucceeded, row.JobStatus)
702702
require.Equal(t, database.WorkspaceTransitionStart, row.Transition)
703703
case deletedID:
704-
require.Len(t, row.AgentIds, 0)
704+
require.Len(t, row.Agents, 0)
705705
require.Equal(t, database.ProvisionerJobStatusSucceeded, row.JobStatus)
706706
require.Equal(t, database.WorkspaceTransitionDelete, row.Transition)
707707
default:

coderd/database/queries.sql.go

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

coderd/database/queries/workspaces.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ SELECT
694694
workspaces.name as workspace_name,
695695
job_status,
696696
transition,
697-
(array_agg(agent_id) FILTER (WHERE agent_id IS NOT NULL))::uuid[] as agent_ids
697+
(array_agg(ROW(agent_id, agent_name)::agent_id_name_pair) FILTER (WHERE agent_id IS NOT NULL))::agent_id_name_pair[] as agents
698698
FROM workspaces
699699
LEFT JOIN LATERAL (
700700
SELECT
@@ -711,6 +711,7 @@ LEFT JOIN LATERAL (
711711
LEFT JOIN (
712712
SELECT
713713
workspace_agents.id as agent_id,
714+
workspace_agents.name as agent_name,
714715
job_id
715716
FROM workspace_resources
716717
JOIN workspace_agents ON workspace_agents.resource_id = workspace_resources.id
@@ -719,3 +720,4 @@ LEFT JOIN (
719720
-- @authorize_filter
720721
GROUP BY workspaces.id, workspaces.name, latest_build.job_status, latest_build.job_id, latest_build.transition;
721722

723+

coderd/database/sqlc.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ sql:
2828
emit_enum_valid_method: true
2929
emit_all_enum_values: true
3030
overrides:
31+
- db_type: "agent_id_name_pair"
32+
go_type:
33+
type: "AgentIDNamePair"
3134
# Used in 'CustomRoles' query to filter by (name,organization_id)
3235
- db_type: "name_organization_pair"
3336
go_type:

coderd/database/types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,23 @@ func (*NameOrganizationPair) Scan(_ interface{}) error {
174174
func (a NameOrganizationPair) Value() (driver.Value, error) {
175175
return fmt.Sprintf(`(%s,%s)`, a.Name, a.OrganizationID.String()), nil
176176
}
177+
178+
// AgentIDNamePair is used as a result tuple for workspace and agent rows.
179+
type AgentIDNamePair struct {
180+
ID uuid.UUID `db:"id" json:"id"`
181+
Name string `db:"name" json:"name"`
182+
}
183+
184+
func (p *AgentIDNamePair) Scan(src interface{}) error {
185+
switch v := src.(type) {
186+
case []byte:
187+
return json.Unmarshal(v, &p)
188+
case string:
189+
return json.Unmarshal([]byte(v), &p)
190+
}
191+
return xerrors.Errorf("unexpected type %T", src)
192+
}
193+
194+
func (p AgentIDNamePair) Value() (driver.Value, error) {
195+
return fmt.Sprintf(`(%s,%s)`, p.ID.String(), p.Name), nil
196+
}

0 commit comments

Comments
 (0)