cloudproviderresource.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/util/reflectutils"
  20. "yunion.io/x/sqlchemy"
  21. api "yunion.io/x/onecloud/pkg/apis/cloudid"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/validators"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/util/stringutils2"
  26. )
  27. // +onecloud:swagger-gen-ignore
  28. type SCloudproviderResourceBaseManager struct {
  29. }
  30. type SCloudproviderResourceBase struct {
  31. // 子订阅Id
  32. ManagerId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"optional" json:"manager_id"`
  33. }
  34. func (manager *SCloudproviderResourceBaseManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query api.CloudproviderResourceListInput) (*sqlchemy.SQuery, error) {
  35. if len(query.ManagerId) > 0 {
  36. _, err := validators.ValidateModel(ctx, userCred, CloudproviderManager, &query.ManagerId)
  37. if err != nil {
  38. return nil, err
  39. }
  40. q = q.Equals("manager_id", query.ManagerId)
  41. }
  42. return q, nil
  43. }
  44. func (manager *SCloudproviderResourceBaseManager) FetchCustomizeColumns(
  45. ctx context.Context,
  46. userCred mcclient.TokenCredential,
  47. query jsonutils.JSONObject,
  48. objs []interface{},
  49. fields stringutils2.SSortedStrings,
  50. isList bool,
  51. ) []api.CloudproviderResourceDetails {
  52. rows := make([]api.CloudproviderResourceDetails, len(objs))
  53. providerIds := make([]string, len(objs))
  54. for i := range objs {
  55. var base *SCloudproviderResourceBase
  56. err := reflectutils.FindAnonymouStructPointer(objs[i], &base)
  57. if err != nil {
  58. log.Errorf("Cannot find SCloudproviderResourceBase in %#v: %s", objs[i], err)
  59. } else if base != nil && len(base.ManagerId) > 0 {
  60. providerIds[i] = base.ManagerId
  61. }
  62. }
  63. providerMaps, err := db.FetchIdNameMap2(CloudproviderManager, providerIds)
  64. if err != nil {
  65. log.Errorf("FetchIdNameMap2 fail %v", err)
  66. return rows
  67. }
  68. for i := range rows {
  69. rows[i].Manager, _ = providerMaps[providerIds[i]]
  70. }
  71. return rows
  72. }