From f4c1a3ea597f11f63fae96fdee3619f2aaaa98d6 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Thu, 17 Sep 2020 23:18:12 +0800 Subject: [PATCH] support mutli params --- pkg/apigateway/handler/resource.go | 12 +++++++++++- pkg/mcclient/modulebase/base.go | 14 ++++++++++++++ pkg/mcclient/modulebase/modules.go | 1 + pkg/mcclient/modulebase/resource.go | 10 ++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pkg/apigateway/handler/resource.go b/pkg/apigateway/handler/resource.go index 10b674cdad..fc9baa6031 100644 --- a/pkg/apigateway/handler/resource.go +++ b/pkg/apigateway/handler/resource.go @@ -488,7 +488,17 @@ func (f *ResourceHandlers) batchUpdateHandler(ctx context.Context, w http.Respon return } - ret := module.BatchUpdate(req.Session(), idlist, body) + var ret []modulebase.SubmitResult + if jsonutils.QueryBoolean(query, "batch_params", false) { + bodys, err := body.GetArray() + if err != nil { + httperrors.GeneralServerError(ctx, w, err) + return + } + ret = module.BatchParamsUpdate(req.Session(), idlist, bodys) + } else { + ret = module.BatchUpdate(req.Session(), idlist, body) + } w.WriteHeader(207) appsrv.SendJSON(w, modulebase.SubmitResults2JSON(ret)) } diff --git a/pkg/mcclient/modulebase/base.go b/pkg/mcclient/modulebase/base.go index 9d1ded2abd..b060345a96 100644 --- a/pkg/mcclient/modulebase/base.go +++ b/pkg/mcclient/modulebase/base.go @@ -304,6 +304,20 @@ func BatchDo(ids []string, do func(id string) (jsonutils.JSONObject, error)) []S return waitResults(results, len(ids)) } +func BatchParamsDo( + ids []string, params []jsonutils.JSONObject, + do func(id string, param jsonutils.JSONObject) (jsonutils.JSONObject, error), +) []SubmitResult { + results := make(chan SubmitResult, len(ids)) + for i := 0; i < len(ids); i++ { + go func(id string, param jsonutils.JSONObject) { + r, e := do(id, param) + addResult(results, id, r, e) + }(ids[i], params[i]) + } + return waitResults(results, len(ids)) +} + func BatchDoClassAction( batchParams []jsonutils.JSONObject, do func(jsonutils.JSONObject) (jsonutils.JSONObject, error), ) []SubmitResult { diff --git a/pkg/mcclient/modulebase/modules.go b/pkg/mcclient/modulebase/modules.go index 26e8ed2492..7494b11ca5 100644 --- a/pkg/mcclient/modulebase/modules.go +++ b/pkg/mcclient/modulebase/modules.go @@ -113,6 +113,7 @@ type Manager interface { PutInContext(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) PutInContexts(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) BatchUpdate(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject) []SubmitResult + BatchParamsUpdate(session *mcclient.ClientSession, idlist []string, params []jsonutils.JSONObject) []SubmitResult BatchPut(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject) []SubmitResult BatchPutInContext(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject, ctx Manager, ctxid string) []SubmitResult BatchPutInContexts(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject, ctxs []ManagerContext) []SubmitResult diff --git a/pkg/mcclient/modulebase/resource.go b/pkg/mcclient/modulebase/resource.go index 730aa691b1..1cfefe3f3c 100644 --- a/pkg/mcclient/modulebase/resource.go +++ b/pkg/mcclient/modulebase/resource.go @@ -453,6 +453,16 @@ func (this *ResourceManager) BatchPutInContexts(session *mcclient.ClientSession, }) } +func (this *ResourceManager) BatchParamsUpdate(session *mcclient.ClientSession, idlist []string, params []jsonutils.JSONObject) []SubmitResult { + return this.BatchParamsPutInContexts(session, idlist, params, nil) +} + +func (this *ResourceManager) BatchParamsPutInContexts(session *mcclient.ClientSession, idlist []string, params []jsonutils.JSONObject, ctxs []ManagerContext) []SubmitResult { + return BatchParamsDo(idlist, params, func(id string, param jsonutils.JSONObject) (jsonutils.JSONObject, error) { + return this.PutInContexts(session, id, param, ctxs) + }) +} + func (this *ResourceManager) Patch(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) { return this.PatchInContexts(session, id, params, nil) }