Rainbond/api/controller/batch_operation.go

112 lines
4.4 KiB
Go
Raw Normal View History

2019-03-08 19:22:22 +08:00
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2019 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package controller
import (
"fmt"
"net/http"
2019-03-09 13:51:36 +08:00
"github.com/Sirupsen/logrus"
2019-03-08 19:22:22 +08:00
"github.com/goodrain/rainbond/api/handler"
2019-03-09 11:50:31 +08:00
"github.com/goodrain/rainbond/api/middleware"
2019-08-27 20:07:02 +08:00
"github.com/goodrain/rainbond/api/util"
2019-03-08 19:22:22 +08:00
"github.com/goodrain/rainbond/api/model"
2019-08-27 20:07:02 +08:00
dbmodel "github.com/goodrain/rainbond/db/model"
2019-03-08 19:22:22 +08:00
httputil "github.com/goodrain/rainbond/util/http"
)
//BatchOperation batch operation for tenant
//support operation is : start,build,stop,update
func BatchOperation(w http.ResponseWriter, r *http.Request) {
var build model.BeatchOperationRequestStruct
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &build.Body, nil)
if !ok {
2019-03-09 13:51:36 +08:00
logrus.Errorf("start batch operation validate request body failure")
2019-03-08 19:22:22 +08:00
return
}
2019-03-09 11:50:31 +08:00
tenantName := r.Context().Value(middleware.ContextKey("tenant_name")).(string)
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
2019-08-27 20:07:02 +08:00
// create event for each operation
eventRe := createBatchEvents(&build, tenantID, build.Operator)
2019-08-27 20:07:02 +08:00
2019-03-08 19:22:22 +08:00
var re handler.BatchOperationResult
switch build.Body.Operation {
case "build":
2019-03-09 11:50:31 +08:00
for i := range build.Body.BuildInfos {
build.Body.BuildInfos[i].TenantName = tenantName
}
2019-08-27 20:07:02 +08:00
re = handler.GetBatchOperationHandler().Build(build.Body.BuildInfos)
2019-03-08 19:22:22 +08:00
case "start":
2019-08-27 20:07:02 +08:00
re = handler.GetBatchOperationHandler().Start(build.Body.StartInfos)
2019-03-08 19:22:22 +08:00
case "stop":
2019-08-27 20:07:02 +08:00
re = handler.GetBatchOperationHandler().Stop(build.Body.StopInfos)
2019-03-08 19:22:22 +08:00
case "upgrade":
2019-08-27 20:07:02 +08:00
re = handler.GetBatchOperationHandler().Upgrade(build.Body.UpgradeInfos)
2019-03-08 19:22:22 +08:00
default:
httputil.ReturnError(r, w, 400, fmt.Sprintf("operation %s do not support batch", build.Body.Operation))
return
}
2019-08-27 20:07:02 +08:00
// append every create event result to re and then return
re.BatchResult = append(re.BatchResult, eventRe.BatchResult...)
2019-03-08 19:22:22 +08:00
httputil.ReturnSuccess(r, w, re)
}
2019-08-27 20:07:02 +08:00
2019-08-28 19:53:52 +08:00
func createBatchEvents(build *model.BeatchOperationRequestStruct, tenantID, operator string) (re handler.BatchOperationResult) {
2019-08-27 20:07:02 +08:00
for i := range build.Body.BuildInfos {
2019-08-28 19:53:52 +08:00
event, err := util.CreateEvent(dbmodel.TargetTypeService, "build-service", build.Body.BuildInfos[i].ServiceID, tenantID, "", operator, dbmodel.ASYNEVENTTYPE)
2019-08-27 20:07:02 +08:00
if err != nil {
re.BatchResult = append(re.BatchResult, handler.OperationResult{ErrMsg: "create event failure", ServiceID: build.Body.BuildInfos[i].ServiceID})
continue
}
build.Body.BuildInfos[i].EventID = event.EventID
}
for i := range build.Body.StartInfos {
2019-08-28 19:53:52 +08:00
event, err := util.CreateEvent(dbmodel.TargetTypeService, "start-service", build.Body.StartInfos[i].ServiceID, tenantID, "", operator, dbmodel.ASYNEVENTTYPE)
2019-08-27 20:07:02 +08:00
if err != nil {
re.BatchResult = append(re.BatchResult, handler.OperationResult{ErrMsg: "create event failure", ServiceID: build.Body.StartInfos[i].ServiceID})
continue
}
build.Body.StartInfos[i].EventID = event.EventID
}
for i := range build.Body.StopInfos {
2019-08-28 19:53:52 +08:00
event, err := util.CreateEvent(dbmodel.TargetTypeService, "stop-service", build.Body.StopInfos[i].ServiceID, tenantID, "", operator, dbmodel.ASYNEVENTTYPE)
2019-08-27 20:07:02 +08:00
if err != nil {
re.BatchResult = append(re.BatchResult, handler.OperationResult{ErrMsg: "create event failure", ServiceID: build.Body.StopInfos[i].ServiceID})
continue
}
build.Body.StopInfos[i].EventID = event.EventID
}
for i := range build.Body.UpgradeInfos {
2019-08-28 19:53:52 +08:00
event, err := util.CreateEvent(dbmodel.TargetTypeService, "upgrade-service", build.Body.UpgradeInfos[i].ServiceID, tenantID, "", operator, dbmodel.ASYNEVENTTYPE)
2019-08-27 20:07:02 +08:00
if err != nil {
re.BatchResult = append(re.BatchResult, handler.OperationResult{ErrMsg: "create event failure", ServiceID: build.Body.UpgradeInfos[i].ServiceID})
continue
}
build.Body.UpgradeInfos[i].EventID = event.EventID
}
return
}