cloudpolicyresource.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "database/sql"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/util/reflectutils"
  21. "yunion.io/x/sqlchemy"
  22. api "yunion.io/x/onecloud/pkg/apis/cloudid"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. "yunion.io/x/onecloud/pkg/util/stringutils2"
  27. )
  28. // +onecloud:swagger-gen-ignore
  29. type SCloudpolicyResourceBaseManager struct {
  30. }
  31. type SCloudpolicyResourceBase struct {
  32. // 权限Id
  33. CloudpolicyId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" json:"cloudpolicy_id"`
  34. }
  35. func (manager *SCloudpolicyResourceBaseManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, policyCred mcclient.TokenCredential, query api.CloudpolicyResourceListInput) (*sqlchemy.SQuery, error) {
  36. if len(query.Cloudpolicy) > 0 {
  37. policy, err := CloudpolicyManager.FetchByIdOrName(ctx, nil, query.Cloudpolicy)
  38. if err != nil {
  39. if err == sql.ErrNoRows {
  40. return nil, httperrors.NewResourceNotFoundError2("cloudpolicy", query.Cloudpolicy)
  41. }
  42. return nil, httperrors.NewGeneralError(err)
  43. }
  44. q = q.Equals("cloudpolicy_id", policy.GetId())
  45. }
  46. return q, nil
  47. }
  48. func (manager *SCloudpolicyResourceBaseManager) FetchCustomizeColumns(
  49. ctx context.Context,
  50. policyCred mcclient.TokenCredential,
  51. query jsonutils.JSONObject,
  52. objs []interface{},
  53. fields stringutils2.SSortedStrings,
  54. isList bool,
  55. ) []api.CloudpolicyResourceDetails {
  56. rows := make([]api.CloudpolicyResourceDetails, len(objs))
  57. policyIds := make([]string, len(objs))
  58. for i := range objs {
  59. var base *SCloudpolicyResourceBase
  60. err := reflectutils.FindAnonymouStructPointer(objs[i], &base)
  61. if err != nil {
  62. log.Errorf("Cannot find SCloudpolicyResourceBase in %#v: %s", objs[i], err)
  63. } else if base != nil && len(base.CloudpolicyId) > 0 {
  64. policyIds[i] = base.CloudpolicyId
  65. }
  66. }
  67. policyMaps, err := db.FetchIdNameMap2(CloudpolicyManager, policyIds)
  68. if err != nil {
  69. log.Errorf("FetchIdNameMap2 fail %v", err)
  70. return rows
  71. }
  72. for i := range rows {
  73. rows[i].Cloudpolicy, _ = policyMaps[policyIds[i]]
  74. }
  75. return rows
  76. }