diff --git a/cli/ctl/agent_repo.go b/cli/ctl/agent_repo.go index 0d997655c65..c3e2fca5fc7 100644 --- a/cli/ctl/agent_repo.go +++ b/cli/ctl/agent_repo.go @@ -55,7 +55,7 @@ func RegisterRepoCommand() *cobra.Command { } var repoAgentCreateExample = `deepflow-ctl repo agent create --arch x86 --image deepflow-agent -deepflow-ctl repo agent create --arch x86 --version-image /root/deepflow-agent --image deepflow-agent.exe +deepflow-ctl repo agent create --arch x86 --version-image /root/deepflow-agent --image deepflow-agent.exe deepflow-ctl repo agent create --arch x86 --version-image /root/deepflow-agent --k8s-image registry.cn-beijing.aliyuncs.com/deepflow-ce/deepflowio-agent:latest` func registerAgentCommand() *cobra.Command { @@ -175,7 +175,7 @@ func createRepoAgent(cmd *cobra.Command, arch, image, versionImage, k8sImage str server := common.GetServerInfo(cmd) url := fmt.Sprintf("http://%s:%d/v1/vtap-repo/", server.IP, server.Port) - resp, err := common.CURLPostFormData(url, contentType, bodyBuf, []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...) + resp, err := common.CURLPostFormData(url, contentType, bodyBuf, []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...) if err != nil { return err } @@ -217,7 +217,7 @@ func getAgentInfo(s string) (branch, revCount, commitID string) { func listRepoAgent(cmd *cobra.Command) { server := common.GetServerInfo(cmd) url := fmt.Sprintf("http://%s:%d/v1/vtap-repo/", server.IP, server.Port) - response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...) + response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd)), common.WithORGID(common.GetORGID(cmd))}...) if err != nil { fmt.Println(err) return @@ -264,7 +264,7 @@ func deleteRepoAgent(cmd *cobra.Command, args []string) error { server := common.GetServerInfo(cmd) url := fmt.Sprintf("http://%s:%d/v1/vtap-repo/", server.IP, server.Port) - _, err := common.CURLPerform("DELETE", url, body, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...) + _, err := common.CURLPerform("DELETE", url, body, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...) if err != nil { return err } diff --git a/server/controller/http/router/vtap_repo.go b/server/controller/http/router/vtap_repo.go index 3ab2f2bfcc7..925989acf2d 100644 --- a/server/controller/http/router/vtap_repo.go +++ b/server/controller/http/router/vtap_repo.go @@ -40,7 +40,7 @@ func (vr *VtapRepo) RegisterTo(e *gin.Engine) { } func getVtapRepo(c *gin.Context) { - data, err := service.GetVtapRepo(nil) + data, err := service.GetVtapRepo(service.GetUserInfo(c).ORGID, nil) JsonResponse(c, data, err) } @@ -72,7 +72,7 @@ func createVtapRepo(c *gin.Context) { } } - data, err := service.CreateVtapRepo(vtapRepo) + data, err := service.CreateVtapRepo(service.GetUserInfo(c).ORGID, vtapRepo) JsonResponse(c, data, err) } @@ -88,5 +88,5 @@ func deleteVtapRepo(c *gin.Context) { common.Response(c, nil, common.NewReponse("FAILED", "", nil, fmt.Sprintf("%s", err))) return } - JsonResponse(c, nil, service.DeleteVtapRepo(vtapRepo.ImageName)) + JsonResponse(c, nil, service.DeleteVtapRepo(service.GetUserInfo(c).ORGID, vtapRepo.ImageName)) } diff --git a/server/controller/http/service/vtap_repo.go b/server/controller/http/service/vtap_repo.go index 6f48b0f0863..f32011c0fec 100644 --- a/server/controller/http/service/vtap_repo.go +++ b/server/controller/http/service/vtap_repo.go @@ -33,38 +33,47 @@ const ( IMAGE_MAX_COUNT = 20 ) -func CreateVtapRepo(vtapRepoCreate *mysql.VTapRepo) (*model.VtapRepo, error) { +func CreateVtapRepo(orgID int, vtapRepoCreate *mysql.VTapRepo) (*model.VtapRepo, error) { + dbInfo, err := mysql.GetDB(orgID) + if err != nil { + return nil, err + } + db := dbInfo.DB var vtapRepoFirst mysql.VTapRepo - if err := mysql.Db.Where("name = ?", vtapRepoCreate.Name).First(&vtapRepoFirst).Error; err != nil { + if err := db.Where("name = ?", vtapRepoCreate.Name).First(&vtapRepoFirst).Error; err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return nil, NewError(httpcommon.SERVER_ERROR, fmt.Sprintf("fail to query vtap_repo by name(%s), error: %s", vtapRepoCreate.Name, err)) } var count int64 - mysql.Db.Model(&mysql.VTapRepo{}).Count(&count) + db.Model(&mysql.VTapRepo{}).Count(&count) if count >= IMAGE_MAX_COUNT { return nil, fmt.Errorf("the number of image can not exceed %d", IMAGE_MAX_COUNT) } - if err = mysql.Db.Create(&vtapRepoCreate).Error; err != nil { + if err = db.Create(&vtapRepoCreate).Error; err != nil { return nil, err } - vtapRepoes, _ := GetVtapRepo(map[string]interface{}{"name": vtapRepoCreate.Name}) + vtapRepoes, _ := GetVtapRepo(orgID, map[string]interface{}{"name": vtapRepoCreate.Name}) return &vtapRepoes[0], nil } // update by name - if err := mysql.Db.Model(&mysql.VTapRepo{}).Where("name = ?", vtapRepoCreate.Name). + if err := db.Model(&mysql.VTapRepo{}).Where("name = ?", vtapRepoCreate.Name). Updates(vtapRepoCreate).Error; err != nil { return nil, err } - vtapRepoes, _ := GetVtapRepo(map[string]interface{}{"name": vtapRepoCreate.Name}) + vtapRepoes, _ := GetVtapRepo(orgID, map[string]interface{}{"name": vtapRepoCreate.Name}) return &vtapRepoes[0], nil } -func GetVtapRepo(filter map[string]interface{}) ([]model.VtapRepo, error) { +func GetVtapRepo(orgID int, filter map[string]interface{}) ([]model.VtapRepo, error) { var vtapRepoes []mysql.VTapRepo - db := mysql.Db + dbInfo, err := mysql.GetDB(orgID) + if err != nil { + return nil, err + } + db := dbInfo.DB if _, ok := filter["name"]; ok { db = db.Where("name = ?", filter["name"]) } @@ -88,13 +97,18 @@ func GetVtapRepo(filter map[string]interface{}) ([]model.VtapRepo, error) { return resp, nil } -func DeleteVtapRepo(name string) error { +func DeleteVtapRepo(orgID int, name string) error { + dbInfo, err := mysql.GetDB(orgID) + if err != nil { + return err + } + db := dbInfo.DB var vtapRepo mysql.VTapRepo - if err := mysql.Db.Where("name = ?", name).Select("name", "id").First(&vtapRepo).Error; err != nil { + if err := db.Where("name = ?", name).Select("name", "id").First(&vtapRepo).Error; err != nil { return NewError(httpcommon.RESOURCE_NOT_FOUND, fmt.Sprintf("vtap_repo (name: %s) not found", name)) } - if err := mysql.Db.Where("name = ?", name).Delete(&mysql.VTapRepo{}).Error; err != nil { + if err := db.Where("name = ?", name).Delete(&mysql.VTapRepo{}).Error; err != nil { return NewError(httpcommon.SERVER_ERROR, fmt.Sprintf("delete vtap_repo (name: %s) failed", name)) } return nil