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..1f439855a 100644 --- a/api/controller/application.go +++ b/api/controller/application.go @@ -36,6 +36,23 @@ 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 + if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &apps, nil) { + return + } + + // get current tenant + tenant := r.Context().Value(middleware.ContextKey("tenant")).(*dbmodel.Tenants) + respList, err := handler.GetApplicationHandler().BatchCreateApp(&apps, tenant.UUID) + if err != nil { + httputil.ReturnBcodeError(r, w, err) + return + } + 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 94eca57e1..5fa1100f3 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) @@ -72,12 +73,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 @@ -91,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) + continue + } + 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) { if req.AppName != "" { diff --git a/api/model/model.go b/api/model/model.go index 2fb170a4f..95efa8523 100644 --- a/api/model/model.go +++ b/api/model/model.go @@ -1629,10 +1629,22 @@ 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"` - 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 - +type CreateAppRequest struct { + AppsInfo []Application `json:"apps_info"` +} + +// CreateAppResponse - +type CreateAppResponse struct { + AppID int64 `json:"app_id"` + RegionAppID string `json:"region_app_id"` } // ListAppResponse -