specs.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "context"
  17. "reflect"
  18. "sort"
  19. "strings"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. type ISpecModelManager interface {
  26. db.IStandaloneModelManager
  27. GetSpecShouldCheckStatus(query *jsonutils.JSONDict) (bool, error)
  28. GetSpecIdent(spec *jsonutils.JSONDict) []string
  29. }
  30. type ISpecModel interface {
  31. db.IStandaloneModel
  32. GetSpec(statusCheck bool) *jsonutils.JSONDict
  33. }
  34. type IBatchSpecModel interface {
  35. BatchGetModelSpecs(statusCheck bool) (jsonutils.JSONObject, error)
  36. }
  37. func GetAllModelSpecs(ctx context.Context, userCred mcclient.TokenCredential, query *jsonutils.JSONDict) (jsonutils.JSONObject, error) {
  38. mans := []ISpecModelManager{HostManager, IsolatedDeviceManager, GuestManager}
  39. return GetModelsSpecs(ctx, userCred, query, mans...)
  40. }
  41. func GetModelsSpecs(ctx context.Context, userCred mcclient.TokenCredential, query *jsonutils.JSONDict, managers ...ISpecModelManager) (jsonutils.JSONObject, error) {
  42. ret := jsonutils.NewDict()
  43. for _, man := range managers {
  44. spec, err := getModelSpecs(man, ctx, userCred, query)
  45. if err != nil {
  46. return nil, err
  47. }
  48. ret.Add(spec, man.KeywordPlural())
  49. }
  50. return ret, nil
  51. }
  52. func GetHostSpecs(ctx context.Context, userCred mcclient.TokenCredential, query *jsonutils.JSONDict) (jsonutils.JSONObject, error) {
  53. return getModelSpecs(HostManager, ctx, userCred, query)
  54. }
  55. func GetIsolatedDeviceSpecs(ctx context.Context, userCred mcclient.TokenCredential, query *jsonutils.JSONDict) (jsonutils.JSONObject, error) {
  56. return getModelSpecs(IsolatedDeviceManager, ctx, userCred, query)
  57. }
  58. func GetServerSpecs(ctx context.Context, userCred mcclient.TokenCredential, query *jsonutils.JSONDict) (jsonutils.JSONObject, error) {
  59. return getModelSpecs(GuestManager, ctx, userCred, query)
  60. }
  61. func GetSpecIdentKey(keys []string) string {
  62. sort.Strings(keys)
  63. return strings.Join(keys, "/")
  64. }
  65. func GetModelSpec(manager ISpecModelManager, model ISpecModel) (jsonutils.JSONObject, error) {
  66. spec := model.GetSpec(false)
  67. specKey := GetSpecIdentKey(manager.GetSpecIdent(spec))
  68. spec.Add(jsonutils.NewString(specKey), "spec_key")
  69. return spec, nil
  70. }
  71. func getModelSpecs(manager ISpecModelManager, ctx context.Context, userCred mcclient.TokenCredential, query *jsonutils.JSONDict) (jsonutils.JSONObject, error) {
  72. statusCheck, err := manager.GetSpecShouldCheckStatus(query)
  73. if err != nil {
  74. return nil, err
  75. }
  76. if bm, ok := manager.(IBatchSpecModel); ok {
  77. return bm.BatchGetModelSpecs(statusCheck)
  78. }
  79. items, err := ListItems(manager, ctx, userCred, query)
  80. if err != nil {
  81. return nil, err
  82. }
  83. retDict := jsonutils.NewDict()
  84. for _, obj := range items {
  85. specObj := obj.(ISpecModel)
  86. spec := specObj.GetSpec(statusCheck)
  87. if spec == nil {
  88. continue
  89. }
  90. specKeys := manager.GetSpecIdent(spec)
  91. specKey := GetSpecIdentKey(specKeys)
  92. if oldSpec, _ := retDict.Get(specKey); oldSpec == nil {
  93. spec.Add(jsonutils.NewInt(1), "count")
  94. retDict.Add(spec, specKey)
  95. } else {
  96. count, _ := oldSpec.Int("count")
  97. oldSpec.(*jsonutils.JSONDict).Set("count", jsonutils.NewInt(count+1))
  98. retDict.Set(specKey, oldSpec)
  99. }
  100. }
  101. return retDict, nil
  102. }
  103. func ListItems(manager db.IModelManager, ctx context.Context, userCred mcclient.TokenCredential, queryDict *jsonutils.JSONDict) ([]ISpecModel, error) {
  104. q := manager.Query()
  105. queryDict, err := manager.ValidateListConditions(ctx, userCred, queryDict)
  106. if err != nil {
  107. return nil, err
  108. }
  109. q, err = db.ListItemQueryFilters(manager, ctx, q, userCred, queryDict, policy.PolicyActionList)
  110. if err != nil {
  111. return nil, err
  112. }
  113. customizeFilters, err := manager.CustomizeFilterList(ctx, q, userCred, queryDict)
  114. if err != nil {
  115. return nil, err
  116. }
  117. rows, err := q.Rows()
  118. if err != nil {
  119. return nil, err
  120. }
  121. defer rows.Close()
  122. records := make(map[string]ISpecModel, 0)
  123. tmpLists := make([]jsonutils.JSONObject, 0)
  124. for rows.Next() {
  125. item, err := db.NewModelObject(manager)
  126. if err != nil {
  127. return nil, err
  128. }
  129. itemInitValue := reflect.Indirect(reflect.ValueOf(item))
  130. item, err = db.NewModelObject(manager)
  131. if err != nil {
  132. return nil, err
  133. }
  134. itemValue := reflect.Indirect(reflect.ValueOf(item))
  135. itemValue.Set(itemInitValue)
  136. err = q.Row2Struct(rows, item)
  137. if err != nil {
  138. return nil, err
  139. }
  140. records[item.GetId()] = item.(ISpecModel)
  141. tmpLists = append(tmpLists, jsonutils.Marshal(item).(*jsonutils.JSONDict))
  142. }
  143. if !customizeFilters.IsEmpty() {
  144. tmpLists, err = customizeFilters.DoApply(tmpLists)
  145. if err != nil {
  146. return nil, err
  147. }
  148. }
  149. items := make([]ISpecModel, 0)
  150. for _, obj := range tmpLists {
  151. id, _ := obj.GetString("id")
  152. items = append(items, records[id])
  153. }
  154. return items, nil
  155. }