diff --git a/Gopkg.lock b/Gopkg.lock index 35c2526f1c..4482016efa 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1124,7 +1124,7 @@ [[projects]] branch = "master" - digest = "1:253553e4aea4744856900c4015036177f56ddfa4400a4422a2aab8cb1737bd00" + digest = "1:e5dd1b8806ddb16ddd31b64c2208d0cc566cd184d12f63844c33581c28d7fa50" name = "yunion.io/x/pkg" packages = [ "gotypes", @@ -1158,7 +1158,7 @@ "utils", ] pruneopts = "UT" - revision = "6a2c5061c44f5e4a813a87553014ef72d1b983ea" + revision = "98dfb17dd78a596fbe5542e8276f4c717dc752fe" [[projects]] branch = "master" diff --git a/pkg/compute/capabilities/handler.go b/pkg/compute/capabilities/handler.go index 4bed41373b..a6f0aa9378 100644 --- a/pkg/compute/capabilities/handler.go +++ b/pkg/compute/capabilities/handler.go @@ -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)) -} \ No newline at end of file +} diff --git a/pkg/compute/models/capabilities.go b/pkg/compute/models/capabilities.go index 55c202ed89..9d960b2fde 100644 --- a/pkg/compute/models/capabilities.go +++ b/pkg/compute/models/capabilities.go @@ -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 } -} \ No newline at end of file +} diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index e31305517b..53cbe4a730 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -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() diff --git a/pkg/compute/models/specs.go b/pkg/compute/models/specs.go index 93bdfb363e..841d1c87f8 100644 --- a/pkg/compute/models/specs.go +++ b/pkg/compute/models/specs.go @@ -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 } diff --git a/pkg/compute/models/zones.go b/pkg/compute/models/zones.go index f2c82736c7..8b1cc8c57e 100644 --- a/pkg/compute/models/zones.go +++ b/pkg/compute/models/zones.go @@ -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 }