mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
chore: add db query to retrieve workspaces & their agents (#14792)
Second PR for #14716. Adds a query that, given a user ID, returns all the workspaces they own, that can also be `ActionRead` by the requesting user. ``` type GetWorkspacesAndAgentsByOwnerIDRow struct { WorkspaceID uuid.UUID `db:"workspace_id" json:"workspace_id"` WorkspaceName string `db:"workspace_name" json:"workspace_name"` JobStatus ProvisionerJobStatus `db:"job_status" json:"job_status"` Transition WorkspaceTransition `db:"transition" json:"transition"` Agents []AgentIDNamePair `db:"agents" json:"agents"` } ``` `JobStatus` and `Transition` are set using the latest build/job of the workspace. Deleted workspaces are not included.
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@ -174,3 +175,35 @@ func (*NameOrganizationPair) Scan(_ interface{}) error {
|
||||
func (a NameOrganizationPair) Value() (driver.Value, error) {
|
||||
return fmt.Sprintf(`(%s,%s)`, a.Name, a.OrganizationID.String()), nil
|
||||
}
|
||||
|
||||
// AgentIDNamePair is used as a result tuple for workspace and agent rows.
|
||||
type AgentIDNamePair struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
Name string `db:"name" json:"name"`
|
||||
}
|
||||
|
||||
func (p *AgentIDNamePair) Scan(src interface{}) error {
|
||||
var v string
|
||||
switch a := src.(type) {
|
||||
case []byte:
|
||||
v = string(a)
|
||||
case string:
|
||||
v = a
|
||||
default:
|
||||
return xerrors.Errorf("unexpected type %T", src)
|
||||
}
|
||||
parts := strings.Split(strings.Trim(v, "()"), ",")
|
||||
if len(parts) != 2 {
|
||||
return xerrors.New("invalid format for AgentIDNamePair")
|
||||
}
|
||||
id, err := uuid.Parse(strings.TrimSpace(parts[0]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.ID, p.Name = id, strings.TrimSpace(parts[1])
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p AgentIDNamePair) Value() (driver.Value, error) {
|
||||
return fmt.Sprintf(`(%s,%s)`, p.ID.String(), p.Name), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user