Commit 132d3af5 authored by yangk's avatar yangk
Browse files

add sync app config groups interface

Showing with 143 additions and 8 deletions
+143 -8
......@@ -169,6 +169,7 @@ type ApplicationInterface interface {
DeleteConfigGroup(w http.ResponseWriter, r *http.Request)
ListConfigGroups(w http.ResponseWriter, r *http.Request)
SyncComponents(w http.ResponseWriter, r *http.Request)
SyncAppConfigGroups(w http.ResponseWriter, r *http.Request)
}
//Gatewayer gateway api interface
......
......@@ -323,6 +323,7 @@ func (v2 *V2) applicationRouter() chi.Router {
// Synchronize component information, full coverage
r.Post("/components", controller.GetManager().SyncComponents)
r.Post("/app-config-groups", controller.GetManager().SyncAppConfigGroups)
return r
}
......
......@@ -123,3 +123,18 @@ func (a *ApplicationController)SyncComponents(w http.ResponseWriter, r *http.Req
}
httputil.ReturnSuccess(r, w, nil)
}
// SyncAppConfigGroups -
func (a *ApplicationController)SyncAppConfigGroups(w http.ResponseWriter, r *http.Request){
var syncAppConfigGroupReq model.SyncAppConfigGroup
app := r.Context().Value(middleware.ContextKey("application")).(*dbmodel.Application)
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &syncAppConfigGroupReq, nil){
return
}
err := handler.GetApplicationHandler().SyncAppConfigGroups(app, syncAppConfigGroupReq.AppConfigGroups)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, nil)
}
......@@ -264,10 +264,10 @@ func (a *ApplicationAction) ListConfigGroups(appID string, page, pageSize int) (
}
// SyncComponentConfigGroupRels -
func (a *ApplicationAction) SyncComponentConfigGroupRels(tx *gorm.DB, app *dbmodel.Application, components []*model.Component) error{
func (a *ApplicationAction) SyncComponentConfigGroupRels(tx *gorm.DB, app *dbmodel.Application, components []*model.Component) error {
var (
componentIDs []string
cgservices []*dbmodel.ConfigGroupService
cgservices []*dbmodel.ConfigGroupService
)
for _, component := range components {
if component.AppConfigGroupRels == nil {
......@@ -283,3 +283,40 @@ func (a *ApplicationAction) SyncComponentConfigGroupRels(tx *gorm.DB, app *dbmod
}
return db.GetManager().AppConfigGroupServiceDaoTransactions(tx).CreateOrUpdateConfigGroupServicesInBatch(cgservices)
}
// SyncAppConfigGroups -
func (a *ApplicationAction) SyncAppConfigGroups(app *dbmodel.Application, appConfigGroups []model.ApplicationConfigGroup) error {
var (
cgroups []*dbmodel.ApplicationConfigGroup
cgitems []*dbmodel.ConfigGroupItem
cgservices []*dbmodel.ConfigGroupService
)
for _, configGroup := range appConfigGroups {
cgroups = append(cgroups, configGroup.DbModel(app.AppID))
for _, item := range configGroup.ConfigItems {
cgitems = append(cgitems, item.DbModel(app.AppID, configGroup.ConfigGroupName))
}
for _, cgservice := range configGroup.ConfigGroupServices {
cgservices = append(cgservices, cgservice.DbModel(app.AppID, configGroup.ConfigGroupName))
}
}
return db.GetManager().DB().Transaction(func(tx *gorm.DB) error {
if err := db.GetManager().AppConfigGroupDaoTransactions(tx).DeleteByAppID(app.AppID); err != nil {
return err
}
if err := db.GetManager().AppConfigGroupServiceDaoTransactions(tx).DeleteByAppID(app.AppID); err != nil {
return err
}
if err := db.GetManager().AppConfigGroupItemDaoTransactions(tx).DeleteByAppID(app.AppID); err != nil {
return err
}
if err := db.GetManager().AppConfigGroupDaoTransactions(tx).CreateOrUpdateConfigGroupsInBatch(cgroups); err != nil {
return err
}
if err := db.GetManager().AppConfigGroupServiceDaoTransactions(tx).CreateOrUpdateConfigGroupServicesInBatch(cgservices); err != nil {
return err
}
return db.GetManager().AppConfigGroupItemDaoTransactions(tx).CreateOrUpdateConfigGroupItemsInBatch(cgitems)
})
}
......@@ -44,6 +44,7 @@ type ApplicationHandler interface {
ListConfigGroups(appID string, page, pageSize int) (*model.ListApplicationConfigGroupResp, error)
SyncComponents(app *dbmodel.Application, components []*model.Component) error
SyncComponentConfigGroupRels(tx *gorm.DB, app *dbmodel.Application, components []*model.Component) error
SyncAppConfigGroups(app *dbmodel.Application, appConfigGroups []model.ApplicationConfigGroup) error
}
// NewApplicationHandler creates a new Tenant Application Handler.
......
......@@ -1832,6 +1832,16 @@ type ConfigGroupService struct {
ServiceAlias string `json:"service_alias"`
}
// DbModel return database model
func (c ConfigGroupService) DbModel(appID, configGroupName string) *dbmodel.ConfigGroupService {
return &dbmodel.ConfigGroupService{
AppID: appID,
ConfigGroupName: configGroupName,
ServiceID: c.ServiceID,
ServiceAlias: c.ServiceAlias,
}
}
// ConfigItem -
type ConfigItem struct {
AppID string `json:"-"`
......@@ -1840,14 +1850,35 @@ type ConfigItem struct {
ItemValue string `json:"item_value" validate:"required,max=65535"`
}
// DbModel return database model
func (c ConfigItem) DbModel(appID, configGroupName string) *dbmodel.ConfigGroupItem {
return &dbmodel.ConfigGroupItem{
AppID: appID,
ConfigGroupName: configGroupName,
ItemKey: c.ItemKey,
ItemValue: c.ItemValue,
}
}
// ApplicationConfigGroup -
type ApplicationConfigGroup struct {
AppID string `json:"app_id"`
ConfigGroupName string `json:"config_group_name" validate:"required,alphanum,min=2,max=64"`
DeployType string `json:"deploy_type" validate:"required,oneof=env configfile"`
ServiceIDs []string `json:"service_ids"`
ConfigItems []ConfigItem `json:"config_items"`
Enable bool `json:"enable"`
AppID string `json:"app_id"`
ConfigGroupName string `json:"config_group_name" validate:"required,alphanum,min=2,max=64"`
DeployType string `json:"deploy_type" validate:"required,oneof=env configfile"`
ServiceIDs []string `json:"service_ids"`
ConfigItems []ConfigItem `json:"config_items"`
ConfigGroupServices []ConfigGroupService `json:"config_group_services"`
Enable bool `json:"enable"`
}
// DbModel return database model
func (a ApplicationConfigGroup) DbModel(appID string) *dbmodel.ApplicationConfigGroup {
return &dbmodel.ApplicationConfigGroup{
AppID: appID,
ConfigGroupName: a.ConfigGroupName,
DeployType: a.DeployType,
Enable: a.Enable,
}
}
// ApplicationConfigGroupResp -
......@@ -1890,3 +1921,8 @@ func (a *AppConfigGroupRelations) DbModel(appID, serviceID, serviceAlias string)
ServiceAlias: serviceAlias,
}
}
// SyncAppConfigGroup -
type SyncAppConfigGroup struct {
AppConfigGroups []ApplicationConfigGroup `json:"app_config_groups"`
}
......@@ -83,6 +83,8 @@ type AppConfigGroupDao interface {
ListByServiceID(sid string) ([]*model.ApplicationConfigGroup, error)
GetConfigGroupsByAppID(appID string, page, pageSize int) ([]*model.ApplicationConfigGroup, int64, error)
DeleteConfigGroup(appID, configGroupName string) error
DeleteByAppID(appID string) error
CreateOrUpdateConfigGroupsInBatch(cgroups []*model.ApplicationConfigGroup) error
}
//AppConfigGroupServiceDao service config group Dao
......@@ -93,6 +95,7 @@ type AppConfigGroupServiceDao interface {
DeleteEffectiveServiceByServiceID(serviceID string) error
DeleteByComponentIDs(componentIDs []string) error
CreateOrUpdateConfigGroupServicesInBatch(cgservices []*model.ConfigGroupService) error
DeleteByAppID(appID string) error
}
//AppConfigGroupItemDao Application config item group Dao
......@@ -101,6 +104,8 @@ type AppConfigGroupItemDao interface {
GetConfigGroupItemsByID(appID, configGroupName string) ([]*model.ConfigGroupItem, error)
ListByServiceID(sid string) ([]*model.ConfigGroupItem, error)
DeleteConfigGroupItem(appID, configGroupName string) error
DeleteByAppID(appID string) error
CreateOrUpdateConfigGroupItemsInBatch(cgitems []*model.ConfigGroupItem) error
}
// VolumeTypeDao volume type dao
......
......@@ -72,6 +72,23 @@ func (a *AppConfigGroupDaoImpl) DeleteConfigGroup(appID, configGroupName string)
return a.DB.Where("app_id = ? AND config_group_name = ?", appID, configGroupName).Delete(model.ApplicationConfigGroup{}).Error
}
//DeleteByAppID -
func (a *AppConfigGroupDaoImpl) DeleteByAppID(appID string) error {
return a.DB.Where("app_id = ?", appID).Delete(model.ApplicationConfigGroup{}).Error
}
// CreateOrUpdateConfigGroupsInBatch -
func (a *AppConfigGroupDaoImpl) CreateOrUpdateConfigGroupsInBatch(cgroups []*model.ApplicationConfigGroup) error {
var objects []interface{}
for _, cg := range cgroups {
objects = append(objects, *cg)
}
if err := gormbulkups.BulkUpsert(a.DB, objects, 2000); err != nil {
return pkgerr.Wrap(err, "create or update config groups in batch")
}
return nil
}
// AppConfigGroupServiceDaoImpl -
type AppConfigGroupServiceDaoImpl struct {
DB *gorm.DB
......@@ -119,6 +136,11 @@ func (a *AppConfigGroupServiceDaoImpl) DeleteByComponentIDs(componentIDs []strin
return a.DB.Where("service_id in (?)", componentIDs).Delete(model.ConfigGroupService{}).Error
}
//DeleteByAppID -
func (a *AppConfigGroupServiceDaoImpl) DeleteByAppID(appID string) error {
return a.DB.Where("app_id = ?", appID).Delete(model.ConfigGroupService{}).Error
}
// CreateOrUpdateConfigGroupServicesInBatch -
func (a *AppConfigGroupServiceDaoImpl) CreateOrUpdateConfigGroupServicesInBatch(cgservices []*model.ConfigGroupService) error {
var objects []interface{}
......@@ -180,3 +202,20 @@ func (a *AppConfigGroupItemDaoImpl) ListByServiceID(sid string) ([]*model.Config
func (a *AppConfigGroupItemDaoImpl) DeleteConfigGroupItem(appID, configGroupName string) error {
return a.DB.Where("app_id = ? AND config_group_name = ?", appID, configGroupName).Delete(model.ConfigGroupItem{}).Error
}
//DeleteByAppID -
func (a *AppConfigGroupItemDaoImpl) DeleteByAppID(appID string) error {
return a.DB.Where("app_id = ?", appID).Delete(model.ConfigGroupItem{}).Error
}
// CreateOrUpdateConfigGroupItemsInBatch -
func (a *AppConfigGroupItemDaoImpl) CreateOrUpdateConfigGroupItemsInBatch(cgitems []*model.ConfigGroupItem) error {
var objects []interface{}
for _, cgi := range cgitems {
objects = append(objects, *cgi)
}
if err := gormbulkups.BulkUpsert(a.DB, objects, 2000); err != nil {
return pkgerr.Wrap(err, "create or update config group items in batch")
}
return nil
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment