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')
RELEASE_VERSION = 5.4.1
RELEASE_VERSION = 5.5.0
APP = n9e
SERVER_BIN = $(APP)

View File

@ -128,6 +128,8 @@ insert into `role_operation`(role_name, operation) values('Standard', '/job-task
CREATE TABLE `busi_group` (
`id` bigint unsigned not null auto_increment,
`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_by` varchar(64) not null default '',
`update_at` bigint not null default 0,

View File

@ -9,13 +9,15 @@ import (
)
type BusiGroup struct {
Id int64 `json:"id" gorm:"primaryKey"`
Name string `json:"name"`
CreateAt int64 `json:"create_at"`
CreateBy string `json:"create_by"`
UpdateAt int64 `json:"update_at"`
UpdateBy string `json:"update_by"`
UserGroups []UserGroupWithPermFlag `json:"user_groups" gorm:"-"`
Id int64 `json:"id" gorm:"primaryKey"`
Name string `json:"name"`
LabelEnable int `json:"label_enable"`
LabelValue string `json:"label_value"`
CreateAt int64 `json:"create_at"`
CreateBy string `json:"create_by"`
UpdateAt int64 `json:"update_at"`
UpdateBy string `json:"update_by"`
UserGroups []UserGroupWithPermFlag `json:"user_groups" gorm:"-"`
}
type UserGroupWithPermFlag struct {
@ -51,6 +53,21 @@ func (bg *BusiGroup) FillUserGroups() error {
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) {
var lst []*BusiGroup
err := DB().Where(where, args...).Find(&lst).Error
@ -196,8 +213,8 @@ func (bg *BusiGroup) DelMembers(members []BusiGroupMember, username string) erro
}).Error
}
func (bg *BusiGroup) Update(name string, updateBy string) error {
if bg.Name == name {
func (bg *BusiGroup) Update(name string, labelEnable int, labelValue string, updateBy string) error {
if bg.Name == name && bg.LabelEnable == labelEnable && bg.LabelValue == labelValue {
return nil
}
@ -210,14 +227,29 @@ func (bg *BusiGroup) Update(name string, updateBy string) error {
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{}{
"name": name,
"update_at": time.Now().Unix(),
"update_by": updateBy,
"name": name,
"label_enable": labelEnable,
"label_value": labelValue,
"update_at": time.Now().Unix(),
"update_by": updateBy,
}).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)
if err != nil {
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")
}
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)
for i := 0; i < count; i++ {
ug, err := UserGroupGet("id=?", members[i].UserGroupId)
@ -241,11 +286,13 @@ func BusiGroupAdd(name string, members []BusiGroupMember, creator string) error
now := time.Now().Unix()
obj := &BusiGroup{
Name: name,
CreateAt: now,
CreateBy: creator,
UpdateAt: now,
UpdateBy: creator,
Name: name,
LabelEnable: labelEnable,
LabelValue: labelValue,
CreateAt: now,
CreateBy: creator,
UpdateAt: now,
UpdateBy: creator,
}
return DB().Transaction(func(tx *gorm.DB) error {

View File

@ -127,6 +127,19 @@ func TargetGetsByCluster(cluster string) ([]*Target, error) {
var lst []*Target
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
}

View File

@ -122,6 +122,13 @@ func syncTargets() error {
}
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]
}

View File

@ -12,8 +12,10 @@ import (
)
type busiGroupForm struct {
Name string `json:"name" binding:"required"`
Members []models.BusiGroupMember `json:"members"`
Name string `json:"name" binding:"required"`
LabelEnable int `json:"label_enable"`
LabelValue string `json:"label_value"`
Members []models.BusiGroupMember `json:"members"`
}
func busiGroupAdd(c *gin.Context) {
@ -37,7 +39,7 @@ func busiGroupAdd(c *gin.Context) {
}
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去查应该可以查到
newbg, err := models.BusiGroupGet("name=?", f.Name)
@ -57,7 +59,7 @@ func busiGroupPut(c *gin.Context) {
username := c.MustGet("username").(string)
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) {

View File

@ -311,7 +311,7 @@ func createTokens(signingKey, userIdentity string) (*TokenDetails, error) {
func verifyToken(signingKey, tokenString string) (*jwt.Token, error) {
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) {