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"
|
2021-06-07 09:37:15 +08:00
|
|
|
"github.com/jinzhu/gorm"
|
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-23 15:51:34 +08:00
|
|
|
services, err := db.GetManager().TenantServiceDao().GetServicesByServiceIDs(req.ServiceIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-23 18:58:54 +08:00
|
|
|
|
|
|
|
tx := db.GetManager().Begin()
|
2020-09-30 10:55:09 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logrus.Errorf("Unexpected panic occurred, rollback transaction: %v", r)
|
|
|
|
tx.Rollback()
|
|
|
|
}
|
|
|
|
}()
|
2020-09-23 14:48:53 +08:00
|
|
|
// Create application configGroup-services
|
2020-09-23 15:51:34 +08:00
|
|
|
for _, s := range services {
|
2020-11-13 09:42:17 +08:00
|
|
|
serviceConfigGroup := dbmodel.ConfigGroupService{
|
2020-09-23 14:48:53 +08:00
|
|
|
AppID: appID,
|
|
|
|
ConfigGroupName: req.ConfigGroupName,
|
2020-09-23 15:51:34 +08:00
|
|
|
ServiceID: s.ServiceID,
|
|
|
|
ServiceAlias: s.ServiceAlias,
|
2020-09-23 14:48:53 +08:00
|
|
|
}
|
2020-09-24 10:25:58 +08:00
|
|
|
if err := db.GetManager().AppConfigGroupServiceDaoTransactions(tx).AddModel(&serviceConfigGroup); err != nil {
|
2020-11-13 09:42:17 +08:00
|
|
|
if err == bcode.ErrConfigGroupServiceExist {
|
2020-09-22 14:37:52 +08:00
|
|
|
logrus.Warningf("config group \"%s\" under this service \"%s\" already exists.", serviceConfigGroup.ConfigGroupName, serviceConfigGroup.ServiceID)
|
|
|
|
continue
|
|
|
|
}
|
2020-09-23 18:58:54 +08:00
|
|
|
tx.Rollback()
|
2020-09-22 14:37:52 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 14:48:53 +08:00
|
|
|
|
|
|
|
// Create application configGroup-configItem
|
2020-09-22 14:37:52 +08:00
|
|
|
for _, it := range req.ConfigItems {
|
2020-10-08 16:10:03 +08:00
|
|
|
configItem := &dbmodel.ConfigGroupItem{
|
2020-09-23 14:48:53 +08:00
|
|
|
AppID: appID,
|
|
|
|
ConfigGroupName: req.ConfigGroupName,
|
|
|
|
ItemKey: it.ItemKey,
|
|
|
|
ItemValue: it.ItemValue,
|
|
|
|
}
|
2020-09-23 18:58:54 +08:00
|
|
|
if err := db.GetManager().AppConfigGroupItemDaoTransactions(tx).AddModel(configItem); err != nil {
|
2020-09-22 14:37:52 +08:00
|
|
|
if err == bcode.ErrConfigItemExist {
|
|
|
|
logrus.Warningf("config item \"%s\" under this config group \"%s\" already exists.", configItem.ItemKey, configItem.ConfigGroupName)
|
|
|
|
continue
|
|
|
|
}
|
2020-09-23 18:58:54 +08:00
|
|
|
tx.Rollback()
|
2020-09-22 14:37:52 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-23 14:48:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create application configGroup
|
|
|
|
config := &dbmodel.ApplicationConfigGroup{
|
|
|
|
AppID: appID,
|
|
|
|
ConfigGroupName: req.ConfigGroupName,
|
|
|
|
DeployType: req.DeployType,
|
2020-11-17 14:07:43 +08:00
|
|
|
Enable: req.Enable,
|
2020-09-21 18:33:10 +08:00
|
|
|
}
|
2020-09-23 18:58:54 +08:00
|
|
|
if err := db.GetManager().AppConfigGroupDaoTransactions(tx).AddModel(config); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := tx.Commit().Error; err != nil {
|
|
|
|
tx.Rollback()
|
2020-09-21 18:33:10 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-22 15:51:57 +08:00
|
|
|
|
2020-09-23 21:41:13 +08:00
|
|
|
appconfig, err := db.GetManager().AppConfigGroupDao().GetConfigGroupByID(appID, req.ConfigGroupName)
|
2020-09-23 15:51:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-30 10:55:09 +08:00
|
|
|
configGroupServices, err := db.GetManager().AppConfigGroupServiceDao().GetConfigGroupServicesByID(appID, req.ConfigGroupName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
configGroupItems, err := db.GetManager().AppConfigGroupItemDao().GetConfigGroupItemsByID(appID, req.ConfigGroupName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-22 15:51:57 +08:00
|
|
|
var resp *model.ApplicationConfigGroupResp
|
|
|
|
resp = &model.ApplicationConfigGroupResp{
|
|
|
|
CreateTime: appconfig.CreatedAt,
|
|
|
|
AppID: appID,
|
|
|
|
ConfigGroupName: appconfig.ConfigGroupName,
|
|
|
|
DeployType: appconfig.DeployType,
|
2020-09-30 10:55:09 +08:00
|
|
|
ConfigItems: configGroupItems,
|
|
|
|
Services: configGroupServices,
|
2020-11-17 14:07:43 +08:00
|
|
|
Enable: appconfig.Enable,
|
2020-09-22 15:51:57 +08:00
|
|
|
}
|
|
|
|
return resp, nil
|
2020-09-21 18:33:10 +08:00
|
|
|
}
|
2020-09-23 21:41:13 +08:00
|
|
|
|
|
|
|
// UpdateConfigGroup -
|
|
|
|
func (a *ApplicationAction) UpdateConfigGroup(appID, configGroupName string, req *model.UpdateAppConfigGroupReq) (*model.ApplicationConfigGroupResp, error) {
|
2020-09-24 13:06:22 +08:00
|
|
|
appconfig, err := db.GetManager().AppConfigGroupDao().GetConfigGroupByID(appID, configGroupName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-23 21:41:13 +08:00
|
|
|
services, err := db.GetManager().TenantServiceDao().GetServicesByServiceIDs(req.ServiceIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := db.GetManager().Begin()
|
2020-09-30 10:55:09 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logrus.Errorf("Unexpected panic occurred, rollback transaction: %v", r)
|
|
|
|
tx.Rollback()
|
|
|
|
}
|
|
|
|
}()
|
2020-11-17 16:01:57 +08:00
|
|
|
// Update effective status
|
2020-11-17 17:41:22 +08:00
|
|
|
appconfig.Enable = req.Enable
|
2020-11-17 16:01:57 +08:00
|
|
|
if err := db.GetManager().AppConfigGroupDaoTransactions(tx).UpdateModel(appconfig); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-23 21:41:13 +08:00
|
|
|
// Update application configGroup-services
|
2020-09-24 10:05:54 +08:00
|
|
|
if err := db.GetManager().AppConfigGroupServiceDaoTransactions(tx).DeleteConfigGroupService(appID, configGroupName); err != nil {
|
2020-09-23 21:41:13 +08:00
|
|
|
tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, s := range services {
|
2020-11-13 09:42:17 +08:00
|
|
|
serviceConfigGroup := dbmodel.ConfigGroupService{
|
2020-09-23 21:41:13 +08:00
|
|
|
AppID: appID,
|
|
|
|
ConfigGroupName: configGroupName,
|
|
|
|
ServiceID: s.ServiceID,
|
|
|
|
ServiceAlias: s.ServiceAlias,
|
|
|
|
}
|
|
|
|
if err := db.GetManager().AppConfigGroupServiceDaoTransactions(tx).AddModel(&serviceConfigGroup); err != nil {
|
2021-05-13 16:34:20 +08:00
|
|
|
if err == bcode.ErrConfigGroupServiceExist {
|
2021-05-13 17:57:17 +08:00
|
|
|
logrus.Debugf("config group \"%s\" under this service \"%s\" already exists.", serviceConfigGroup.ConfigGroupName, serviceConfigGroup.ServiceID)
|
2021-05-13 16:34:20 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-09-23 21:41:13 +08:00
|
|
|
tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update application configGroup-configItem
|
2020-09-24 13:06:22 +08:00
|
|
|
if err := db.GetManager().AppConfigGroupItemDaoTransactions(tx).DeleteConfigGroupItem(appID, configGroupName); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-23 21:41:13 +08:00
|
|
|
for _, it := range req.ConfigItems {
|
2020-10-08 16:10:03 +08:00
|
|
|
configItem := &dbmodel.ConfigGroupItem{
|
2020-09-23 21:41:13 +08:00
|
|
|
AppID: appID,
|
|
|
|
ConfigGroupName: configGroupName,
|
|
|
|
ItemKey: it.ItemKey,
|
|
|
|
ItemValue: it.ItemValue,
|
|
|
|
}
|
2020-09-24 13:06:22 +08:00
|
|
|
if err := db.GetManager().AppConfigGroupItemDaoTransactions(tx).AddModel(configItem); err != nil {
|
2021-05-13 16:34:20 +08:00
|
|
|
if err == bcode.ErrConfigItemExist {
|
2021-05-13 17:57:17 +08:00
|
|
|
logrus.Debugf("config item \"%s\" under this config group \"%s\" already exists.", configItem.ItemKey, configItem.ConfigGroupName)
|
2021-05-13 16:34:20 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-09-23 21:41:13 +08:00
|
|
|
tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := tx.Commit().Error; err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-30 10:55:09 +08:00
|
|
|
configGroupServices, err := db.GetManager().AppConfigGroupServiceDao().GetConfigGroupServicesByID(appID, configGroupName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
configGroupItems, err := db.GetManager().AppConfigGroupItemDao().GetConfigGroupItemsByID(appID, configGroupName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-24 10:05:54 +08:00
|
|
|
|
2020-09-23 21:41:13 +08:00
|
|
|
// Build return data
|
|
|
|
var resp *model.ApplicationConfigGroupResp
|
|
|
|
resp = &model.ApplicationConfigGroupResp{
|
|
|
|
CreateTime: appconfig.CreatedAt,
|
|
|
|
AppID: appconfig.AppID,
|
|
|
|
ConfigGroupName: appconfig.ConfigGroupName,
|
|
|
|
DeployType: appconfig.DeployType,
|
2020-09-30 10:55:09 +08:00
|
|
|
ConfigItems: configGroupItems,
|
|
|
|
Services: configGroupServices,
|
2020-11-17 14:07:43 +08:00
|
|
|
Enable: appconfig.Enable,
|
2020-09-23 21:41:13 +08:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
2020-09-23 22:23:02 +08:00
|
|
|
|
|
|
|
// DeleteConfigGroup -
|
|
|
|
func (a *ApplicationAction) DeleteConfigGroup(appID, configGroupName string) error {
|
|
|
|
tx := db.GetManager().Begin()
|
2020-09-30 10:55:09 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logrus.Errorf("Unexpected panic occurred, rollback transaction: %v", r)
|
|
|
|
tx.Rollback()
|
|
|
|
}
|
|
|
|
}()
|
2020-09-23 22:23:02 +08:00
|
|
|
// Delete application configGroup-services
|
2020-09-24 10:05:54 +08:00
|
|
|
if err := db.GetManager().AppConfigGroupServiceDaoTransactions(tx).DeleteConfigGroupService(appID, configGroupName); err != nil {
|
2020-09-23 22:23:02 +08:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Delete application configGroup-configItem
|
2020-09-24 10:05:54 +08:00
|
|
|
if err := db.GetManager().AppConfigGroupItemDaoTransactions(tx).DeleteConfigGroupItem(appID, configGroupName); err != nil {
|
2020-09-23 22:23:02 +08:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Delete application configGroup
|
|
|
|
if err := db.GetManager().AppConfigGroupDaoTransactions(tx).DeleteConfigGroup(appID, configGroupName); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit().Error; err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-24 10:05:54 +08:00
|
|
|
|
|
|
|
// ListConfigGroups -
|
|
|
|
func (a *ApplicationAction) ListConfigGroups(appID string, page, pageSize int) (*model.ListApplicationConfigGroupResp, error) {
|
2020-09-30 10:55:09 +08:00
|
|
|
var resp model.ListApplicationConfigGroupResp
|
|
|
|
|
2020-09-24 10:05:54 +08:00
|
|
|
configGroups, total, err := db.GetManager().AppConfigGroupDao().GetConfigGroupsByAppID(appID, page, pageSize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, c := range configGroups {
|
|
|
|
cgroup := model.ApplicationConfigGroupResp{
|
|
|
|
CreateTime: c.CreatedAt,
|
|
|
|
AppID: c.AppID,
|
|
|
|
ConfigGroupName: c.ConfigGroupName,
|
|
|
|
DeployType: c.DeployType,
|
2020-11-17 14:07:43 +08:00
|
|
|
Enable: c.Enable,
|
2020-09-24 10:05:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
configGroupServices, err := db.GetManager().AppConfigGroupServiceDao().GetConfigGroupServicesByID(c.AppID, c.ConfigGroupName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
configGroupItems, err := db.GetManager().AppConfigGroupItemDao().GetConfigGroupItemsByID(c.AppID, c.ConfigGroupName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:55:09 +08:00
|
|
|
cgroup.Services = configGroupServices
|
|
|
|
cgroup.ConfigItems = configGroupItems
|
2020-09-24 10:05:54 +08:00
|
|
|
resp.ConfigGroup = append(resp.ConfigGroup, cgroup)
|
|
|
|
}
|
|
|
|
resp.Page = page
|
|
|
|
resp.Total = total
|
|
|
|
resp.PageSize = pageSize
|
|
|
|
return &resp, nil
|
|
|
|
}
|
2021-06-07 09:37:15 +08:00
|
|
|
|
|
|
|
// SyncComponentConfigGroupRels -
|
|
|
|
func (a *ApplicationAction) SyncComponentConfigGroupRels(tx *gorm.DB, componentIDs []string, cgservices []dbmodel.ConfigGroupService) error{
|
|
|
|
if err := db.GetManager().AppConfigGroupServiceDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.GetManager().AppConfigGroupServiceDaoTransactions(tx).CreateOrUpdateConfigGroupServicesInBatch(cgservices); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|