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)
| 167 | // @Success 201 {object} codersdk.Response |
| 168 | // @Router /api/v2/licenses/refresh-entitlements [post] |
| 169 | func (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 | |
| 213 | func (api *API) refreshEntitlements(ctx context.Context) error { |
| 214 | api.Logger.Info(ctx, "refresh entitlements now") |
nothing calls this directly
no test coverage detected