Skip to content

feat: add all safe experiments to the deployment page #10276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added new route for safe experiments
  • Loading branch information
Kira-Pilot committed Oct 16, 2023
commit 308dbb1bff3b689a3e1cdbe39de93707dde3cced
34 changes: 27 additions & 7 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 23 additions & 7 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ func New(options *Options) *API {
})
r.Route("/experiments", func(r chi.Router) {
r.Use(apiKeyMiddleware)
r.Get("/available", api.handleExperimentsSafe)
r.Get("/", api.handleExperimentsGet)
})
r.Get("/updatecheck", api.updateCheck)
Expand Down
26 changes: 16 additions & 10 deletions coderd/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ import (
"github.com/coder/coder/v2/codersdk"
)

// @Summary Get experiments
// @ID get-experiments
// @Summary Get enabled experiments
// @ID get-enabled-experiments
// @Security CoderSessionToken
// @Produce json
// @Tags General
// @Param include_all query bool false "All available experiments"
// @Success 200 {array} codersdk.Experiment
// @Router /experiments [get]
func (api *API) handleExperimentsGet(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
all := r.URL.Query().Has("include_all")

if !all {
httpapi.Write(ctx, rw, http.StatusOK, api.Experiments)
return
}
httpapi.Write(ctx, rw, http.StatusOK, api.Experiments)
}

httpapi.Write(ctx, rw, http.StatusOK, codersdk.ExperimentsAll)
// @Summary Get safe experiments
// @ID get-safe-experiments
// @Security CoderSessionToken
// @Produce json
// @Tags General
// @Success 200 {array} codersdk.Experiment
// @Router /experiments/available [get]
func (api *API) handleExperimentsSafe(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
httpapi.Write(ctx, rw, http.StatusOK, codersdk.AvailableExperiments{
Safe: codersdk.ExperimentsAll,
})
}
33 changes: 17 additions & 16 deletions codersdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2029,26 +2029,27 @@ func (e Experiments) Enabled(ex Experiment) bool {
return false
}

type ExperimentOptions struct {
// All signifies that all experiments - rather than just those that are enabled -
// should be returned
IncludeAll bool `json:"include_all,omitempty"`
func (c *Client) Experiments(ctx context.Context) (Experiments, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/experiments", nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, ReadBodyAsError(res)
}
var exp []Experiment
return exp, json.NewDecoder(res.Body).Decode(&exp)
}

// asRequestOption returns a function that can be used in (*Client).Request.
// It modifies the request query parameters.
func (o ExperimentOptions) asRequestOption() RequestOption {
return func(r *http.Request) {
q := r.URL.Query()
if o.IncludeAll {
q.Set("include_all", "true")
}
r.URL.RawQuery = q.Encode()
}
// AvailableExperiments is an expandable type that returns all safe experiments
// available to be used with a deployment.
type AvailableExperiments struct {
Safe []Experiment `json:"safe"`
}

func (c *Client) Experiments(ctx context.Context, opts ExperimentOptions) (Experiments, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/experiments", nil, opts.asRequestOption())
func (c *Client) SafeExperiments(ctx context.Context) (Experiments, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/experiments/available", nil)
if err != nil {
return nil, err
}
Expand Down
43 changes: 37 additions & 6 deletions docs/api/general.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.