Implement email/password auth with JWT sessions and API key auth for sandbox lifecycle. Users get a default team on signup; sandboxes, snapshots, and API keys are scoped to teams. - Add user, team, users_teams, and team_api_keys tables (goose migrations) - Add JWT middleware (Bearer token) for user management endpoints - Add API key middleware (X-API-Key header, SHA-256 hashed) for sandbox ops - Add signup/login handlers with transactional user+team creation - Add API key CRUD endpoints (create/list/delete) - Replace owner_id with team_id on sandboxes and templates - Update all handlers to use team-scoped queries - Add godotenv for .env file loading - Update OpenAPI spec and test UI with auth flows
357 lines
8.3 KiB
Go
357 lines
8.3 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: sandboxes.sql
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const bulkUpdateStatusByIDs = `-- name: BulkUpdateStatusByIDs :exec
|
|
UPDATE sandboxes
|
|
SET status = $2,
|
|
last_updated = NOW()
|
|
WHERE id = ANY($1::text[])
|
|
`
|
|
|
|
type BulkUpdateStatusByIDsParams struct {
|
|
Column1 []string `json:"column_1"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
func (q *Queries) BulkUpdateStatusByIDs(ctx context.Context, arg BulkUpdateStatusByIDsParams) error {
|
|
_, err := q.db.Exec(ctx, bulkUpdateStatusByIDs, arg.Column1, arg.Status)
|
|
return err
|
|
}
|
|
|
|
const getSandbox = `-- name: GetSandbox :one
|
|
SELECT id, host_id, template, status, vcpus, memory_mb, timeout_sec, guest_ip, host_ip, created_at, started_at, last_active_at, last_updated, team_id FROM sandboxes WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetSandbox(ctx context.Context, id string) (Sandbox, error) {
|
|
row := q.db.QueryRow(ctx, getSandbox, id)
|
|
var i Sandbox
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.HostID,
|
|
&i.Template,
|
|
&i.Status,
|
|
&i.Vcpus,
|
|
&i.MemoryMb,
|
|
&i.TimeoutSec,
|
|
&i.GuestIp,
|
|
&i.HostIp,
|
|
&i.CreatedAt,
|
|
&i.StartedAt,
|
|
&i.LastActiveAt,
|
|
&i.LastUpdated,
|
|
&i.TeamID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getSandboxByTeam = `-- name: GetSandboxByTeam :one
|
|
SELECT id, host_id, template, status, vcpus, memory_mb, timeout_sec, guest_ip, host_ip, created_at, started_at, last_active_at, last_updated, team_id FROM sandboxes WHERE id = $1 AND team_id = $2
|
|
`
|
|
|
|
type GetSandboxByTeamParams struct {
|
|
ID string `json:"id"`
|
|
TeamID string `json:"team_id"`
|
|
}
|
|
|
|
func (q *Queries) GetSandboxByTeam(ctx context.Context, arg GetSandboxByTeamParams) (Sandbox, error) {
|
|
row := q.db.QueryRow(ctx, getSandboxByTeam, arg.ID, arg.TeamID)
|
|
var i Sandbox
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.HostID,
|
|
&i.Template,
|
|
&i.Status,
|
|
&i.Vcpus,
|
|
&i.MemoryMb,
|
|
&i.TimeoutSec,
|
|
&i.GuestIp,
|
|
&i.HostIp,
|
|
&i.CreatedAt,
|
|
&i.StartedAt,
|
|
&i.LastActiveAt,
|
|
&i.LastUpdated,
|
|
&i.TeamID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const insertSandbox = `-- name: InsertSandbox :one
|
|
INSERT INTO sandboxes (id, team_id, host_id, template, status, vcpus, memory_mb, timeout_sec)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
|
RETURNING id, host_id, template, status, vcpus, memory_mb, timeout_sec, guest_ip, host_ip, created_at, started_at, last_active_at, last_updated, team_id
|
|
`
|
|
|
|
type InsertSandboxParams struct {
|
|
ID string `json:"id"`
|
|
TeamID string `json:"team_id"`
|
|
HostID string `json:"host_id"`
|
|
Template string `json:"template"`
|
|
Status string `json:"status"`
|
|
Vcpus int32 `json:"vcpus"`
|
|
MemoryMb int32 `json:"memory_mb"`
|
|
TimeoutSec int32 `json:"timeout_sec"`
|
|
}
|
|
|
|
func (q *Queries) InsertSandbox(ctx context.Context, arg InsertSandboxParams) (Sandbox, error) {
|
|
row := q.db.QueryRow(ctx, insertSandbox,
|
|
arg.ID,
|
|
arg.TeamID,
|
|
arg.HostID,
|
|
arg.Template,
|
|
arg.Status,
|
|
arg.Vcpus,
|
|
arg.MemoryMb,
|
|
arg.TimeoutSec,
|
|
)
|
|
var i Sandbox
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.HostID,
|
|
&i.Template,
|
|
&i.Status,
|
|
&i.Vcpus,
|
|
&i.MemoryMb,
|
|
&i.TimeoutSec,
|
|
&i.GuestIp,
|
|
&i.HostIp,
|
|
&i.CreatedAt,
|
|
&i.StartedAt,
|
|
&i.LastActiveAt,
|
|
&i.LastUpdated,
|
|
&i.TeamID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listSandboxes = `-- name: ListSandboxes :many
|
|
SELECT id, host_id, template, status, vcpus, memory_mb, timeout_sec, guest_ip, host_ip, created_at, started_at, last_active_at, last_updated, team_id FROM sandboxes ORDER BY created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListSandboxes(ctx context.Context) ([]Sandbox, error) {
|
|
rows, err := q.db.Query(ctx, listSandboxes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Sandbox
|
|
for rows.Next() {
|
|
var i Sandbox
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.HostID,
|
|
&i.Template,
|
|
&i.Status,
|
|
&i.Vcpus,
|
|
&i.MemoryMb,
|
|
&i.TimeoutSec,
|
|
&i.GuestIp,
|
|
&i.HostIp,
|
|
&i.CreatedAt,
|
|
&i.StartedAt,
|
|
&i.LastActiveAt,
|
|
&i.LastUpdated,
|
|
&i.TeamID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listSandboxesByHostAndStatus = `-- name: ListSandboxesByHostAndStatus :many
|
|
SELECT id, host_id, template, status, vcpus, memory_mb, timeout_sec, guest_ip, host_ip, created_at, started_at, last_active_at, last_updated, team_id FROM sandboxes
|
|
WHERE host_id = $1 AND status = ANY($2::text[])
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
type ListSandboxesByHostAndStatusParams struct {
|
|
HostID string `json:"host_id"`
|
|
Column2 []string `json:"column_2"`
|
|
}
|
|
|
|
func (q *Queries) ListSandboxesByHostAndStatus(ctx context.Context, arg ListSandboxesByHostAndStatusParams) ([]Sandbox, error) {
|
|
rows, err := q.db.Query(ctx, listSandboxesByHostAndStatus, arg.HostID, arg.Column2)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Sandbox
|
|
for rows.Next() {
|
|
var i Sandbox
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.HostID,
|
|
&i.Template,
|
|
&i.Status,
|
|
&i.Vcpus,
|
|
&i.MemoryMb,
|
|
&i.TimeoutSec,
|
|
&i.GuestIp,
|
|
&i.HostIp,
|
|
&i.CreatedAt,
|
|
&i.StartedAt,
|
|
&i.LastActiveAt,
|
|
&i.LastUpdated,
|
|
&i.TeamID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listSandboxesByTeam = `-- name: ListSandboxesByTeam :many
|
|
SELECT id, host_id, template, status, vcpus, memory_mb, timeout_sec, guest_ip, host_ip, created_at, started_at, last_active_at, last_updated, team_id FROM sandboxes WHERE team_id = $1 ORDER BY created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListSandboxesByTeam(ctx context.Context, teamID string) ([]Sandbox, error) {
|
|
rows, err := q.db.Query(ctx, listSandboxesByTeam, teamID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Sandbox
|
|
for rows.Next() {
|
|
var i Sandbox
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.HostID,
|
|
&i.Template,
|
|
&i.Status,
|
|
&i.Vcpus,
|
|
&i.MemoryMb,
|
|
&i.TimeoutSec,
|
|
&i.GuestIp,
|
|
&i.HostIp,
|
|
&i.CreatedAt,
|
|
&i.StartedAt,
|
|
&i.LastActiveAt,
|
|
&i.LastUpdated,
|
|
&i.TeamID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const updateLastActive = `-- name: UpdateLastActive :exec
|
|
UPDATE sandboxes
|
|
SET last_active_at = $2,
|
|
last_updated = NOW()
|
|
WHERE id = $1
|
|
`
|
|
|
|
type UpdateLastActiveParams struct {
|
|
ID string `json:"id"`
|
|
LastActiveAt pgtype.Timestamptz `json:"last_active_at"`
|
|
}
|
|
|
|
func (q *Queries) UpdateLastActive(ctx context.Context, arg UpdateLastActiveParams) error {
|
|
_, err := q.db.Exec(ctx, updateLastActive, arg.ID, arg.LastActiveAt)
|
|
return err
|
|
}
|
|
|
|
const updateSandboxRunning = `-- name: UpdateSandboxRunning :one
|
|
UPDATE sandboxes
|
|
SET status = 'running',
|
|
host_ip = $2,
|
|
guest_ip = $3,
|
|
started_at = $4,
|
|
last_active_at = $4,
|
|
last_updated = NOW()
|
|
WHERE id = $1
|
|
RETURNING id, host_id, template, status, vcpus, memory_mb, timeout_sec, guest_ip, host_ip, created_at, started_at, last_active_at, last_updated, team_id
|
|
`
|
|
|
|
type UpdateSandboxRunningParams struct {
|
|
ID string `json:"id"`
|
|
HostIp string `json:"host_ip"`
|
|
GuestIp string `json:"guest_ip"`
|
|
StartedAt pgtype.Timestamptz `json:"started_at"`
|
|
}
|
|
|
|
func (q *Queries) UpdateSandboxRunning(ctx context.Context, arg UpdateSandboxRunningParams) (Sandbox, error) {
|
|
row := q.db.QueryRow(ctx, updateSandboxRunning,
|
|
arg.ID,
|
|
arg.HostIp,
|
|
arg.GuestIp,
|
|
arg.StartedAt,
|
|
)
|
|
var i Sandbox
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.HostID,
|
|
&i.Template,
|
|
&i.Status,
|
|
&i.Vcpus,
|
|
&i.MemoryMb,
|
|
&i.TimeoutSec,
|
|
&i.GuestIp,
|
|
&i.HostIp,
|
|
&i.CreatedAt,
|
|
&i.StartedAt,
|
|
&i.LastActiveAt,
|
|
&i.LastUpdated,
|
|
&i.TeamID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updateSandboxStatus = `-- name: UpdateSandboxStatus :one
|
|
UPDATE sandboxes
|
|
SET status = $2,
|
|
last_updated = NOW()
|
|
WHERE id = $1
|
|
RETURNING id, host_id, template, status, vcpus, memory_mb, timeout_sec, guest_ip, host_ip, created_at, started_at, last_active_at, last_updated, team_id
|
|
`
|
|
|
|
type UpdateSandboxStatusParams struct {
|
|
ID string `json:"id"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
func (q *Queries) UpdateSandboxStatus(ctx context.Context, arg UpdateSandboxStatusParams) (Sandbox, error) {
|
|
row := q.db.QueryRow(ctx, updateSandboxStatus, arg.ID, arg.Status)
|
|
var i Sandbox
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.HostID,
|
|
&i.Template,
|
|
&i.Status,
|
|
&i.Vcpus,
|
|
&i.MemoryMb,
|
|
&i.TimeoutSec,
|
|
&i.GuestIp,
|
|
&i.HostIp,
|
|
&i.CreatedAt,
|
|
&i.StartedAt,
|
|
&i.LastActiveAt,
|
|
&i.LastUpdated,
|
|
&i.TeamID,
|
|
)
|
|
return i, err
|
|
}
|