role_assignments.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 init() {
  21. type RoleAssignmentsOptions struct {
  22. Effective bool `help:"Include role assignment of group members"`
  23. System bool `help:"Include system user account"`
  24. Policy bool `help:"Show matched policies"`
  25. Domain string `help:"Role assignments for domain"`
  26. User string `help:"For user"`
  27. UserDomain string `help:"Domain for user"`
  28. Group string `help:"For group"`
  29. GroupDomain string `help:"Domain for group"`
  30. Project string `help:"Role assignments for project"`
  31. ProjectDomain string `help:"Domain for project"`
  32. Role string `help:"Role assignments for role"`
  33. RoleDomain string `help:"Domain for role"`
  34. Limit int64 `help:"maximal returned number of rows"`
  35. Offset int64 `help:"offset index of returned results"`
  36. Users []string `help:"fitler by user id or name"`
  37. Groups []string `help:"fitler by group id or name"`
  38. Roles []string `help:"fitler by role id or name"`
  39. Projects []string `help:"fitler by project id or name"`
  40. Domains []string `help:"fitler by domain id or name"`
  41. ProjectDomainId string
  42. ProjectDomains []string `help:"filter by project's domain id or name"`
  43. }
  44. R(&RoleAssignmentsOptions{}, "role-assignments", "List all role assignments", func(s *mcclient.ClientSession, args *RoleAssignmentsOptions) error {
  45. query := jsonutils.NewDict()
  46. query.Add(jsonutils.JSONNull, "include_names")
  47. if args.Effective {
  48. query.Add(jsonutils.JSONNull, "effective")
  49. }
  50. if args.System {
  51. query.Add(jsonutils.JSONNull, "include_system")
  52. }
  53. if args.Policy {
  54. query.Add(jsonutils.JSONNull, "include_policies")
  55. }
  56. if len(args.Domain) > 0 {
  57. domainId, err := modules.Domains.GetId(s, args.Domain, nil)
  58. if err != nil {
  59. return err
  60. }
  61. query.Add(jsonutils.NewString(domainId), "scope", "domain", "id")
  62. }
  63. if len(args.Project) > 0 {
  64. pid, err := getProjectId(s, args.Project, args.ProjectDomain)
  65. if err != nil {
  66. return err
  67. }
  68. query.Add(jsonutils.NewString(pid), "scope", "project", "id")
  69. }
  70. if len(args.ProjectDomainId) > 0 {
  71. query.Add(jsonutils.NewString(args.ProjectDomainId), "project_domain_id")
  72. }
  73. if len(args.User) > 0 {
  74. uid, err := getUserId(s, args.User, args.UserDomain)
  75. if err != nil {
  76. return err
  77. }
  78. query.Add(jsonutils.NewString(uid), "user", "id")
  79. }
  80. if len(args.Group) > 0 {
  81. gid, err := getGroupId(s, args.Group, args.GroupDomain)
  82. if err != nil {
  83. return err
  84. }
  85. query.Add(jsonutils.NewString(gid), "group", "id")
  86. }
  87. if len(args.Role) > 0 {
  88. rid, err := getRoleId(s, args.Role, args.RoleDomain)
  89. if err != nil {
  90. return err
  91. }
  92. query.Add(jsonutils.NewString(rid), "role", "id")
  93. }
  94. if len(args.Users) > 0 {
  95. query.Add(jsonutils.NewStringArray(args.Users), "users")
  96. }
  97. if len(args.Groups) > 0 {
  98. query.Add(jsonutils.NewStringArray(args.Groups), "groups")
  99. }
  100. if len(args.Roles) > 0 {
  101. query.Add(jsonutils.NewStringArray(args.Roles), "roles")
  102. }
  103. if len(args.Projects) > 0 {
  104. query.Add(jsonutils.NewStringArray(args.Projects), "projects")
  105. }
  106. if len(args.Domains) > 0 {
  107. query.Add(jsonutils.NewStringArray(args.Domains), "domains")
  108. }
  109. if len(args.ProjectDomains) > 0 {
  110. query.Add(jsonutils.NewStringArray(args.ProjectDomains), "project_domains")
  111. }
  112. if args.Limit > 0 {
  113. query.Add(jsonutils.NewInt(args.Limit), "limit")
  114. }
  115. if args.Offset > 0 {
  116. query.Add(jsonutils.NewInt(args.Offset), "offset")
  117. }
  118. result, err := modules.RoleAssignments.List(s, query)
  119. if err != nil {
  120. return err
  121. }
  122. printList(result, modules.RoleAssignments.GetColumns(s))
  123. return nil
  124. })
  125. }