From 24295785bd21747318f74949d2e1fa7775e5d83b Mon Sep 17 00:00:00 2001 From: yangk Date: Mon, 9 Nov 2020 15:48:35 +0800 Subject: [PATCH 1/4] added batch create applications --- api/api/api_interface.go | 1 + api/api_routers/version2/v2Routers.go | 1 + api/controller/application.go | 29 +++++++++++++++++++++++++++ api/handler/application_handler.go | 8 +------- api/model/model.go | 18 ++++++++++++++--- 5 files changed, 47 insertions(+), 10 deletions(-) diff --git a/api/api/api_interface.go b/api/api/api_interface.go index 5461aafa3..b25ac41c4 100644 --- a/api/api/api_interface.go +++ b/api/api/api_interface.go @@ -154,6 +154,7 @@ type AppInterface interface { // ApplicationInterface tenant application interface type ApplicationInterface interface { CreateApp(w http.ResponseWriter, r *http.Request) + BatchCreateApp(w http.ResponseWriter, r *http.Request) UpdateApp(w http.ResponseWriter, r *http.Request) ListApps(w http.ResponseWriter, r *http.Request) ListServices(w http.ResponseWriter, r *http.Request) diff --git a/api/api_routers/version2/v2Routers.go b/api/api_routers/version2/v2Routers.go index 5ba7e981d..0277a737e 100644 --- a/api/api_routers/version2/v2Routers.go +++ b/api/api_routers/version2/v2Routers.go @@ -137,6 +137,7 @@ func (v2 *V2) tenantNameRouter() chi.Router { r.Get("/chargesverify", controller.ChargesVerifyController) //tenant app r.Post("/apps", controller.GetManager().CreateApp) + r.Post("/batch_create_apps", controller.GetManager().BatchCreateApp) r.Get("/apps", controller.GetManager().ListApps) r.Mount("/apps/{app_id}", v2.applicationRouter()) //get some service pod info diff --git a/api/controller/application.go b/api/controller/application.go index 400825b4d..bd5b6e29f 100644 --- a/api/controller/application.go +++ b/api/controller/application.go @@ -10,6 +10,7 @@ import ( "github.com/goodrain/rainbond/api/model" dbmodel "github.com/goodrain/rainbond/db/model" httputil "github.com/goodrain/rainbond/util/http" + "github.com/sirupsen/logrus" ) // ApplicationController - @@ -36,6 +37,34 @@ func (a *ApplicationController) CreateApp(w http.ResponseWriter, r *http.Request httputil.ReturnSuccess(r, w, app) } +// BatchCreateApp - +func (a *ApplicationController) BatchCreateApp(w http.ResponseWriter, r *http.Request) { + var ( + apps model.CreateAppRequest + resp model.CreateAppResponse + respList []model.CreateAppResponse + ) + if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &apps, nil) { + return + } + + // get current tenant + tenant := r.Context().Value(middleware.ContextKey("tenant")).(*dbmodel.Tenants) + for _, app := range apps.AppsInfo { + app.TenantID = tenant.UUID + regionApp, err := handler.GetApplicationHandler().CreateApp(&app) + if err != nil { + logrus.Debugf("Batch Create App error is %v ", err) + httputil.ReturnBcodeError(r, w, err) + return + } + resp.AppID = app.GroupID + resp.RegionAppID = regionApp.AppID + respList = append(respList, resp) + } + httputil.ReturnSuccess(r, w, respList) +} + // UpdateApp - func (a *ApplicationController) UpdateApp(w http.ResponseWriter, r *http.Request) { var updateAppReq model.UpdateAppRequest diff --git a/api/handler/application_handler.go b/api/handler/application_handler.go index 98b36be28..b78bc120d 100644 --- a/api/handler/application_handler.go +++ b/api/handler/application_handler.go @@ -72,12 +72,6 @@ func (a *ApplicationAction) CreateApp(req *model.Application) (*model.Applicatio return nil, err } if len(req.ServiceIDs) != 0 { - for _,sid := range req.ServiceIDs { - if _, err := db.GetManager().TenantServiceDao().GetServiceByID(sid); err != nil { - tx.Rollback() - return nil, err - } - } if err := db.GetManager().TenantServiceDao().BindAppByServiceIDs(appReq.AppID, req.ServiceIDs); err != nil { tx.Rollback() return nil, err @@ -251,7 +245,7 @@ func (a *ApplicationAction) getDiskUsage(appID string) float64 { //BatchBindService func (a *ApplicationAction) BatchBindService(appID string, req model.BindServiceRequest) error { - for _,sid := range req.ServiceIDs { + for _, sid := range req.ServiceIDs { if _, err := db.GetManager().TenantServiceDao().GetServiceByID(sid); err != nil { return err } diff --git a/api/model/model.go b/api/model/model.go index abb482421..972ee57f7 100644 --- a/api/model/model.go +++ b/api/model/model.go @@ -1629,12 +1629,24 @@ func NewAppStatusFromImport(app *ImportAppStruct) *dbmodel.AppStatus { // Application - type Application struct { - AppName string `json:"app_name" validate:"required"` - AppID string `json:"app_id"` - TenantID string `json:"tenant_id"` + AppName string `json:"app_name" validate:"required"` + GroupID int64 `json:"group_id"` + AppID string `json:"app_id"` + TenantID string `json:"tenant_id"` ServiceIDs []string `json:"service_ids"` } +// CreateAppRequest - +type CreateAppRequest struct { + AppsInfo []Application `json:"apps_info"` +} + +// CreateAppResponse - +type CreateAppResponse struct { + AppID int64 `json:"app_id"` + RegionAppID string `json:"region_app_id"` +} + // ListAppResponse - type ListAppResponse struct { Page int `json:"page"` From 24b9e7905cadabf992c0c519aa10155c2ceb336e Mon Sep 17 00:00:00 2001 From: yangk Date: Tue, 10 Nov 2020 16:12:40 +0800 Subject: [PATCH 2/4] modified group_id name and some logic --- api/controller/application.go | 22 +++++----------------- api/handler/application_handler.go | 21 +++++++++++++++++++++ api/model/model.go | 10 +++++----- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/api/controller/application.go b/api/controller/application.go index bd5b6e29f..1f439855a 100644 --- a/api/controller/application.go +++ b/api/controller/application.go @@ -10,7 +10,6 @@ import ( "github.com/goodrain/rainbond/api/model" dbmodel "github.com/goodrain/rainbond/db/model" httputil "github.com/goodrain/rainbond/util/http" - "github.com/sirupsen/logrus" ) // ApplicationController - @@ -39,28 +38,17 @@ func (a *ApplicationController) CreateApp(w http.ResponseWriter, r *http.Request // BatchCreateApp - func (a *ApplicationController) BatchCreateApp(w http.ResponseWriter, r *http.Request) { - var ( - apps model.CreateAppRequest - resp model.CreateAppResponse - respList []model.CreateAppResponse - ) + var apps model.CreateAppRequest if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &apps, nil) { return } // get current tenant tenant := r.Context().Value(middleware.ContextKey("tenant")).(*dbmodel.Tenants) - for _, app := range apps.AppsInfo { - app.TenantID = tenant.UUID - regionApp, err := handler.GetApplicationHandler().CreateApp(&app) - if err != nil { - logrus.Debugf("Batch Create App error is %v ", err) - httputil.ReturnBcodeError(r, w, err) - return - } - resp.AppID = app.GroupID - resp.RegionAppID = regionApp.AppID - respList = append(respList, resp) + respList, err := handler.GetApplicationHandler().BatchCreateApp(&apps, tenant.UUID) + if err != nil { + httputil.ReturnBcodeError(r, w, err) + return } httputil.ReturnSuccess(r, w, respList) } diff --git a/api/handler/application_handler.go b/api/handler/application_handler.go index b78bc120d..183d193e9 100644 --- a/api/handler/application_handler.go +++ b/api/handler/application_handler.go @@ -26,6 +26,7 @@ type ApplicationAction struct { // ApplicationHandler defines handler methods to TenantApplication. type ApplicationHandler interface { CreateApp(req *model.Application) (*model.Application, error) + BatchCreateApp(req *model.CreateAppRequest, tenantID string) ([]model.CreateAppResponse, error) UpdateApp(srcApp *dbmodel.Application, req model.UpdateAppRequest) (*dbmodel.Application, error) ListApps(tenantID, appName string, page, pageSize int) (*model.ListAppResponse, error) GetAppByID(appID string) (*dbmodel.Application, error) @@ -85,6 +86,26 @@ func (a *ApplicationAction) CreateApp(req *model.Application) (*model.Applicatio return req, nil } +// BatchCreateApp - +func (a *ApplicationAction) BatchCreateApp(apps *model.CreateAppRequest, tenantID string) ([]model.CreateAppResponse, error) { + var ( + resp model.CreateAppResponse + respList []model.CreateAppResponse + ) + for _, app := range apps.AppsInfo { + app.TenantID = tenantID + regionApp, err := GetApplicationHandler().CreateApp(&app) + if err != nil { + logrus.Errorf("Batch Create App [%v] error is [%v] ", app.AppName, err) + return nil, err + } + resp.AppID = app.ConsoleAppID + resp.RegionAppID = regionApp.AppID + respList = append(respList, resp) + } + return respList, nil +} + // UpdateApp - func (a *ApplicationAction) UpdateApp(srcApp *dbmodel.Application, req model.UpdateAppRequest) (*dbmodel.Application, error) { srcApp.AppName = req.AppName diff --git a/api/model/model.go b/api/model/model.go index 972ee57f7..f4f29c260 100644 --- a/api/model/model.go +++ b/api/model/model.go @@ -1629,11 +1629,11 @@ func NewAppStatusFromImport(app *ImportAppStruct) *dbmodel.AppStatus { // Application - type Application struct { - AppName string `json:"app_name" validate:"required"` - GroupID int64 `json:"group_id"` - AppID string `json:"app_id"` - TenantID string `json:"tenant_id"` - ServiceIDs []string `json:"service_ids"` + AppName string `json:"app_name" validate:"required"` + ConsoleAppID int64 `json:"console_app_id"` + AppID string `json:"app_id"` + TenantID string `json:"tenant_id"` + ServiceIDs []string `json:"service_ids"` } // CreateAppRequest - From 622941f7e3249026f19482b05cde5ccb6be67d89 Mon Sep 17 00:00:00 2001 From: yangk Date: Tue, 10 Nov 2020 16:47:44 +0800 Subject: [PATCH 3/4] modified error handle --- api/handler/application_handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/handler/application_handler.go b/api/handler/application_handler.go index 8bd8738bc..5fa1100f3 100644 --- a/api/handler/application_handler.go +++ b/api/handler/application_handler.go @@ -97,7 +97,7 @@ func (a *ApplicationAction) BatchCreateApp(apps *model.CreateAppRequest, tenantI regionApp, err := GetApplicationHandler().CreateApp(&app) if err != nil { logrus.Errorf("Batch Create App [%v] error is [%v] ", app.AppName, err) - return nil, err + continue } resp.AppID = app.ConsoleAppID resp.RegionAppID = regionApp.AppID From 40f5078e0a32dd9d8a6079080e414b503fe79a78 Mon Sep 17 00:00:00 2001 From: yangk Date: Thu, 12 Nov 2020 16:34:06 +0800 Subject: [PATCH 4/4] modified import package name --- builder/sources/registry/manifest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/sources/registry/manifest.go b/builder/sources/registry/manifest.go index 495a2997b..894d55309 100644 --- a/builder/sources/registry/manifest.go +++ b/builder/sources/registry/manifest.go @@ -24,7 +24,7 @@ import ( "io/ioutil" "net/http" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" manifestV1 "github.com/docker/distribution/manifest/schema1" manifestV2 "github.com/docker/distribution/manifest/schema2" digest "github.com/opencontainers/go-digest"