Skip to content

Commit

Permalink
add label_value field for busi_group
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed Mar 21, 2022
1 parent 65f7214 commit c9be9b0
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions docker/initsql/a-n9e.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
83 changes: 65 additions & 18 deletions src/models/busi_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions src/models/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions src/server/memsto/target_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
10 changes: 6 additions & 4 deletions src/webapi/router/router_busi_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/webapi/router/router_mw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c9be9b0

Please sign in to comment.