keystoneutils.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 identity
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/onecloud/pkg/mcclient"
  18. modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  19. )
  20. func getUserId(s *mcclient.ClientSession, user string, domain string) (string, error) {
  21. query := jsonutils.NewDict()
  22. if len(domain) > 0 {
  23. domainId, err := modules.Domains.GetId(s, domain, nil)
  24. if err != nil {
  25. return "", err
  26. }
  27. query.Add(jsonutils.NewString(domainId), "domain_id")
  28. }
  29. return modules.UsersV3.GetId(s, user, query)
  30. }
  31. func getGroupId(s *mcclient.ClientSession, group string, domain string) (string, error) {
  32. query := jsonutils.NewDict()
  33. if len(domain) > 0 {
  34. domainId, err := modules.Domains.GetId(s, domain, nil)
  35. if err != nil {
  36. return "", err
  37. }
  38. query.Add(jsonutils.NewString(domainId), "domain_id")
  39. }
  40. return modules.Groups.GetId(s, group, query)
  41. }
  42. func getRoleId(s *mcclient.ClientSession, role string, domain string) (string, error) {
  43. query := jsonutils.NewDict()
  44. if len(domain) > 0 {
  45. domainId, err := modules.Domains.GetId(s, domain, nil)
  46. if err != nil {
  47. return "", err
  48. }
  49. query.Add(jsonutils.NewString(domainId), "domain_id")
  50. }
  51. return modules.RolesV3.GetId(s, role, query)
  52. }
  53. func getProjectId(s *mcclient.ClientSession, project string, domain string) (string, error) {
  54. query := jsonutils.NewDict()
  55. if len(domain) > 0 {
  56. domainId, err := modules.Domains.GetId(s, domain, nil)
  57. if err != nil {
  58. return "", err
  59. }
  60. query.Add(jsonutils.NewString(domainId), "domain_id")
  61. }
  62. return modules.Projects.GetId(s, project, query)
  63. }
  64. func getUserGroupId(s *mcclient.ClientSession, user, group, domain string) (string, string, error) {
  65. query := jsonutils.NewDict()
  66. if len(domain) > 0 {
  67. domainId, err := modules.Domains.GetId(s, domain, nil)
  68. if err != nil {
  69. return "", "", err
  70. }
  71. query.Add(jsonutils.NewString(domainId), "domain_id")
  72. }
  73. uid, err := modules.UsersV3.GetId(s, user, query)
  74. if err != nil {
  75. return "", "", err
  76. }
  77. gid, err := modules.Groups.GetId(s, group, query)
  78. if err != nil {
  79. return "", "", err
  80. }
  81. return uid, gid, nil
  82. }