db.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 apihelper
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/mcclient/auth"
  22. "yunion.io/x/onecloud/pkg/mcclient/options"
  23. )
  24. type GetDBModelsOptions struct {
  25. modelOptions *GetModelsOptions
  26. modelDBManager db.IModelManager
  27. }
  28. func (o GetDBModelsOptions) IncludeOtherCloudEnv() bool {
  29. return o.modelOptions.InCludeOtherCloudEnv
  30. }
  31. func (o GetDBModelsOptions) GetModelSet() IModelSet {
  32. return o.modelOptions.ModelSet
  33. }
  34. func (o GetDBModelsOptions) IncludeDetails() bool {
  35. return o.modelOptions.IncludeDetails
  36. }
  37. func (o GetDBModelsOptions) IncludeEmulated() bool {
  38. return o.modelOptions.IncludeEmulated
  39. }
  40. func (o GetDBModelsOptions) BatchListSize() int {
  41. return o.modelOptions.BatchListSize
  42. }
  43. func (o GetDBModelsOptions) InCludeOtherCloudEnv() bool {
  44. return o.modelOptions.InCludeOtherCloudEnv
  45. }
  46. func GetDBModels(opts *GetDBModelsOptions) error {
  47. man := opts.modelDBManager
  48. manKeyPlural := man.KeywordPlural()
  49. limit := opts.BatchListSize()
  50. // limit := 5
  51. listOptions := options.BaseListOptions{
  52. System: options.Bool(true),
  53. Admin: options.Bool(true),
  54. Scope: "system",
  55. Details: options.Bool(opts.IncludeDetails()),
  56. ShowEmulated: options.Bool(opts.IncludeEmulated()),
  57. OrderBy: []string{"updated_at"},
  58. Order: "asc",
  59. Limit: &limit,
  60. }
  61. if !opts.InCludeOtherCloudEnv() {
  62. listOptions.Filter = append(listOptions.Filter,
  63. "manager_id.isnullorempty()", // len(manager_id) > 0 is for pubcloud objects
  64. // "external_id.isnullorempty()", // len(external_id) > 0 is for pubcloud objects
  65. )
  66. listOptions.CloudEnv = "onpremise"
  67. // listOptions.Provider = []string{"OneCloud"}
  68. }
  69. if inter, ok := opts.GetModelSet().(IModelSetFilter); ok {
  70. filter := inter.ModelFilter()
  71. listOptions.Filter = append(listOptions.Filter, filter...)
  72. }
  73. params, err := listOptions.Params()
  74. if err != nil {
  75. return fmt.Errorf("%s: making list params: %s", manKeyPlural, err)
  76. }
  77. if inter, ok := opts.GetModelSet().(IModelListParam); ok {
  78. filter := inter.ModelParamFilter()
  79. params.Update(filter)
  80. }
  81. //XXX
  82. //params.Set(api.LBAGENT_QUERY_ORIG_KEY, jsonutils.NewString(api.LBAGENT_QUERY_ORIG_VAL))
  83. entriesJson := []jsonutils.JSONObject{}
  84. for {
  85. log.Debugf("list %s with params: %s", manKeyPlural, params.String())
  86. var err error
  87. listResult, err := db.ListItems(man, context.Background(), auth.AdminCredential(), params, nil)
  88. if err != nil {
  89. return fmt.Errorf("%s: list failed: %s",
  90. manKeyPlural, err)
  91. }
  92. entriesJson = append(entriesJson, listResult.Data...)
  93. if listResult.Offset+len(listResult.Data) >= listResult.Total {
  94. break
  95. } else {
  96. offset := listResult.Offset + len(listResult.Data)
  97. params.Set("offset", jsonutils.NewInt(int64(offset)))
  98. }
  99. }
  100. {
  101. err := InitializeModelSetFromJSON(opts.GetModelSet(), entriesJson)
  102. if err != nil {
  103. return fmt.Errorf("%s: initializing model set failed: %s",
  104. manKeyPlural, err)
  105. }
  106. }
  107. return nil
  108. }