mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
region: capabilities add specs
This commit is contained in:
@@ -1,22 +1,33 @@
|
||||
package capabilities
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
||||
"yunion.io/x/onecloud/pkg/appsrv"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
||||
)
|
||||
|
||||
func AddCapabilityHandler(prefix string, app *appsrv.Application) {
|
||||
app.AddHandler2("GET", fmt.Sprintf("%s/capabilities", prefix), auth.Authenticate(capaHandler), nil, "get_capabilities", nil)
|
||||
}
|
||||
|
||||
func capaHandler(context context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
capa := models.GetCapabilities(nil)
|
||||
func capaHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
userCred := auth.FetchUserCredential(ctx)
|
||||
query, err := jsonutils.ParseQueryString(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
httperrors.GeneralServerError(w, err)
|
||||
return
|
||||
}
|
||||
capa, err := models.GetCapabilities(ctx, userCred, query, nil)
|
||||
if err != nil {
|
||||
httperrors.GeneralServerError(w, err)
|
||||
return
|
||||
}
|
||||
appsrv.SendJSON(w, jsonutils.Marshal(capa))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/sqlchemy"
|
||||
"yunion.io/x/jsonutils"
|
||||
)
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
)
|
||||
|
||||
type SCapabilities struct {
|
||||
Hypervisors []string
|
||||
@@ -18,10 +20,10 @@ type SCapabilities struct {
|
||||
MaxDataDiskCount int
|
||||
SchedPolicySupport bool
|
||||
Usable bool
|
||||
Specs jsonutils.JSONObject
|
||||
Specs jsonutils.JSONObject
|
||||
}
|
||||
|
||||
func GetCapabilities(zone *SZone) SCapabilities {
|
||||
func GetCapabilities(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, zone *SZone) (SCapabilities, error) {
|
||||
capa := SCapabilities{}
|
||||
capa.Hypervisors = getHypervisors(zone)
|
||||
capa.StorageTypes = getStorageTypes(zone)
|
||||
@@ -32,8 +34,13 @@ func GetCapabilities(zone *SZone) SCapabilities {
|
||||
capa.MinDataDiskCount = getMinDataDiskCount(zone)
|
||||
capa.MaxDataDiskCount = getMaxDataDiskCount(zone)
|
||||
capa.Usable = isUsable(zone)
|
||||
capa.Specs =
|
||||
return capa
|
||||
if query == nil {
|
||||
query = jsonutils.NewDict()
|
||||
}
|
||||
var err error
|
||||
mans := []ISpecModelManager{HostManager, IsolatedDeviceManager}
|
||||
capa.Specs, err = GetModelsSpecs(ctx, userCred, query.(*jsonutils.JSONDict), mans...)
|
||||
return capa, err
|
||||
}
|
||||
|
||||
func getHypervisors(zone *SZone) []string {
|
||||
@@ -164,4 +171,4 @@ func isUsable(zone *SZone) bool {
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -520,7 +520,6 @@ func (self *SHost) GetSpec(statusCheck bool) *jsonutils.JSONDict {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
log.Errorf("===***start getspec: %s", self.Id)
|
||||
spec := self.GetHardwareSpecification()
|
||||
spec.Remove("storage_info")
|
||||
nifs := self.GetNetInterfaces()
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
@@ -24,17 +23,18 @@ type ISpecModel interface {
|
||||
}
|
||||
|
||||
func GetAllModelSpecs(ctx context.Context, userCred mcclient.TokenCredential, query *jsonutils.JSONDict) (jsonutils.JSONObject, error) {
|
||||
mans := []ISpecModelManager{HostManager, IsolatedDeviceManager, GuestManager}
|
||||
return GetModelsSpecs(ctx, userCred, query, mans...)
|
||||
}
|
||||
|
||||
func GetModelsSpecs(ctx context.Context, userCred mcclient.TokenCredential, query *jsonutils.JSONDict, managers ...ISpecModelManager) (jsonutils.JSONObject, error) {
|
||||
ret := jsonutils.NewDict()
|
||||
for keyword, man := range map[string]ISpecModelManager{
|
||||
HostManager.KeywordPlural(): HostManager,
|
||||
IsolatedDeviceManager.KeywordPlural(): IsolatedDeviceManager,
|
||||
GuestManager.KeywordPlural(): GuestManager,
|
||||
} {
|
||||
for _, man := range managers {
|
||||
spec, err := getModelSpecs(man, ctx, userCred, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret.Add(spec, keyword)
|
||||
ret.Add(spec, man.KeywordPlural())
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
@@ -63,7 +63,6 @@ func getModelSpecs(manager ISpecModelManager, ctx context.Context, userCred mccl
|
||||
if spec == nil {
|
||||
continue
|
||||
}
|
||||
log.Errorf("=========get %s spec: %s", specObj.GetShortDesc(), spec)
|
||||
specKeys := manager.GetSpecIdent(spec)
|
||||
sort.Strings(specKeys)
|
||||
specKey := strings.Join(specKeys, "/")
|
||||
@@ -76,7 +75,6 @@ func getModelSpecs(manager ISpecModelManager, ctx context.Context, userCred mccl
|
||||
retDict.Set(specKey, oldSpec)
|
||||
}
|
||||
}
|
||||
log.Errorf("========ret specdict: %s", retDict)
|
||||
return retDict, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -479,7 +479,10 @@ func (self *SZone) AllowGetDetailsCapability(ctx context.Context, userCred mccli
|
||||
}
|
||||
|
||||
func (self *SZone) GetDetailsCapability(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
capa := GetCapabilities(self)
|
||||
capa, err := GetCapabilities(ctx, userCred, query, self)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jsonutils.Marshal(&capa), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user