From 581bb8833e73b4fe800cd8bb54024d89412ab328 Mon Sep 17 00:00:00 2001 From: Wang Yan Date: Wed, 1 Sep 2021 10:49:00 +0800 Subject: [PATCH] remove the internal legacy API to switch quota The init design of this API is to avoid the quota error leads to system disaster. As quota has been refineded and redis lock has been removed, the API can be deprecated safely. And this API is only call the DB to refresh quota data, user can call the SyncQuota API to handle this. Signed-off-by: Wang Yan --- src/core/api/harborapi_test.go | 1 - src/core/api/internal.go | 34 -------------------------------- src/core/api/internal_test.go | 36 ---------------------------------- src/server/route.go | 1 - 4 files changed, 72 deletions(-) diff --git a/src/core/api/harborapi_test.go b/src/core/api/harborapi_test.go index ed67e8577..ec9569b9d 100644 --- a/src/core/api/harborapi_test.go +++ b/src/core/api/harborapi_test.go @@ -115,7 +115,6 @@ func init() { beego.Router("/api/"+api.APIVersion+"/chartrepo/:repo/charts/:name/:version/labels", chartLabelAPIType, "get:GetLabels;post:MarkLabel") beego.Router("/api/"+api.APIVersion+"/chartrepo/:repo/charts/:name/:version/labels/:id([0-9]+)", chartLabelAPIType, "delete:RemoveLabel") - beego.Router("/api/internal/switchquota", &InternalAPI{}, "put:SwitchQuota") beego.Router("/api/internal/syncquota", &InternalAPI{}, "post:SyncQuota") // Init user Info diff --git a/src/core/api/internal.go b/src/core/api/internal.go index 6d6cd6518..12ab4c19c 100644 --- a/src/core/api/internal.go +++ b/src/core/api/internal.go @@ -69,40 +69,6 @@ func (ia *InternalAPI) RenameAdmin() { ia.DestroySession() } -// QuotaSwitcher ... -type QuotaSwitcher struct { - Enabled bool -} - -// SwitchQuota ... -func (ia *InternalAPI) SwitchQuota() { - var req QuotaSwitcher - if err := ia.DecodeJSONReq(&req); err != nil { - ia.SendBadRequestError(err) - return - } - ctx := orm.NewContext(ia.Ctx.Request.Context(), o.NewOrm()) - cur := config.ReadOnly(ctx) - // quota per project from disable to enable, it needs to update the quota usage bases on the DB records. - if !config.QuotaPerProjectEnable(ctx) && req.Enabled { - if !cur { - config.GetCfgManager(ctx).Set(ctx, common.ReadOnly, true) - config.GetCfgManager(ctx).Save(ctx) - } - if err := quota.RefreshForProjects(ctx); err != nil { - ia.SendInternalServerError(err) - return - } - } - defer func() { - ctx := orm.Context() - config.GetCfgManager(ctx).Set(ctx, common.ReadOnly, cur) - config.GetCfgManager(ctx).Set(ctx, common.QuotaPerProjectEnable, req.Enabled) - config.GetCfgManager(ctx).Save(ctx) - }() - return -} - // SyncQuota ... func (ia *InternalAPI) SyncQuota() { if !config.QuotaPerProjectEnable(orm.Context()) { diff --git a/src/core/api/internal_test.go b/src/core/api/internal_test.go index 02903a98b..2ca101d21 100644 --- a/src/core/api/internal_test.go +++ b/src/core/api/internal_test.go @@ -19,42 +19,6 @@ import ( "testing" ) -// cannot verify the real scenario here -func TestSwitchQuota(t *testing.T) { - cases := []*codeCheckingCase{ - // 401 - { - request: &testingRequest{ - method: http.MethodPut, - url: "/api/internal/switchquota", - }, - code: http.StatusUnauthorized, - }, - // 200 - { - request: &testingRequest{ - method: http.MethodPut, - url: "/api/internal/switchquota", - credential: sysAdmin, - bodyJSON: &QuotaSwitcher{ - Enabled: true, - }, - }, - code: http.StatusOK, - }, - // 403 - { - request: &testingRequest{ - url: "/api/internal/switchquota", - method: http.MethodPut, - credential: nonSysAdmin, - }, - code: http.StatusForbidden, - }, - } - runCodeCheckingCases(t, cases...) -} - // cannot verify the real scenario here func TestSyncQuota(t *testing.T) { cases := []*codeCheckingCase{ diff --git a/src/server/route.go b/src/server/route.go index 5e73943d4..7ee8c6817 100644 --- a/src/server/route.go +++ b/src/server/route.go @@ -46,7 +46,6 @@ func registerRoutes() { beego.Router(common.AuthProxyRediretPath, &controllers.AuthProxyController{}, "get:HandleRedirect") beego.Router("/api/internal/renameadmin", &api.InternalAPI{}, "post:RenameAdmin") - beego.Router("/api/internal/switchquota", &api.InternalAPI{}, "put:SwitchQuota") beego.Router("/api/internal/syncquota", &api.InternalAPI{}, "post:SyncQuota") beego.Router("/service/notifications/jobs/webhook/:id([0-9]+)", &jobs.Handler{}, "post:HandleNotificationJob")