add label_value field for busi_group

This commit is contained in:
Ulric Qin 2022-03-21 11:44:51 +08:00
parent 65f7214e67
commit c9be9b0538
7 changed files with 95 additions and 24 deletions

View File

@ -2,7 +2,7 @@
NOW = $(shell date -u '+%Y%m%d%I%M%S') NOW = $(shell date -u '+%Y%m%d%I%M%S')
RELEASE_VERSION = 5.4.1 RELEASE_VERSION = 5.5.0
APP = n9e APP = n9e
SERVER_BIN = $(APP) SERVER_BIN = $(APP)

View File

@ -128,6 +128,8 @@ insert into `role_operation`(role_name, operation) values('Standard', '/job-task
CREATE TABLE `busi_group` ( CREATE TABLE `busi_group` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`name` varchar(191) not null, `name` varchar(191) not null,
`label_enable` tinyint(1) not null default 0,
`label_value` varchar(191) not null default '' comment 'if label_enable: label_value can not be blank',
`create_at` bigint not null default 0, `create_at` bigint not null default 0,
`create_by` varchar(64) not null default '', `create_by` varchar(64) not null default '',
`update_at` bigint not null default 0, `update_at` bigint not null default 0,

View File

@ -11,6 +11,8 @@ import (
type BusiGroup struct { type BusiGroup struct {
Id int64 `json:"id" gorm:"primaryKey"` Id int64 `json:"id" gorm:"primaryKey"`
Name string `json:"name"` Name string `json:"name"`
LabelEnable int `json:"label_enable"`
LabelValue string `json:"label_value"`
CreateAt int64 `json:"create_at"` CreateAt int64 `json:"create_at"`
CreateBy string `json:"create_by"` CreateBy string `json:"create_by"`
UpdateAt int64 `json:"update_at"` UpdateAt int64 `json:"update_at"`
@ -51,6 +53,21 @@ func (bg *BusiGroup) FillUserGroups() error {
return nil return nil
} }
func BusiGroupGetMap() (map[int64]*BusiGroup, error) {
var lst []*BusiGroup
err := DB().Find(&lst).Error
if err != nil {
return nil, err
}
ret := make(map[int64]*BusiGroup)
for i := 0; i < len(lst); i++ {
ret[lst[i].Id] = lst[i]
}
return ret, nil
}
func BusiGroupGet(where string, args ...interface{}) (*BusiGroup, error) { func BusiGroupGet(where string, args ...interface{}) (*BusiGroup, error) {
var lst []*BusiGroup var lst []*BusiGroup
err := DB().Where(where, args...).Find(&lst).Error err := DB().Where(where, args...).Find(&lst).Error
@ -196,8 +213,8 @@ func (bg *BusiGroup) DelMembers(members []BusiGroupMember, username string) erro
}).Error }).Error
} }
func (bg *BusiGroup) Update(name string, updateBy string) error { func (bg *BusiGroup) Update(name string, labelEnable int, labelValue string, updateBy string) error {
if bg.Name == name { if bg.Name == name && bg.LabelEnable == labelEnable && bg.LabelValue == labelValue {
return nil return nil
} }
@ -210,14 +227,29 @@ func (bg *BusiGroup) Update(name string, updateBy string) error {
return errors.New("BusiGroup already exists") return errors.New("BusiGroup already exists")
} }
if labelEnable == 1 {
exists, err = BusiGroupExists("label_enable = 1 and label_value = ? and id <> ?", labelValue, bg.Id)
if err != nil {
return errors.WithMessage(err, "failed to count BusiGroup")
}
if exists {
return errors.New("BusiGroup already exists")
}
} else {
labelValue = ""
}
return DB().Model(bg).Updates(map[string]interface{}{ return DB().Model(bg).Updates(map[string]interface{}{
"name": name, "name": name,
"label_enable": labelEnable,
"label_value": labelValue,
"update_at": time.Now().Unix(), "update_at": time.Now().Unix(),
"update_by": updateBy, "update_by": updateBy,
}).Error }).Error
} }
func BusiGroupAdd(name string, members []BusiGroupMember, creator string) error { func BusiGroupAdd(name string, labelEnable int, labelValue string, members []BusiGroupMember, creator string) error {
exists, err := BusiGroupExists("name=?", name) exists, err := BusiGroupExists("name=?", name)
if err != nil { if err != nil {
return errors.WithMessage(err, "failed to count BusiGroup") return errors.WithMessage(err, "failed to count BusiGroup")
@ -227,6 +259,19 @@ func BusiGroupAdd(name string, members []BusiGroupMember, creator string) error
return errors.New("BusiGroup already exists") return errors.New("BusiGroup already exists")
} }
if labelEnable == 1 {
exists, err = BusiGroupExists("label_enable = 1 and label_value = ?", labelValue)
if err != nil {
return errors.WithMessage(err, "failed to count BusiGroup")
}
if exists {
return errors.New("BusiGroup already exists")
}
} else {
labelValue = ""
}
count := len(members) count := len(members)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
ug, err := UserGroupGet("id=?", members[i].UserGroupId) ug, err := UserGroupGet("id=?", members[i].UserGroupId)
@ -242,6 +287,8 @@ func BusiGroupAdd(name string, members []BusiGroupMember, creator string) error
now := time.Now().Unix() now := time.Now().Unix()
obj := &BusiGroup{ obj := &BusiGroup{
Name: name, Name: name,
LabelEnable: labelEnable,
LabelValue: labelValue,
CreateAt: now, CreateAt: now,
CreateBy: creator, CreateBy: creator,
UpdateAt: now, UpdateAt: now,

View File

@ -127,6 +127,19 @@ func TargetGetsByCluster(cluster string) ([]*Target, error) {
var lst []*Target var lst []*Target
err := session.Find(&lst).Error err := session.Find(&lst).Error
if err == nil {
bgcache, err := BusiGroupGetMap()
if err != nil {
return nil, err
}
for i := 0; i < len(lst); i++ {
err = lst[i].FillGroup(bgcache)
if err != nil {
return nil, err
}
}
}
return lst, err return lst, err
} }

View File

@ -122,6 +122,13 @@ func syncTargets() error {
} }
lst[i].TagsMap[arr[0]] = arr[1] lst[i].TagsMap[arr[0]] = arr[1]
} }
// handle BusiGroup's LabelValue
// BusiGroup的LabelValue就相当于一个特殊的标签来对待
if lst[i].GroupObj != nil && lst[i].GroupObj.LabelEnable == 1 {
lst[i].TagsMap["busigroup"] = lst[i].GroupObj.LabelValue
}
m[lst[i].Ident] = lst[i] m[lst[i].Ident] = lst[i]
} }

