override.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 service
  15. import (
  16. "context"
  17. "database/sql"
  18. "github.com/golang-plus/uuid"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/sqlchemy"
  22. api "yunion.io/x/onecloud/pkg/apis/identity"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/keystone/models"
  26. )
  27. func keystoneUUIDGenerator() string {
  28. id, _ := uuid.NewV4()
  29. return id.Format(uuid.StyleWithoutDash)
  30. }
  31. func keystoneProjectFetcher(ctx context.Context, idstr string, domainId string) (*db.STenant, error) {
  32. tenantObj, err := models.ProjectManager.FetchProject(idstr, idstr, domainId, domainId)
  33. if err != nil {
  34. if errors.Cause(err) == sql.ErrNoRows {
  35. return nil, errors.Wrapf(httperrors.ErrResourceNotFound, "tenant %s", idstr)
  36. } else {
  37. return nil, errors.Wrap(err, "models.ProjectManager.FetchByIdOrName")
  38. }
  39. }
  40. ret := project2Tenant(tenantObj)
  41. return &ret, nil
  42. }
  43. func keystoneDomainFetcher(ctx context.Context, idstr string) (*db.STenant, error) {
  44. domainObj, err := models.DomainManager.FetchByIdOrName(ctx, nil, idstr)
  45. if err != nil {
  46. if errors.Cause(err) == sql.ErrNoRows {
  47. return nil, errors.Wrapf(httperrors.ErrResourceNotFound, "domain %s", idstr)
  48. } else {
  49. return nil, errors.Wrap(err, "models.DomainManager.FetchByIdOrName")
  50. }
  51. }
  52. ret := domain2Tenant(domainObj.(*models.SDomain))
  53. return &ret, nil
  54. }
  55. func project2Tenant(tenant *models.SProject) db.STenant {
  56. ret := db.STenant{}
  57. ret.Id = tenant.Id
  58. ret.Name = tenant.Name
  59. ret.DomainId = tenant.DomainId
  60. ret.Domain = tenant.GetDomain().Name
  61. return ret
  62. }
  63. func domain2Tenant(domain *models.SDomain) db.STenant {
  64. ret := db.STenant{}
  65. ret.Id = domain.Id
  66. ret.Name = domain.Name
  67. ret.DomainId = api.KeystoneDomainRoot
  68. ret.Domain = api.KeystoneDomainRoot
  69. return ret
  70. }
  71. func keystoneProjectsFetcher(ctx context.Context, idList []string, isDomain bool) map[string]db.STenant {
  72. if isDomain {
  73. domains := make(map[string]models.SDomain)
  74. err := db.FetchStandaloneObjectsByIds(models.DomainManager, idList, &domains)
  75. if err != nil {
  76. log.Errorf("FetchStandaloneObjectsByIds for domain fail %s", err)
  77. return nil
  78. }
  79. ret := make(map[string]db.STenant)
  80. for id, domain := range domains {
  81. ret[id] = domain2Tenant(&domain)
  82. }
  83. return ret
  84. } else {
  85. projects := make(map[string]models.SProject)
  86. err := db.FetchStandaloneObjectsByIds(models.ProjectManager, idList, &projects)
  87. if err != nil {
  88. log.Errorf("FetchStandaloneObjectsByIds for project fail %s", err)
  89. return nil
  90. }
  91. ret := make(map[string]db.STenant)
  92. for id, project := range projects {
  93. ret[id] = project2Tenant(&project)
  94. }
  95. return ret
  96. }
  97. }
  98. func keystoneProjectQuery(fields ...string) *sqlchemy.SQuery {
  99. return models.ProjectManager.Query(fields...)
  100. }
  101. func keystoneDomainQuery(fields ...string) *sqlchemy.SQuery {
  102. return models.DomainManager.Query(fields...)
  103. }
  104. func keystoneUserFetcher(ctx context.Context, idstr string) (*db.SUser, error) {
  105. userObj, err := models.UserManager.FetchByIdOrName(ctx, nil, idstr)
  106. if err != nil {
  107. if errors.Cause(err) == sql.ErrNoRows {
  108. return nil, errors.Wrapf(httperrors.ErrResourceNotFound, "user %s", idstr)
  109. } else {
  110. return nil, errors.Wrap(err, "models.UserManager.FetchByIdOrName")
  111. }
  112. }
  113. ret := user2User(userObj.(*models.SUser))
  114. return &ret, nil
  115. }
  116. func user2User(u *models.SUser) db.SUser {
  117. ret := db.SUser{}
  118. ret.Id = u.Id
  119. ret.Name = u.Name
  120. ret.DomainId = u.DomainId
  121. ret.Domain = u.GetDomain().Name
  122. return ret
  123. }