8000 feat: add `dismissed` property to the healthcheck section by mtojek · Pull Request #10940 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: add dismissed property to the healthcheck section #10940

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 17 commits into from
Nov 29, 2023
Merged
Prev Previous commit
Next Next commit
Merge branch 'main' into 10712-api-health-settings-dismissed
  • Loading branch information
mtojek committed Nov 29, 2023
commit 55e610020938aba04cdedccd9d89bd61ade820a2
31 changes: 31 additions & 0 deletions coderd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/google/uuid"

"github.com/coder/coder/v2/coderd/audit"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/healthcheck"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
Expand Down Expand Up @@ -189,6 +193,33 @@ func (api *API) putDeploymentHealthSettings(rw http.ResponseWriter, r *http.Requ
return
}

currentSettingsJSON, err := api.Database.GetHealthSettings(r.Context())
if err != nil {
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to fetch current health settings.",
Detail: err.Error(),
})
return
}

if bytes.Equal(settingsJSON, []byte(currentSettingsJSON)) {
httpapi.Write(r.Context(), rw, http.StatusNotModified, nil)
return
}

auditor := api.Auditor.Load()
aReq, commitAudit := audit.InitRequest[database.HealthSettings](rw, &audit.RequestParams{
Audit: *auditor,
Log: api.Logger,
Request: r,
Action: database.AuditActionWrite,
})
defer commitAudit()
aReq.New = database.HealthSettings{
ID: uuid.New(),
DismissedHealthchecks: settings.DismissedHealthchecks,
}

err = api.Database.UpsertHealthSettings(ctx, string(settingsJSON))
if err != nil {
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
Expand Down
62 changes: 59 additions & 3 deletions coderd/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func TestHealthSettings(t *testing.T) {
require.Equal(t, codersdk.HealthSettings{DismissedHealthchecks: []string{}}, settings)
})

t.Run("Updated", func(t *testing.T) {
t.Run("DismissSection", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
Expand All @@ -268,15 +268,71 @@ func TestHealthSettings(t *testing.T) {
DismissedHealthchecks: []string{healthcheck.SectionDERP, healthcheck.SectionWebsocket},
}

// when
err := adminClient.UpdateHealthSettings(ctx, expected)
// when: dismiss "derp" and "websocket"
err := adminClient.PutHealthSettings(ctx, expected)
require.NoError(t, err)

// then
settings, err := adminClient.HealthSettings(ctx)
require.NoError(t, err)
require.Equal(t, expected, settings)
})

t.Run("UnDismissSection", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

// given
adminClient := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, adminClient)

initial := codersdk.HealthSettings{
DismissedHealthchecks: []string{healthcheck.SectionDERP, healthcheck.SectionWebsocket},
}

err := adminClient.PutHealthSettings(ctx, initial)
require.NoError(t, err)

expected := codersdk.HealthSettings{
DismissedHealthchecks: []string{healthcheck.SectionDERP},
}

// when: undismiss "websocket"
err = adminClient.PutHealthSettings(ctx, expected)
require.NoError(t, err)

// then
settings, err := adminClient.HealthSettings(ctx)
require.NoError(t, err)
require.Equal(t, expected, settings)
})

t.Run("NotModified", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

// given
adminClient := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, adminClient)

expected := codersdk.HealthSettings{
DismissedHealthchecks: []string{healthcheck.SectionDERP, healthcheck.SectionWebsocket},
}

err := adminClient.PutHealthSettings(ctx, expected)
require.NoError(t, err)

// when
err = adminClient.PutHealthSettings(ctx, expected)

// then
require.Error(t, err)
require.Contains(t, err.Error(), "health settings not modified")
})
}

func TestDebugWebsocket(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion codersdk/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"net/http"

"golang.org/x/xerrors"
)

type HealthSettings struct {
Expand All @@ -27,12 +29,16 @@ func (c *Client) HealthSettings(ctx context.Context) (HealthSettings, error) {
return settings, json.NewDecoder(res.Body).Decode(&settings)
}

func (c *Client) UpdateHealthSettings(ctx context.Context, settings HealthSettings) error {
func (c *Client) PutHealthSettings(ctx context.Context, settings HealthSettings) error {
res, err := c.Request(ctx, http.MethodPut, "/api/v2/debug/health/settings", settings)
if err != nil {
return err
}
defer res.Body.Close()

if res.StatusCode == http.StatusNotModified {
return xerrors.New("health settings not modified")
}
if res.StatusCode != http.StatusOK {
return ReadBodyAsError(res)
}
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0