Files
coder/codersdk/workspacesdk/workspaceagentconnwatch.go
T
Nick Vigilante 7301cee2c4 fix: list every enum value the API can return in the Swagger spec (#30122)
The API reference is generated from Swagger comments and struct tags,
and several `enums:"..."` tags narrowed a named enum type, so the docs
omitted values the server returns. For example, `User.status` listed
only `active` and `suspended`, not `dormant`, and
`TemplateVersion.warnings` listed `DEPRECATED_PARAMETERS`, which is not
a value of the type.

Remove 34 `enums` tags that narrow or duplicate a named type so the
referenced type's full value list is used. Tags that describe an
intentional subset stay: ACL role fields (which omit the `""` deleted
sentinel used only in update requests), `APIKey.scope`, and plain-string
fields. `Template.provisioner` keeps its tag because `.swaggo` replaces
`ProvisionerType` with `string`.

Also fix enum defects that break code generators:

- The provisioner jobs and daemons `status` query parameters listed
their enum twice (Swagger 2.0 requires unique `enum` items) or with the
wrong type. Both are now `[]codersdk.ProvisionerJobStatus` and
`[]codersdk.ProvisionerDaemonStatus`, matching what each handler parses,
including comma-separated values. The daemons route previously
documented job statuses, which the handler rejects with a 500.
- Replace the `_` zero placeholder of `WatchErrorCode` with `iota + 1`,
so the spec no longer lists 0 or `x-enum-varnames: ["_", ...]`. Wire
values are unchanged.

Where the narrowed tag hid values the server returns, the SDK type now
includes them: `LoginTypeOAuth2ProviderApp`, `BuildReasonAutodelete`,
and `MissingTemplateParameter` widen the generated TypeScript unions.
The dashboard attributes `autodelete` builds to Coder like other system
builds. `APIKey.login_type` and `AuthorizationCheck.action` set explicit
examples so the generated samples stay representative.

`TestSwaggerEnumsNotNarrowed` fails when a property's enum list drops
values of its named type (other than the 15 allowlisted intentional
subsets), lists a value the type lacks, or repeats a value.

Part of DOCS-1122, split from #30121.

---
🤖 Built with AI assistance.
2026-11-10 17:42:30 -05:00

87 lines
2.3 KiB
Go

package workspacesdk
import (
"context"
"fmt"
"github.com/google/uuid"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/wsjson"
"github.com/coder/websocket"
)
type WatchErrorCode int
const (
// Codes start at 1 so a zero Code means no code was set.
WatchErrorTooManyAgents WatchErrorCode = iota + 1
WatchErrorNameNotFound
WatchErrorNoAgents
WatchErrorServerShutdown
WatchErrorDatabase
WatchErrorInternal
)
type ConnectionWatchEvent struct {
Error *WatchError `json:"error"`
BuildUpdate *BuildUpdate `json:"build_update,omitempty"`
AgentUpdate *AgentUpdate `json:"agent_update,omitempty"`
}
type WatchError struct {
Code WatchErrorCode `json:"code"`
Retryable bool `json:"retryable"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
}
func (e *WatchError) Error() string {
if e.Details != "" {
return fmt.Sprintf("%s: %s", e.Message, e.Details)
}
return e.Message
}
type BuildUpdate struct {
Transition codersdk.WorkspaceTransition `json:"transition"`
JobStatus codersdk.ProvisionerJobStatus `json:"job_status"`
}
type AgentUpdate struct {
Lifecycle codersdk.WorkspaceAgentLifecycle `json:"lifecycle"`
ID uuid.UUID `json:"id" format:"uuid"`
}
func (c *Client) WorkspaceAgentConnectionWatch(
dialCtx context.Context, workspaceID uuid.UUID, agentName string,
) (
dec *wsjson.Decoder[ConnectionWatchEvent], err error,
) {
wsOptions := &websocket.DialOptions{
HTTPClient: c.client.HTTPClient,
// Need to disable compression to avoid a data-race.
CompressionMode: websocket.CompressionDisabled,
}
c.client.SessionTokenProvider.SetDialOption(wsOptions)
watchURL, err := c.client.URL.Parse(fmt.Sprintf("/api/v2/workspaces/%s/agent-connection-watch", workspaceID))
if err != nil {
return nil, xerrors.Errorf("parse url: %w", err)
}
if agentName != "" {
q := watchURL.Query()
q.Set("agent_name", agentName)
watchURL.RawQuery = q.Encode()
}
// nolint:bodyclose
conn, res, err := websocket.Dial(dialCtx, watchURL.String(), wsOptions)
if err != nil {
bodyErr := codersdk.ReadBodyAsError(res)
return nil, bodyErr
}
return wsjson.NewDecoder[ConnectionWatchEvent](conn, websocket.MessageText, c.client.Logger()), nil
}