monitorscoperesource.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/pkg/util/rbacscope"
  19. "yunion.io/x/sqlchemy"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. )
  23. type SMonitorScopedResourceManager struct {
  24. db.SScopedResourceBaseManager
  25. }
  26. type SMonitorScopedResource struct {
  27. db.SScopedResourceBase
  28. }
  29. func (m *SMonitorScopedResourceManager) FilterByOwner(ctx context.Context, q *sqlchemy.SQuery, man db.FilterByOwnerProvider, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, scope rbacscope.TRbacScope) *sqlchemy.SQuery {
  30. if ownerId == nil {
  31. return q
  32. }
  33. switch scope {
  34. case rbacscope.ScopeDomain:
  35. q = q.Filter(
  36. sqlchemy.OR(
  37. sqlchemy.Equals(q.Field("domain_id"), ownerId.GetProjectDomainId()),
  38. sqlchemy.IsNullOrEmpty(q.Field("domain_id")),
  39. ),
  40. )
  41. case rbacscope.ScopeProject:
  42. q = q.Filter(
  43. sqlchemy.OR(
  44. sqlchemy.Equals(q.Field("tenant_id"), ownerId.GetProjectId()),
  45. sqlchemy.IsNullOrEmpty(q.Field("domain_id")),
  46. sqlchemy.Equals(q.Field("domain_id"), ownerId.GetProjectDomainId()),
  47. ),
  48. )
  49. }
  50. return q
  51. }
  52. func (s *SMonitorScopedResource) CustomizeCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
  53. scope, _ := data.GetString("scope")
  54. switch rbacscope.TRbacScope(scope) {
  55. case rbacscope.ScopeSystem:
  56. s.DomainId = ""
  57. s.ProjectId = ""
  58. case rbacscope.ScopeDomain:
  59. s.DomainId = ownerId.GetProjectDomainId()
  60. s.ProjectId = ""
  61. case rbacscope.ScopeProject:
  62. s.DomainId = ownerId.GetProjectDomainId()
  63. s.ProjectId = ownerId.GetProjectId()
  64. }
  65. return nil
  66. }
  67. func (manager *SMonitorScopedResourceManager) ResourceScope() rbacscope.TRbacScope {
  68. return manager.SScopedResourceBaseManager.ResourceScope()
  69. }
  70. func (manager *SMonitorScopedResourceManager) FetchOwnerId(ctx context.Context, data jsonutils.JSONObject) (mcclient.IIdentityProvider, error) {
  71. return manager.SScopedResourceBaseManager.FetchOwnerId(ctx, data)
  72. }
  73. func (model *SMonitorScopedResource) GetOwnerId() mcclient.IIdentityProvider {
  74. return model.SScopedResourceBase.GetOwnerId()
  75. }