MCPcopy Index your code
hub / github.com/coder/coder / postRefreshEntitlements

Method postRefreshEntitlements

enterprise/coderd/licenses.go:169–211  ·  view source on GitHub ↗

postRefreshEntitlements forces an `updateEntitlements` call and publishes a message to the PubsubEventLicenses topic to force other replicas to update their entitlements. Updates happen automatically on a timer, however that time is every 10 minutes, and we want to be able to force an update immedia

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

167// @Success 201 {object} codersdk.Response
168// @Router /api/v2/licenses/refresh-entitlements [post]
169func (api *API) postRefreshEntitlements(rw http.ResponseWriter, r *http.Request) {
170 ctx := r.Context()
171
172 // If the user cannot create a new license, then they cannot refresh entitlements.
173 // Refreshing entitlements is a way to force a refresh of the license, so it is
174 // equivalent to creating a new license.
175 if !api.AGPL.Authorize(r, policy.ActionCreate, rbac.ResourceLicense) {
176 httpapi.Forbidden(rw)
177 return
178 }
179
180 // Prevent abuse by limiting how often we allow a forced refresh.
181 now := time.Now()
182 if ok, wait := api.Entitlements.AllowRefresh(now); !ok {
183 rw.Header().Set("Retry-After", strconv.Itoa(int(wait.Seconds())))
184 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
185 Message: fmt.Sprintf("Entitlements already recently refreshed, please wait %d seconds to force a new refresh", int(wait.Seconds())),
186 })
187 return
188 }
189
190 err := api.replicaManager.UpdateNow(ctx)
191 if err != nil {
192 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
193 Message: "Failed to sync replicas",
194 Detail: err.Error(),
195 })
196 return
197 }
198
199 err = api.refreshEntitlements(ctx)
200 if err != nil {
201 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
202 Message: "Failed to refresh entitlements",
203 Detail: err.Error(),
204 })
205 return
206 }
207
208 httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{
209 Message: "Entitlements updated",
210 })
211}
212
213func (api *API) refreshEntitlements(ctx context.Context) error {
214 api.Logger.Info(ctx, "refresh entitlements now")

Callers

nothing calls this directly

Calls 10

refreshEntitlementsMethod · 0.95
ForbiddenFunction · 0.92
WriteFunction · 0.92
AllowRefreshMethod · 0.80
UpdateNowMethod · 0.80
ContextMethod · 0.65
AuthorizeMethod · 0.65
SetMethod · 0.65
HeaderMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected