fix: refresh entitlements after creating first user (#12285)

This commit is contained in:
Marcin Tojek
2024-02-23 17:48:24 +01:00
committed by GitHub
parent 2cb9bfd517
commit 90db6683c4
7 changed files with 64 additions and 15 deletions

View File

@ -203,6 +203,9 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
})
})
})
api.AGPL.RefreshEntitlements = func(ctx context.Context) error {
return api.refreshEntitlements(ctx)
}
api.AGPL.APIHandler.Group(func(r chi.Router) {
r.Get("/entitlements", api.serveEntitlements)

View File

@ -197,20 +197,10 @@ func (api *API) postRefreshEntitlements(rw http.ResponseWriter, r *http.Request)
return
}
err = api.updateEntitlements(ctx)
err = api.refreshEntitlements(ctx)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to update entitlements",
Detail: err.Error(),
})
return
}
err = api.Pubsub.Publish(PubsubEventLicenses, []byte("refresh"))
if err != nil {
api.Logger.Error(context.Background(), "failed to publish forced entitlement update", slog.Error(err))
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to publish forced entitlement update. Other replicas might not be updated.",
Message: "Failed to refresh entitlements",
Detail: err.Error(),
})
return
@ -221,6 +211,21 @@ func (api *API) postRefreshEntitlements(rw http.ResponseWriter, r *http.Request)
})
}
func (api *API) refreshEntitlements(ctx context.Context) error {
api.Logger.Info(ctx, "refresh entitlements now")
err := api.updateEntitlements(ctx)
if err != nil {
return xerrors.Errorf("failed to update entitlements: %w", err)
}
err = api.Pubsub.Publish(PubsubEventLicenses, []byte("refresh"))
if err != nil {
api.Logger.Error(ctx, "failed to publish forced entitlement update", slog.Error(err))
return xerrors.Errorf("failed to publish forced entitlement update, other replicas might not be updated: %w", err)
}
return nil
}
// @Summary Get licenses
// @ID get-licenses
// @Security CoderSessionToken

View File

@ -1,6 +1,7 @@
package coderd_test
import (
"context"
"net/http"
"testing"
"time"
@ -218,3 +219,21 @@ func TestUserQuietHours(t *testing.T) {
require.Contains(t, sdkErr.Message, "cannot set custom quiet hours schedule")
})
}
func TestCreateFirstUser_Entitlements_Trial(t *testing.T) {
t.Parallel()
adminClient, _ := coderdenttest.New(t, &coderdenttest.Options{
LicenseOptions: &coderdenttest.LicenseOptions{
Trial: true,
},
})
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()
//nolint:gocritic // we need the first user so admin
entitlements, err := adminClient.Entitlements(ctx)
require.NoError(t, err)
require.True(t, entitlements.Trial, "Trial license should be immediately active.")
}