View File

@ -13,6 +13,8 @@ import (
type busiGroupForm struct { type busiGroupForm struct {
Name string `json:"name" binding:"required"` Name string `json:"name" binding:"required"`
LabelEnable int `json:"label_enable"`
LabelValue string `json:"label_value"`
Members []models.BusiGroupMember `json:"members"` Members []models.BusiGroupMember `json:"members"`
} }
@ -37,7 +39,7 @@ func busiGroupAdd(c *gin.Context) {
} }
username := c.MustGet("username").(string) username := c.MustGet("username").(string)
ginx.Dangerous(models.BusiGroupAdd(f.Name, f.Members, username)) ginx.Dangerous(models.BusiGroupAdd(f.Name, f.LabelEnable, f.LabelValue, f.Members, username))
// 如果创建成功拿着name去查应该可以查到 // 如果创建成功拿着name去查应该可以查到
newbg, err := models.BusiGroupGet("name=?", f.Name) newbg, err := models.BusiGroupGet("name=?", f.Name)
@ -57,7 +59,7 @@ func busiGroupPut(c *gin.Context) {
username := c.MustGet("username").(string) username := c.MustGet("username").(string)
targetbg := c.MustGet("busi_group").(*models.BusiGroup) targetbg := c.MustGet("busi_group").(*models.BusiGroup)
ginx.NewRender(c).Message(targetbg.Update(f.Name, username)) ginx.NewRender(c).Message(targetbg.Update(f.Name, f.LabelEnable, f.LabelValue, username))
} }
func busiGroupMemberAdd(c *gin.Context) { func busiGroupMemberAdd(c *gin.Context) {

View File

@ -311,7 +311,7 @@ func createTokens(signingKey, userIdentity string) (*TokenDetails, error) {
func verifyToken(signingKey, tokenString string) (*jwt.Token, error) { func verifyToken(signingKey, tokenString string) (*jwt.Token, error) {
if tokenString == "" { if tokenString == "" {
return nil, fmt.Errorf("Bearer token not found") return nil, fmt.Errorf("bearer token not found")
} }
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {