Skip to content

Commit

Permalink
[CLI] agent repo add orgID
Browse files Browse the repository at this point in the history
  • Loading branch information
jin-xiaofeng committed May 23, 2024
1 parent 86f0140 commit 8e96d4f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
8 changes: 4 additions & 4 deletions cli/ctl/agent_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions server/controller/http/router/vtap_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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))
}
38 changes: 26 additions & 12 deletions server/controller/http/service/vtap_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
Expand All @@ -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
Expand Down

0 comments on commit 8e96d4f

Please sign in to comment.