Skip to content

Commit

Permalink
[Server] add team id to agent, agent group, agent group config
Browse files Browse the repository at this point in the history
  • Loading branch information
roryye authored and SongZhen0704 committed Apr 25, 2024
1 parent 9bae310 commit c29adff
Show file tree
Hide file tree
Showing 19 changed files with 756 additions and 348 deletions.
1 change: 1 addition & 0 deletions server/agent_config/json.go
Expand Up @@ -89,4 +89,5 @@ type AgentGroupConfigResponse struct {
AnalyzerIP *string `json:"ANALYZER_IP"`
WasmPlugins []string `json:"WASM_PLUGINS"`
SoPlugins []string `json:"SO_PLUGINS"`
TeamID int `json:"TEAM_ID"`
}
6 changes: 4 additions & 2 deletions server/controller/common/const.go
Expand Up @@ -677,6 +677,8 @@ const (
)

const (
HEADER_KEY_X_ORG_ID = "X-Org-Id"
INGESTER_BODY_ORG_ID = "org-id"
HEADER_KEY_X_ORG_ID = "X-Org-Id"
INGESTER_BODY_ORG_ID = "org-id"
HEADER_KEY_X_USER_TYPE = "X-User-Type"
HEADER_KEY_X_USER_ID = "X-User-Id"
)
2 changes: 2 additions & 0 deletions server/controller/db/mysql/migration/rawsql/init.sql
Expand Up @@ -1057,6 +1057,7 @@ TRUNCATE TABLE vtap;

CREATE TABLE IF NOT EXISTS vtap_group (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
team_id INTEGER,
name VARCHAR(64) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Expand Down Expand Up @@ -1615,6 +1616,7 @@ TRUNCATE TABLE policy_acl_group;

CREATE TABLE IF NOT EXISTS vtap_group_configuration(
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
team_id INTEGER,
max_collect_pps INTEGER DEFAULT NULL,
max_npb_bps BIGINT DEFAULT NULL COMMENT 'unit: bps',
max_cpus INTEGER DEFAULT NULL,
Expand Down
37 changes: 37 additions & 0 deletions server/controller/db/mysql/migration/rawsql/issu/6.5.1.24.sql
@@ -0,0 +1,37 @@
-- modify start, add upgrade sql
DROP PROCEDURE IF EXISTS AddColumnIfNotExists;

CREATE PROCEDURE AddColumnIfNotExists(
IN tableName VARCHAR(255),
IN colName VARCHAR(255),
IN colType VARCHAR(255)
)
BEGIN
DECLARE column_count INT;

-- 检查列是否存在
SELECT COUNT(*)
INTO column_count
FROM information_schema.columns
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = tableName
AND column_name = colName;

-- 如果列不存在,则添加列
IF column_count = 0 THEN
SET @sql = CONCAT('ALTER TABLE ', tableName, ' ADD COLUMN ', colName, ' ', colType);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
END;

CALL AddColumnIfNotExists('vtap_group', 'team_id', 'INTEGER');

DROP PROCEDURE AddColumnIfNotExists;

update vtap_group set team_id=1 where team_id is NULL;

-- update db_version to latest, remeber update DB_VERSION_EXPECT in migrate/init.go
UPDATE db_version SET version='6.5.1.24';
-- modify end
2 changes: 1 addition & 1 deletion server/controller/db/mysql/migration/version.go
Expand Up @@ -18,5 +18,5 @@ package migration

const (
DB_VERSION_TABLE = "db_version"
DB_VERSION_EXPECTED = "6.5.1.23"
DB_VERSION_EXPECTED = "6.5.1.24"
)
1 change: 1 addition & 0 deletions server/controller/db/mysql/model.go
Expand Up @@ -211,6 +211,7 @@ type VTapGroup struct {
UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;not null;default:CURRENT_TIMESTAMP" json:"UPDATED_AT"`
Lcuuid string `gorm:"column:lcuuid;type:char(64);not null" json:"LCUUID"`
ShortUUID string `gorm:"column:short_uuid;type:char(32);default:null" json:"SHORT_UUID"`
TeamID int `gorm:"column:team_id;type:int;default:0" json:"TEAM_ID"`
}

func (VTapGroup) TableName() string {
Expand Down
27 changes: 27 additions & 0 deletions server/controller/http/middleware.go
Expand Up @@ -17,6 +17,7 @@
package http

import (
"errors"
"fmt"
"strconv"

Expand All @@ -42,6 +43,32 @@ func HandleOrgIDMiddleware() gin.HandlerFunc {
}
}
ctx.Set(common.HEADER_KEY_X_ORG_ID, orgID)

var err error
var userType, userID int
userTypeString := ctx.Request.Header.Get(common.HEADER_KEY_X_USER_TYPE)
if userTypeString == "" {
log.Warningf("header is invalid: no %s", common.HEADER_KEY_X_USER_TYPE)
} else {
userType, err = strconv.Atoi(userTypeString)
if err != nil {
ctx.Abort()
return
}
}
userIDString := ctx.Request.Header.Get(common.HEADER_KEY_X_USER_ID)
if len(userIDString) == 0 {
err = errors.New(fmt.Sprintf("header is invalid: no %s", common.HEADER_KEY_X_USER_ID))
} else {
userID, err = strconv.Atoi(userIDString)
if err != nil {
ctx.Abort()
return
}
}
ctx.Set(common.HEADER_KEY_X_USER_TYPE, userType)
ctx.Set(common.HEADER_KEY_X_USER_ID, userID)

ctx.Next()
}
}

0 comments on commit c29adff

Please sign in to comment.