groups.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. api "yunion.io/x/onecloud/pkg/apis/identity"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  20. "yunion.io/x/onecloud/pkg/mcclient/options"
  21. )
  22. func init() {
  23. type GroupListOptions struct {
  24. options.BaseListOptions
  25. Name string `help:"Filter by name"`
  26. OrderByDomain string `help:"order by domain name" choices:"asc|desc"`
  27. IdpId string `help:"filter by idp_id"`
  28. }
  29. R(&GroupListOptions{}, "group-list", "List groups", func(s *mcclient.ClientSession, args *GroupListOptions) error {
  30. params, err := options.ListStructToParams(args)
  31. if err != nil {
  32. return err
  33. }
  34. result, err := modules.Groups.List(s, params)
  35. if err != nil {
  36. return err
  37. }
  38. printList(result, modules.Groups.GetColumns(s))
  39. return nil
  40. })
  41. type GroupShowOptions struct {
  42. ID string `help:"ID or Name of group"`
  43. Domain string `help:"Id or Name of domain"`
  44. }
  45. R(&GroupShowOptions{}, "group-show", "Show details of a group", func(s *mcclient.ClientSession, args *GroupShowOptions) error {
  46. params := jsonutils.NewDict()
  47. if len(args.Domain) > 0 {
  48. domainId, err := modules.Domains.GetId(s, args.Domain, nil)
  49. if err != nil {
  50. return err
  51. }
  52. params.Add(jsonutils.NewString(domainId), "domain_id")
  53. }
  54. grpId, err := modules.Groups.GetId(s, args.ID, params)
  55. if err != nil {
  56. return err
  57. }
  58. result, err := modules.Groups.GetById(s, grpId, nil)
  59. if err != nil {
  60. return err
  61. }
  62. printObject(result)
  63. return nil
  64. })
  65. R(&GroupShowOptions{}, "group-user-list", "Show members of a group", func(s *mcclient.ClientSession, args *GroupShowOptions) error {
  66. params := jsonutils.NewDict()
  67. if len(args.Domain) > 0 {
  68. domainId, err := modules.Domains.GetId(s, args.Domain, nil)
  69. if err != nil {
  70. return err
  71. }
  72. params.Add(jsonutils.NewString(domainId), "domain_id")
  73. }
  74. grpId, err := modules.Groups.GetId(s, args.ID, params)
  75. if err != nil {
  76. return err
  77. }
  78. users, err := modules.Groups.GetUsers(s, grpId, nil)
  79. if err != nil {
  80. return err
  81. }
  82. printList(users, modules.UsersV3.GetColumns(s))
  83. return nil
  84. })
  85. type GroupCreateOptions struct {
  86. NAME string `help:"Name of the group"`
  87. Desc string `help:"Description"`
  88. Domain string `help:"Domain ID or Name"`
  89. }
  90. R(&GroupCreateOptions{}, "group-create", "Create a group", func(s *mcclient.ClientSession, args *GroupCreateOptions) error {
  91. params := jsonutils.NewDict()
  92. params.Add(jsonutils.NewString(args.NAME), "name")
  93. if len(args.Desc) > 0 {
  94. params.Add(jsonutils.NewString(args.Desc), "description")
  95. }
  96. if len(args.Domain) > 0 {
  97. domainId, e := modules.Domains.GetId(s, args.Domain, nil)
  98. if e != nil {
  99. return e
  100. }
  101. params.Add(jsonutils.NewString(domainId), "domain_id")
  102. }
  103. result, e := modules.Groups.Create(s, params)
  104. if e != nil {
  105. return e
  106. }
  107. printObject(result)
  108. return nil
  109. })
  110. R(&GroupShowOptions{}, "group-delete", "Delete a group", func(s *mcclient.ClientSession, args *GroupShowOptions) error {
  111. params := jsonutils.NewDict()
  112. if len(args.Domain) > 0 {
  113. domainId, err := modules.Domains.GetId(s, args.Domain, nil)
  114. if err != nil {
  115. return err
  116. }
  117. params.Add(jsonutils.NewString(domainId), "domain_id")
  118. }
  119. grpId, err := modules.Groups.GetId(s, args.ID, params)
  120. if err != nil {
  121. return err
  122. }
  123. result, err := modules.Groups.Delete(s, grpId, nil)
  124. if err != nil {
  125. return err
  126. }
  127. printObject(result)
  128. return nil
  129. })
  130. R(&GroupShowOptions{}, "group-project-list", "List projects of group", func(s *mcclient.ClientSession, args *GroupShowOptions) error {
  131. query := jsonutils.NewDict()
  132. if len(args.Domain) > 0 {
  133. domainId, err := modules.Domains.GetId(s, args.Domain, nil)
  134. if err != nil {
  135. return err
  136. }
  137. query.Add(jsonutils.NewString(domainId), "domain_id")
  138. }
  139. uid, err := modules.Groups.GetId(s, args.ID, query)
  140. if err != nil {
  141. return err
  142. }
  143. projects, e := modules.Groups.GetProjects(s, uid)
  144. if e != nil {
  145. return e
  146. }
  147. printList(projects, modules.Projects.GetColumns(s))
  148. return nil
  149. })
  150. type GroupJoinProjectOptions struct {
  151. Group string `help:"Group Id or name" optional:"false" positional:"true"`
  152. Project []string `help:"Projects to join" nargs:"+"`
  153. Role []string `help:"User join project with roles" nargs:"+"`
  154. }
  155. R(&GroupJoinProjectOptions{}, "group-join-project", "Group join projects with roles", func(s *mcclient.ClientSession, args *GroupJoinProjectOptions) error {
  156. input := api.SJoinProjectsInput{}
  157. input.Projects = args.Project
  158. input.Roles = args.Role
  159. result, err := modules.Groups.PerformAction(s, args.Group, "join", jsonutils.Marshal(input))
  160. if err != nil {
  161. return err
  162. }
  163. printObject(result)
  164. return nil
  165. })
  166. type GroupLeaveProjectsOptions struct {
  167. Group string `help:"group id or name" optional:"false" positional:"true"`
  168. Project string `help:"project id or name" optional:"false" positional:"true"`
  169. Role []string `help:"roles to remove" nargs:"+"`
  170. }
  171. R(&GroupLeaveProjectsOptions{}, "group-leave-project", "Leave a group from projects", func(s *mcclient.ClientSession, args *GroupLeaveProjectsOptions) error {
  172. input := api.SLeaveProjectsInput{}
  173. input.ProjectRoles = make([]api.SProjectRole, len(args.Role))
  174. for i := range args.Role {
  175. input.ProjectRoles[i].Project = args.Project
  176. input.ProjectRoles[i].Role = args.Role[i]
  177. }
  178. result, err := modules.Groups.PerformAction(s, args.Group, "leave", jsonutils.Marshal(input))
  179. if err != nil {
  180. return err
  181. }
  182. printObject(result)
  183. return nil
  184. })
  185. type GroupAddUsersOptions struct {
  186. GROUP string `help:"group Id or Name" json:"-"`
  187. USER []string `help:"list of user id or name" json:"user"`
  188. }
  189. R(&GroupAddUsersOptions{}, "group-add-users", "Add users into group",
  190. func(s *mcclient.ClientSession, args *GroupAddUsersOptions) error {
  191. params := jsonutils.Marshal(args)
  192. result, err := modules.Groups.PerformAction(s, args.GROUP, "add-users", params)
  193. if err != nil {
  194. return err
  195. }
  196. printObject(result)
  197. return nil
  198. })
  199. R(&GroupAddUsersOptions{}, "group-remove-users", "Add users into group",
  200. func(s *mcclient.ClientSession, args *GroupAddUsersOptions) error {
  201. params := jsonutils.Marshal(args)
  202. result, err := modules.Groups.PerformAction(s, args.GROUP, "remove-users", params)
  203. if err != nil {
  204. return err
  205. }
  206. printObject(result)
  207. return nil
  208. })
  209. }