Browse Source

add stub for search guests/modify auto start

dev
akumas 7 months ago
parent
commit
b56f940459
  1. 4
      core_service.go
  2. 122
      src/modules/api_module.go
  3. 13
      src/modules/restful_data.go

4
core_service.go

@ -7,7 +7,7 @@ import (
)
const (
CurrentVersion = "1.3.0"
CurrentVersion = "1.3.1"
)
type CoreService struct {
@ -88,6 +88,8 @@ func (core *CoreService)OnMessageReceived(msg framework.Message){
case framework.CreateGuestRequest:
case framework.DeleteGuestRequest:
case framework.ResetSystemRequest:
case framework.SearchGuestRequest:
case framework.ModifyAutoStartRequest:
case framework.QueryInstanceStatusRequest:
case framework.GetInstanceStatusRequest:
case framework.StartInstanceRequest:

122
src/modules/api_module.go

@ -414,6 +414,7 @@ func (module *APIModule) RegisterAPIHandler(router *httprouter.Router) {
router.PUT(apiPath("/guests/:id/name/"), module.handleModifyGuestName)
router.PUT(apiPath("/guests/:id/cores"), module.handleModifyGuestCores)
router.PUT(apiPath("/guests/:id/memory"), module.handleModifyGuestMemory)
router.PUT(apiPath("/guests/:id/auto_start"), module.handleModifyAutoStart)
router.PUT(apiPath("/guests/:id/qos/cpu"), module.handleModifyGuestPriority)
router.PUT(apiPath("/guests/:id/qos/disk"), module.handleModifyDiskThreshold)
router.PUT(apiPath("/guests/:id/qos/network"), module.handleModifyNetworkThreshold)
@ -517,6 +518,7 @@ func (module *APIModule) RegisterAPIHandler(router *httprouter.Router) {
//search resource
router.GET(apiPath("/search/security_policy_groups/*filepath"), module.querySecurityPolicyGroups)
router.POST(apiPath("/search/guests/"), module.searchGuests)
}
func (module *APIModule) queryZoneStatistic(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
@ -3334,6 +3336,42 @@ func (module *APIModule) handleModifyGuestMemory(w http.ResponseWriter, r *http.
ResponseOK("", w)
}
func (module *APIModule) handleModifyAutoStart(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if err := module.verifyRequestSignature(r); err != nil{
ResponseFail(ResponseDefaultError, err.Error(), w)
return
}
var id= params.ByName("id")
type userRequest struct {
Enable bool `json:"enable"`
}
decoder := json.NewDecoder(r.Body)
var request userRequest
if err := decoder.Decode(&request);err != nil{
log.Printf("<api> parse modify memory request fail: %s", err.Error())
ResponseFail(ResponseDefaultError, err.Error(), w)
return
}
msg, _ := framework.CreateJsonMessage(framework.ModifyAutoStartRequest)
msg.SetString(framework.ParamKeyGuest, id)
msg.SetBoolean(framework.ParamKeyEnable, request.Enable)
var respChan = make(chan ProxyResult)
if err := module.proxy.SendRequest(msg, respChan); err != nil {
log.Printf("<api> send modify auto start request fail: %s", err.Error())
ResponseFail(ResponseDefaultError, err.Error(), w)
return
}
_, errMsg, success := IsResponseSuccess(respChan)
if !success {
log.Printf("<api> modify auto start fail: %s", errMsg)
ResponseFail(ResponseDefaultError, errMsg, w)
return
}
ResponseOK("", w)
}
func (module *APIModule) handleModifyGuestPriority(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if err := module.verifyRequestSignature(r); err != nil{
ResponseFail(ResponseDefaultError, err.Error(), w)
@ -5734,6 +5772,90 @@ func (module *APIModule) querySecurityPolicyGroups(w http.ResponseWriter, r *htt
ResponseOK(payload, w)
}
func (module *APIModule) searchGuests(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if err := module.verifyRequestSignature(r); err != nil{
ResponseFail(ResponseDefaultError, err.Error(), w)
return
}
const (
FlagOffsetTotal = iota
FlagOffsetLimit
FlagOffsetOffset
DefaultLimit = 20
ValidFlagCount = 3
)
type userRequest struct {
Limit int `json:"limit"`
Offset int `json:"offset,omitempty"`
Pool string `json:"pool,omitempty"`
Cell string `json:"cell,omitempty"`
Keyword string `json:"keyword,omitempty"`
}
decoder := json.NewDecoder(r.Body)
var request userRequest
if err := decoder.Decode(&request);err != nil{
log.Printf("<api> parse search guests request fail: %s", err.Error())
ResponseFail(ResponseDefaultError, err.Error(), w)
return
}
if 0 >= request.Limit{
request.Limit = DefaultLimit
}
msg, _ := framework.CreateJsonMessage(framework.SearchGuestRequest)
msg.SetInt(framework.ParamKeyLimit, request.Limit)
msg.SetInt(framework.ParamKeyStart, request.Offset)
msg.SetString(framework.ParamKeyPool, request.Pool)
msg.SetString(framework.ParamKeyCell, request.Cell)
msg.SetString(framework.ParamKeyData, request.Keyword)
respChan := make(chan ProxyResult)
if err := module.proxy.SendRequest(msg, respChan); err != nil {
log.Printf("<api> send query instance in pool fail: %s", err.Error())
ResponseFail(ResponseDefaultError, err.Error(), w)
return
}
resp, errMsg, success := IsResponseSuccess(respChan)
if !success {
log.Printf("<api> search guests fail: %s", errMsg)
ResponseFail(ResponseDefaultError, errMsg, w)
return
}
type responsePayload struct {
Result []restGuestConfig `json:"result"`
Total int `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
var payload responsePayload
var err error
if payload.Result, err = UnmarshalGuestConfigListFromMessage(resp); err != nil{
log.Printf("<api> parse search guests result fail: %s", err.Error())
ResponseFail(ResponseDefaultError, err.Error(), w)
return
}
var flags []uint64
if flags, err = resp.GetUIntArray(framework.ParamKeyFlag); err != nil{
log.Printf("<api> parse search guests flags fail: %s", err.Error())
ResponseFail(ResponseDefaultError, err.Error(), w)
return
}
if ValidFlagCount != len(flags){
log.Printf("<api> unexpected search guests flags count %d => %d", len(flags), ValidFlagCount)
ResponseFail(ResponseDefaultError, err.Error(), w)
return
}
payload.Total = int(flags[FlagOffsetTotal])
payload.Limit = int(flags[FlagOffsetLimit])
payload.Offset = int(flags[FlagOffsetOffset])
ResponseOK(payload, w)
}
func (module *APIModule) getSecurityPolicyGroup(w http.ResponseWriter, r *http.Request, params httprouter.Params){
var err = module.verifyRequestSignature(r)
if err != nil{

13
src/modules/restful_data.go

@ -24,6 +24,7 @@ type restGuestConfig struct {
Group string `json:"group"`
Pool string `json:"pool,omitempty"`
Cell string `json:"cell,omitempty"`
Host string `json:"host,omitempty"`
Cores uint `json:"cores"`
Memory uint `json:"memory"`
TotalDisk uint64 `json:"total_disk"`
@ -76,7 +77,8 @@ func UnmarshalGuestConfigListFromMessage(msg framework.Message) (result []restGu
if err != nil{
return result, err
}
var names, ids, pools, cells, users, groups, monitors, addresses, systems, createTime, internal, external, hardware []string
var names, ids, pools, cells, hosts, users, groups, monitors, addresses, systems,
createTime, internal, external, hardware []string
var cores, options, enables, progress, status, memories, disks, diskCounts, mediaAttached, cpuPriorities, ioLimits []uint64
if pools, err = msg.GetStringArray(framework.ParamKeyPool); err != nil {
return result, err
@ -84,6 +86,12 @@ func UnmarshalGuestConfigListFromMessage(msg framework.Message) (result []restGu
if cells, err = msg.GetStringArray(framework.ParamKeyCell); err != nil {
return result, err
}
if hosts, err = msg.GetStringArray(framework.ParamKeyHost); nil == err{
if int(count) != len(hosts){
err = fmt.Errorf("unexpected hosts count %d / %d", len(hosts), count)
return
}
}
if names, err = msg.GetStringArray(framework.ParamKeyName); err != nil {
return result, err
}
@ -178,6 +186,9 @@ func UnmarshalGuestConfigListFromMessage(msg framework.Message) (result []restGu
var config restGuestConfig
config.Pool = pools[i]
config.Cell = cells[i]
if 0 != len(hosts){
config.Host = hosts[i]
}
config.Name = names[i]
config.ID = ids[i]
config.Owner = users[i]

Loading…
Cancel
Save