cloudgroupresource.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/errors"
  21. "yunion.io/x/pkg/util/reflectutils"
  22. "yunion.io/x/sqlchemy"
  23. api "yunion.io/x/onecloud/pkg/apis/cloudid"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  25. "yunion.io/x/onecloud/pkg/httperrors"
  26. "yunion.io/x/onecloud/pkg/mcclient"
  27. "yunion.io/x/onecloud/pkg/util/stringutils2"
  28. )
  29. // +onecloud:swagger-gen-ignore
  30. type SCloudgroupResourceBaseManager struct {
  31. }
  32. type SCloudgroupResourceBase struct {
  33. CloudgroupId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required"`
  34. }
  35. func (self *SCloudgroupResourceBase) GetCloudgroup() (*SCloudgroup, error) {
  36. group, err := CloudgroupManager.FetchById(self.CloudgroupId)
  37. if err != nil {
  38. return nil, errors.Wrap(err, "FetchById")
  39. }
  40. return group.(*SCloudgroup), nil
  41. }
  42. func (manager *SCloudgroupResourceBaseManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, groupCred mcclient.TokenCredential, query api.CloudgroupResourceListInput) (*sqlchemy.SQuery, error) {
  43. if len(query.CloudgroupId) > 0 {
  44. group, err := CloudgroupManager.FetchByIdOrName(ctx, nil, query.CloudgroupId)
  45. if err != nil {
  46. if err == sql.ErrNoRows {
  47. return nil, httperrors.NewResourceNotFoundError2("cloudgroup", query.CloudgroupId)
  48. }
  49. return nil, httperrors.NewGeneralError(err)
  50. }
  51. q = q.Equals("cloudgroup_id", group.GetId())
  52. }
  53. return q, nil
  54. }
  55. func (manager *SCloudgroupResourceBaseManager) FetchCustomizeColumns(
  56. ctx context.Context,
  57. groupCred mcclient.TokenCredential,
  58. query jsonutils.JSONObject,
  59. objs []interface{},
  60. fields stringutils2.SSortedStrings,
  61. isList bool,
  62. ) []api.CloudgroupResourceDetails {
  63. rows := make([]api.CloudgroupResourceDetails, len(objs))
  64. groupIds := make([]string, len(objs))
  65. for i := range objs {
  66. var base *SCloudgroupResourceBase
  67. err := reflectutils.FindAnonymouStructPointer(objs[i], &base)
  68. if err != nil {
  69. log.Errorf("Cannot find SCloudgroupResourceBase in %#v: %s", objs[i], err)
  70. } else if base != nil && len(base.CloudgroupId) > 0 {
  71. groupIds[i] = base.CloudgroupId
  72. }
  73. }
  74. groupMaps, err := db.FetchIdNameMap2(CloudgroupManager, groupIds)
  75. if err != nil {
  76. return rows
  77. }
  78. for i := range rows {
  79. rows[i].Cloudgroup, _ = groupMaps[groupIds[i]]
  80. }
  81. return rows
  82. }