Rainbond/api/handler/application_config_group.go

76 lines
2.5 KiB
Go
Raw Normal View History

2020-09-21 18:33:10 +08:00
package handler
import (
"github.com/goodrain/rainbond/api/model"
2020-09-22 14:37:52 +08:00
"github.com/goodrain/rainbond/api/util/bcode"
2020-09-21 18:33:10 +08:00
"github.com/goodrain/rainbond/db"
dbmodel "github.com/goodrain/rainbond/db/model"
2020-09-22 14:37:52 +08:00
"github.com/sirupsen/logrus"
2020-09-21 18:33:10 +08:00
)
// AddConfigGroup -
2020-09-22 15:51:57 +08:00
func (a *ApplicationAction) AddConfigGroup(appID string, req *model.ApplicationConfigGroup) (*model.ApplicationConfigGroupResp, error) {
2020-09-22 14:37:52 +08:00
serviceConfigGroup := &dbmodel.ServiceConfigGroup{
AppID: appID,
ConfigGroupName: req.ConfigGroupName,
2020-09-21 18:33:10 +08:00
}
2020-09-22 14:37:52 +08:00
configItem := &dbmodel.ConfigItem{
AppID: appID,
ConfigGroupName: req.ConfigGroupName,
2020-09-21 18:33:10 +08:00
}
config := &dbmodel.ApplicationConfigGroup{
AppID: appID,
ConfigGroupName: req.ConfigGroupName,
DeployType: req.DeployType,
2020-09-22 14:37:52 +08:00
}
// Create application ConfigGroup
for _, sID := range req.ServiceIDs {
serviceConfigGroup.ServiceID = sID
if err := db.GetManager().ServiceConfigGroupDao().AddModel(serviceConfigGroup); err != nil {
if err == bcode.ErrServiceConfigGroupExist {
logrus.Warningf("config group \"%s\" under this service \"%s\" already exists.", serviceConfigGroup.ConfigGroupName, serviceConfigGroup.ServiceID)
continue
}
return nil, err
}
serviceConfigGroup.ID++
}
for _, it := range req.ConfigItems {
configItem.ItemKey = it.ItemKey
configItem.ItemValue = it.ItemValue
if err := db.GetManager().ConfigItemDao().AddModel(configItem); err != nil {
if err == bcode.ErrConfigItemExist {
logrus.Warningf("config item \"%s\" under this config group \"%s\" already exists.", configItem.ItemKey, configItem.ConfigGroupName)
continue
}
return nil, err
}
configItem.ID++
2020-09-21 18:33:10 +08:00
}
if err := db.GetManager().ApplicationConfigDao().AddModel(config); err != nil {
return nil, err
}
2020-09-22 15:51:57 +08:00
2020-09-23 11:50:32 +08:00
var serviceResult []dbmodel.ServiceIDAndNameResult
services, _ := db.GetManager().TenantServiceDao().GetServiceByIDs(req.ServiceIDs)
for _, service := range services {
s := dbmodel.ServiceIDAndNameResult{
ServiceID: service.ServiceID,
ServiceName: service.ServiceName,
}
serviceResult = append(serviceResult, s)
}
2020-09-22 15:51:57 +08:00
appconfig, _ := db.GetManager().ApplicationConfigDao().GetConfigByID(appID, req.ConfigGroupName)
var resp *model.ApplicationConfigGroupResp
resp = &model.ApplicationConfigGroupResp{
CreateTime: appconfig.CreatedAt,
AppID: appID,
ConfigGroupName: appconfig.ConfigGroupName,
DeployType: appconfig.DeployType,
ConfigItems: req.ConfigItems,
2020-09-23 11:50:32 +08:00
Services: serviceResult,
2020-09-22 15:51:57 +08:00
}
return resp, nil
2020-09-21 18:33:10 +08:00
}