projectresource.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/pkg/errors"
  19. "yunion.io/x/sqlchemy"
  20. api "yunion.io/x/onecloud/pkg/apis/identity"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/httperrors"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. // +onecloud:swagger-gen-ignore
  26. type SProjectResourceBaseManager struct{}
  27. func (manager *SProjectResourceBaseManager) ListItemFilter(
  28. ctx context.Context,
  29. q *sqlchemy.SQuery,
  30. userCred mcclient.TokenCredential,
  31. query api.ProjectFilterListInput,
  32. ) (*sqlchemy.SQuery, error) {
  33. if len(query.ProjectId) > 0 {
  34. var ownerId mcclient.IIdentityProvider
  35. if len(query.ProjectDomainId) > 0 {
  36. domain, err := DomainManager.FetchDomainByIdOrName(query.ProjectDomainId)
  37. if err != nil {
  38. if errors.Cause(err) == sql.ErrNoRows {
  39. return nil, httperrors.NewResourceNotFoundError2(DomainManager.Keyword(), query.ProjectDomainId)
  40. } else {
  41. return nil, errors.Wrap(err, "DomainManager.FetchDomainByIdOrName")
  42. }
  43. }
  44. ownerId = &db.SOwnerId{
  45. Domain: domain.Name,
  46. DomainId: domain.Id,
  47. }
  48. } else {
  49. ownerId = userCred
  50. }
  51. projObj, err := ProjectManager.FetchByIdOrName(ctx, ownerId, query.ProjectId)
  52. if err != nil {
  53. if errors.Cause(err) == sql.ErrNoRows {
  54. return nil, httperrors.NewResourceNotFoundError2(ProjectManager.Keyword(), query.ProjectId)
  55. } else {
  56. return nil, errors.Wrap(err, "ProjectManager.FetchByIdOrName")
  57. }
  58. }
  59. q = q.Equals("project_id", projObj.GetId())
  60. }
  61. return q, nil
  62. }