Skip to content

Commit

Permalink
#2472 validate resource server
Browse files Browse the repository at this point in the history
  • Loading branch information
zgyzgyhero committed Jan 9, 2025
1 parent 088f0aa commit a13a1be
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
24 changes: 22 additions & 2 deletions platform-core/api/v1/system/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ func CreateResourceServer(c *gin.Context) {
middleware.ReturnError(c, exterror.Catch(exterror.New().RequestParamValidateError, err))
return
}
err := database.CreateResourceServer(c, params)
var err error
for _, v := range params {
if err = database.ValidateResourceServer(c, v); err != nil {
break
}
}
if err != nil {
middleware.ReturnError(c, err)
return
}
err = database.CreateResourceServer(c, params)
if err != nil {
middleware.ReturnError(c, err)
} else {
Expand All @@ -80,7 +90,17 @@ func UpdateResourceServer(c *gin.Context) {
middleware.ReturnError(c, exterror.Catch(exterror.New().RequestParamValidateError, err))
return
}
err := database.UpdateResourceServer(c, params)
var err error
for _, v := range params {
if err = database.ValidateResourceServer(c, v); err != nil {
break
}
}
if err != nil {
middleware.ReturnError(c, err)
return
}
err = database.UpdateResourceServer(c, params)
if err != nil {
middleware.ReturnError(c, err)
} else {
Expand Down
5 changes: 3 additions & 2 deletions platform-core/services/database/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,14 @@ func GetResourceServer(ctx context.Context, serverType, serverIp, name, resource
//}
if serverType == "mysql" && name == "" {
for _, row := range resourceServerRows {
if row.Name == "plugin" {
if row.IsAllocated {
resourceServerObj = row
break
}
}
if resourceServerObj == nil {
resourceServerObj = resourceServerRows[0]
err = fmt.Errorf("can not find allocate mysql resource")
return
}
} else {
resourceServerObj = resourceServerRows[0]
Expand Down
28 changes: 28 additions & 0 deletions platform-core/services/database/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,31 @@ func GetResourceItem(ctx context.Context, resourceType, name string, isAllocated
}
return
}

func ValidateResourceServer(ctx context.Context, resourceServer *models.ResourceServer) (err error) {
if resourceServer.Type == "docker" {
return
}
if resourceServer.Status == "active" && resourceServer.IsAllocated {
queryResult, queryErr := db.MysqlEngine.Context(ctx).QueryString("select id from resource_server where `type`=? and status='active' and is_allocated=1", resourceServer.Type)
if queryErr != nil {
err = fmt.Errorf("query resource server fail,%s ", queryErr.Error())
return
}
if len(queryResult) > 0 {
legalFlag := false
if resourceServer.Id != "" {
for _, v := range queryResult {
if v["id"] == resourceServer.Id {
legalFlag = true
break
}
}
}
if !legalFlag {
err = fmt.Errorf("resource server validate fail,already have an active&&allocated %s resource", resourceServer.Type)
}
}
}
return
}

0 comments on commit a13a1be

Please sign in to comment.