mirror of
https://github.com/coder/coder.git
synced 2025-07-12 00:14:10 +00:00
feat: add dynamic parameters websocket endpoint (#17165)
This commit is contained in:
@ -21,6 +21,7 @@ import (
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/tracing"
|
||||
"github.com/coder/websocket"
|
||||
|
||||
"cdr.dev/slog"
|
||||
)
|
||||
@ -336,6 +337,38 @@ func (c *Client) Request(ctx context.Context, method, path string, body interfac
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (c *Client) Dial(ctx context.Context, path string, opts *websocket.DialOptions) (*websocket.Conn, error) {
|
||||
u, err := c.URL.Parse(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tokenHeader := c.SessionTokenHeader
|
||||
if tokenHeader == "" {
|
||||
tokenHeader = SessionTokenHeader
|
||||
}
|
||||
|
||||
if opts == nil {
|
||||
opts = &websocket.DialOptions{}
|
||||
}
|
||||
if opts.HTTPHeader == nil {
|
||||
opts.HTTPHeader = http.Header{}
|
||||
}
|
||||
if opts.HTTPHeader.Get("tokenHeader") == "" {
|
||||
opts.HTTPHeader.Set(tokenHeader, c.SessionToken())
|
||||
}
|
||||
|
||||
conn, resp, err := websocket.Dial(ctx, u.String(), opts)
|
||||
if resp.Body != nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// ExpectJSONMime is a helper function that will assert the content type
|
||||
// of the response is application/json.
|
||||
func ExpectJSONMime(res *http.Response) error {
|
||||
|
@ -9,6 +9,10 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/coder/coder/v2/codersdk/wsjson"
|
||||
previewtypes "github.com/coder/preview/types"
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
|
||||
type TemplateVersionWarning string
|
||||
@ -123,6 +127,28 @@ func (c *Client) CancelTemplateVersion(ctx context.Context, version uuid.UUID) e
|
||||
return nil
|
||||
}
|
||||
|
||||
type DynamicParametersRequest struct {
|
||||
// ID identifies the request. The response contains the same
|
||||
// ID so that the client can match it to the request.
|
||||
ID int `json:"id"`
|
||||
Inputs map[string]string `json:"inputs"`
|
||||
}
|
||||
|
||||
type DynamicParametersResponse struct {
|
||||
ID int `json:"id"`
|
||||
Diagnostics previewtypes.Diagnostics `json:"diagnostics"`
|
||||
Parameters []previewtypes.Parameter `json:"parameters"`
|
||||
// TODO: Workspace tags
|
||||
}
|
||||
|
||||
func (c *Client) TemplateVersionDynamicParameters(ctx context.Context, version uuid.UUID) (*wsjson.Stream[DynamicParametersResponse, DynamicParametersRequest], error) {
|
||||
conn, err := c.Dial(ctx, fmt.Sprintf("/api/v2/templateversions/%s/dynamic-parameters", version), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return wsjson.NewStream[DynamicParametersResponse, DynamicParametersRequest](conn, websocket.MessageText, websocket.MessageText, c.Logger()), nil
|
||||
}
|
||||
|
||||
// TemplateVersionParameters returns parameters a template version exposes.
|
||||
func (c *Client) TemplateVersionRichParameters(ctx context.Context, version uuid.UUID) ([]TemplateVersionParameter, error) {
|
||||
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/templateversions/%s/rich-parameters", version), nil)
|
||||
|
@ -18,9 +18,12 @@ type Decoder[T any] struct {
|
||||
logger slog.Logger
|
||||
}
|
||||
|
||||
// Chan starts the decoder reading from the websocket and returns a channel for reading the
|
||||
// resulting values. The chan T is closed if the underlying websocket is closed, or we encounter an
|
||||
// error. We also close the underlying websocket if we encounter an error reading or decoding.
|
||||
// Chan returns a `chan` that you can read incoming messages from. The returned
|
||||
// `chan` will be closed when the WebSocket connection is closed. If there is an
|
||||
// error reading from the WebSocket or decoding a value the WebSocket will be
|
||||
// closed.
|
||||
//
|
||||
// Safety: Chan must only be called once. Successive calls will panic.
|
||||
func (d *Decoder[T]) Chan() <-chan T {
|
||||
if !d.chanCalled.CompareAndSwap(false, true) {
|
||||
panic("chan called more than once")
|
||||
|
44
codersdk/wsjson/stream.go
Normal file
44
codersdk/wsjson/stream.go
Normal file
@ -0,0 +1,44 @@
|
||||
package wsjson
|
||||
|
||||
import (
|
||||
"cdr.dev/slog"
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
|
||||
// Stream is a two-way messaging interface over a WebSocket connection.
|
||||
type Stream[R any, W any] struct {
|
||||
conn *websocket.Conn
|
||||
r *Decoder[R]
|
||||
w *Encoder[W]
|
||||
}
|
||||
|
||||
func NewStream[R any, W any](conn *websocket.Conn, readType, writeType websocket.MessageType, logger slog.Logger) *Stream[R, W] {
|
||||
return &Stream[R, W]{
|
||||
conn: conn,
|
||||
r: NewDecoder[R](conn, readType, logger),
|
||||
// We intentionally don't call `NewEncoder` because it calls `CloseRead`.
|
||||
w: &Encoder[W]{conn: conn, typ: writeType},
|
||||
}
|
||||
}
|
||||
|
||||
// Chan returns a `chan` that you can read incoming messages from. The returned
|
||||
// `chan` will be closed when the WebSocket connection is closed. If there is an
|
||||
// error reading from the WebSocket or decoding a value the WebSocket will be
|
||||
// closed.
|
||||
//
|
||||
// Safety: Chan must only be called once. Successive calls will panic.
|
||||
func (s *Stream[R, W]) Chan() <-chan R {
|
||||
return s.r.Chan()
|
||||
}
|
||||
|
||||
func (s *Stream[R, W]) Send(v W) error {
|
||||
return s.w.Encode(v)
|
||||
}
|
||||
|
||||
func (s *Stream[R, W]) Close(c websocket.StatusCode) error {
|
||||
return s.conn.Close(c, "")
|
||||
}
|
||||
|
||||
func (s *Stream[R, W]) Drop() {
|
||||
_ = s.conn.Close(websocket.StatusInternalError, "dropping connection")
|
||||
}
|
Reference in New Issue
Block a user