projects.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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/cmd/climc/shell"
  18. api "yunion.io/x/onecloud/pkg/apis/identity"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  21. modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  22. "yunion.io/x/onecloud/pkg/mcclient/options"
  23. identity_options "yunion.io/x/onecloud/pkg/mcclient/options/identity"
  24. )
  25. func init() {
  26. cmd := shell.NewResourceCmd(&modules.Projects)
  27. cmd.List(&identity_options.ProjectListOptions{})
  28. cmd.Perform("user-metadata", &options.ResourceMetadataOptions{})
  29. cmd.Perform("set-user-metadata", &options.ResourceMetadataOptions{})
  30. cmd.Perform("class-metadata", &options.ResourceMetadataOptions{})
  31. cmd.Perform("set-class-metadata", &options.ResourceMetadataOptions{})
  32. cmd.Perform("org-metadata", &options.ResourceMetadataOptions{})
  33. cmd.Perform("set-org-metadata", &options.ResourceMetadataOptions{})
  34. cmd.Perform("set-admin", &identity_options.ProjectSetAdminOptions{})
  35. cmd.GetProperty(&identity_options.ProjectGetPropertyTagValuePairOptions{})
  36. cmd.GetProperty(&identity_options.ProjectGetPropertyTagValueTreeOptions{})
  37. cmd.GetProperty(&identity_options.ProjectGetPropertyDomainTagValuePairOptions{})
  38. cmd.GetProperty(&identity_options.ProjectGetPropertyDomainTagValueTreeOptions{})
  39. cmd.PerformClass("clean", &identity_options.ProjectCleanOptions{})
  40. type ProjectShowOptions struct {
  41. ID string `help:"ID or Name of project"`
  42. Domain string `help:"Domain"`
  43. }
  44. R(&ProjectShowOptions{}, "project-show", "Show details of project", func(s *mcclient.ClientSession, args *ProjectShowOptions) error {
  45. query := jsonutils.NewDict()
  46. if len(args.Domain) > 0 {
  47. domainId, err := modules.Domains.GetId(s, args.Domain, nil)
  48. if err != nil {
  49. return err
  50. }
  51. query.Add(jsonutils.NewString(domainId), "domain_id")
  52. }
  53. result, err := modules.Projects.Get(s, args.ID, query)
  54. if err != nil {
  55. return err
  56. }
  57. printObject(result)
  58. return nil
  59. })
  60. R(&ProjectShowOptions{}, "project-delete", "Delete a project", func(s *mcclient.ClientSession, args *ProjectShowOptions) error {
  61. query := jsonutils.NewDict()
  62. if len(args.Domain) > 0 {
  63. domainId, err := modules.Domains.GetId(s, args.Domain, nil)
  64. if err != nil {
  65. return err
  66. }
  67. query.Add(jsonutils.NewString(domainId), "domain_id")
  68. }
  69. projectId, err := modules.Projects.GetId(s, args.ID, query)
  70. if err != nil {
  71. return err
  72. }
  73. _, err = modules.Projects.Delete(s, projectId, nil)
  74. if err != nil {
  75. return err
  76. }
  77. return nil
  78. })
  79. type ProjectCreateOptions struct {
  80. NAME string `help:"Name of new project"`
  81. Displayname string `help:"display name"`
  82. Domain string `help:"Domain"`
  83. Desc string `help:"Description"`
  84. Enabled bool `help:"Project is enabled"`
  85. Disabled bool `help:"Project is disabled"`
  86. }
  87. R(&ProjectCreateOptions{}, "project-create", "Create a project", func(s *mcclient.ClientSession, args *ProjectCreateOptions) error {
  88. params := jsonutils.NewDict()
  89. params.Add(jsonutils.NewString(args.NAME), "name")
  90. if len(args.Domain) > 0 {
  91. domainId, err := modules.Domains.GetId(s, args.Domain, nil)
  92. if err != nil {
  93. return err
  94. }
  95. params.Add(jsonutils.NewString(domainId), "domain_id")
  96. }
  97. if args.Enabled && !args.Disabled {
  98. params.Add(jsonutils.JSONTrue, "enabled")
  99. } else if !args.Enabled && args.Disabled {
  100. params.Add(jsonutils.JSONTrue, "disabled")
  101. }
  102. if len(args.Desc) > 0 {
  103. params.Add(jsonutils.NewString(args.Desc), "description")
  104. }
  105. if len(args.Displayname) > 0 {
  106. params.Add(jsonutils.NewString(args.Displayname), "displayname")
  107. }
  108. result, err := modules.Projects.Create(s, params)
  109. if err != nil {
  110. return err
  111. }
  112. printObject(result)
  113. return nil
  114. })
  115. type ProjectUpdateOptions struct {
  116. ID string `help:"ID or name of the project to update"`
  117. Domain string `help:"Domain of the project if name is given"`
  118. Name string `help:"New name of the project"`
  119. Desc string `help:"Description"`
  120. Enabled bool `help:"Project is enabled"`
  121. Disabled bool `help:"Project is disabled"`
  122. }
  123. R(&ProjectUpdateOptions{}, "project-update", "Update a project", func(s *mcclient.ClientSession, args *ProjectUpdateOptions) error {
  124. query := jsonutils.NewDict()
  125. if len(args.Domain) > 0 {
  126. domainId, err := modules.Domains.GetId(s, args.Domain, nil)
  127. if err != nil {
  128. return err
  129. }
  130. query.Add(jsonutils.NewString(domainId), "domain_id")
  131. }
  132. pId, err := modules.Projects.GetId(s, args.ID, query)
  133. if err != nil {
  134. return err
  135. }
  136. params := jsonutils.NewDict()
  137. if len(args.Name) > 0 {
  138. params.Add(jsonutils.NewString(args.Name), "name")
  139. }
  140. if args.Enabled && !args.Disabled {
  141. params.Add(jsonutils.JSONTrue, "enabled")
  142. } else if !args.Enabled && args.Disabled {
  143. params.Add(jsonutils.JSONFalse, "enabled")
  144. }
  145. if len(args.Desc) > 0 {
  146. params.Add(jsonutils.NewString(args.Desc), "description")
  147. }
  148. project, err := modules.Projects.Patch(s, pId, params)
  149. if err != nil {
  150. return err
  151. }
  152. printObject(project)
  153. return nil
  154. })
  155. type ProjectUserRoleOptions struct {
  156. PROJECT string `help:"ID or Name of Project"`
  157. USER string `help:"ID or Name of User"`
  158. ROLE string `help:"ID or Name of Role"`
  159. UserDomain string `help:"Domain of user"`
  160. ProjectDomain string `help:"Domain of project"`
  161. RoleDomain string `help:"Domain of role"`
  162. }
  163. R(&ProjectUserRoleOptions{}, "project-add-user", "Add user to project with role", func(s *mcclient.ClientSession, args *ProjectUserRoleOptions) error {
  164. uid, err := getUserId(s, args.USER, args.UserDomain)
  165. if err != nil {
  166. return err
  167. }
  168. pid, err := getProjectId(s, args.PROJECT, args.ProjectDomain)
  169. if err != nil {
  170. return err
  171. }
  172. rid, err := getRoleId(s, args.ROLE, args.RoleDomain)
  173. if err != nil {
  174. return err
  175. }
  176. _, err = modules.RolesV3.PutInContexts(s, rid, nil, []modulebase.ManagerContext{{InstanceManager: &modules.Projects, InstanceId: pid}, {InstanceManager: &modules.UsersV3, InstanceId: uid}})
  177. if err != nil {
  178. return err
  179. }
  180. return nil
  181. })
  182. R(&ProjectUserRoleOptions{}, "project-has-user", "Check a user in a project with a role", func(s *mcclient.ClientSession, args *ProjectUserRoleOptions) error {
  183. uid, err := getUserId(s, args.USER, args.UserDomain)
  184. if err != nil {
  185. return err
  186. }
  187. pid, err := getProjectId(s, args.PROJECT, args.ProjectDomain)
  188. if err != nil {
  189. return err
  190. }
  191. rid, err := getRoleId(s, args.ROLE, args.RoleDomain)
  192. if err != nil {
  193. return err
  194. }
  195. _, err = modules.RolesV3.HeadInContexts(s, rid, nil, []modulebase.ManagerContext{{InstanceManager: &modules.Projects, InstanceId: pid}, {InstanceManager: &modules.UsersV3, InstanceId: uid}})
  196. if err != nil {
  197. return err
  198. }
  199. return nil
  200. })
  201. R(&ProjectUserRoleOptions{}, "project-remove-user", "Remove a user role from a project", func(s *mcclient.ClientSession, args *ProjectUserRoleOptions) error {
  202. uid, err := getUserId(s, args.USER, args.UserDomain)
  203. if err != nil {
  204. return err
  205. }
  206. pid, err := getProjectId(s, args.PROJECT, args.ProjectDomain)
  207. if err != nil {
  208. return err
  209. }
  210. rid, err := getRoleId(s, args.ROLE, args.RoleDomain)
  211. if err != nil {
  212. return err
  213. }
  214. _, err = modules.RolesV3.DeleteInContexts(s, rid, nil, []modulebase.ManagerContext{{InstanceManager: &modules.Projects, InstanceId: pid}, {InstanceManager: &modules.UsersV3, InstanceId: uid}})
  215. if err != nil {
  216. return err
  217. }
  218. return nil
  219. })
  220. type ProjectUserRolesListOptions struct {
  221. PROJECT string `help:"ID or Name of Project"`
  222. USER string `help:"ID or Name of User"`
  223. UserDomain string `help:"Domain of user"`
  224. ProjectDomain string `help:"Domain of project"`
  225. }
  226. R(&ProjectUserRolesListOptions{}, "project-user-roles", "Get roles of a user in a project", func(s *mcclient.ClientSession, args *ProjectUserRolesListOptions) error {
  227. uid, err := getUserId(s, args.USER, args.UserDomain)
  228. if err != nil {
  229. return err
  230. }
  231. pid, err := getProjectId(s, args.PROJECT, args.ProjectDomain)
  232. if err != nil {
  233. return err
  234. }
  235. result, err := modules.RolesV3.ListInContexts(s, nil, []modulebase.ManagerContext{{InstanceManager: &modules.Projects, InstanceId: pid}, {InstanceManager: &modules.UsersV3, InstanceId: uid}})
  236. if err != nil {
  237. return err
  238. }
  239. printList(result, modules.RolesV3.GetColumns(s))
  240. return nil
  241. })
  242. type ProjectGroupRoleOptions struct {
  243. PROJECT string `help:"ID or Name of Project"`
  244. GROUP string `help:"ID or Name of Group"`
  245. ROLE string `help:"ID or Name of Role"`
  246. GroupDomain string `help:"Domain of group"`
  247. ProjectDomain string `help:"Domain of project"`
  248. RoleDomain string `help:"Domain of role"`
  249. }
  250. R(&ProjectGroupRoleOptions{}, "project-add-group", "Add group to project with role", func(s *mcclient.ClientSession, args *ProjectGroupRoleOptions) error {
  251. gid, err := getGroupId(s, args.GROUP, args.GroupDomain)
  252. if err != nil {
  253. return err
  254. }
  255. pid, err := getProjectId(s, args.PROJECT, args.ProjectDomain)
  256. if err != nil {
  257. return err
  258. }
  259. rid, err := getRoleId(s, args.ROLE, args.RoleDomain)
  260. if err != nil {
  261. return err
  262. }
  263. _, err = modules.RolesV3.PutInContexts(s, rid, nil, []modulebase.ManagerContext{{InstanceManager: &modules.Projects, InstanceId: pid}, {InstanceManager: &modules.Groups, InstanceId: gid}})
  264. if err != nil {
  265. return err
  266. }
  267. return nil
  268. })
  269. R(&ProjectGroupRoleOptions{}, "project-has-group", "Check a group in a project with a role", func(s *mcclient.ClientSession, args *ProjectGroupRoleOptions) error {
  270. gid, err := getGroupId(s, args.GROUP, args.GroupDomain)
  271. if err != nil {
  272. return err
  273. }
  274. pid, err := getProjectId(s, args.PROJECT, args.ProjectDomain)
  275. if err != nil {
  276. return err
  277. }
  278. rid, err := getRoleId(s, args.ROLE, args.RoleDomain)
  279. if err != nil {
  280. return err
  281. }
  282. _, err = modules.RolesV3.HeadInContexts(s, rid, nil, []modulebase.ManagerContext{{InstanceManager: &modules.Projects, InstanceId: pid}, {InstanceManager: &modules.Groups, InstanceId: gid}})
  283. if err != nil {
  284. return err
  285. }
  286. return nil
  287. })
  288. R(&ProjectGroupRoleOptions{}, "project-remove-group", "Remove a role for a group in a project", func(s *mcclient.ClientSession, args *ProjectGroupRoleOptions) error {
  289. gid, err := getGroupId(s, args.GROUP, args.GroupDomain)
  290. if err != nil {
  291. return err
  292. }
  293. pid, err := getProjectId(s, args.PROJECT, args.ProjectDomain)
  294. if err != nil {
  295. return err
  296. }
  297. rid, err := getRoleId(s, args.ROLE, args.RoleDomain)
  298. if err != nil {
  299. return err
  300. }
  301. _, err = modules.RolesV3.DeleteInContexts(s, rid, nil, []modulebase.ManagerContext{{InstanceManager: &modules.Projects, InstanceId: pid}, {InstanceManager: &modules.Groups, InstanceId: gid}})
  302. if err != nil {
  303. return err
  304. }
  305. return nil
  306. })
  307. type ProjectGroupRolesListOptions struct {
  308. PROJECT string `help:"ID or Name of Project"`
  309. GROUP string `help:"ID or Name of Group"`
  310. GroupDomain string `help:"Domain of group"`
  311. ProjectDomain string `help:"Domain of project"`
  312. }
  313. R(&ProjectGroupRolesListOptions{}, "project-group-roles", "Get roles for group in project", func(s *mcclient.ClientSession, args *ProjectGroupRolesListOptions) error {
  314. gid, err := getGroupId(s, args.GROUP, args.GroupDomain)
  315. if err != nil {
  316. return err
  317. }
  318. pid, err := getProjectId(s, args.PROJECT, args.ProjectDomain)
  319. if err != nil {
  320. return err
  321. }
  322. result, err := modules.RolesV3.ListInContexts(s, nil, []modulebase.ManagerContext{{InstanceManager: &modules.Projects, InstanceId: pid}, {InstanceManager: &modules.Groups, InstanceId: gid}})
  323. if err != nil {
  324. return err
  325. }
  326. printList(result, modules.RolesV3.GetColumns(s))
  327. return nil
  328. })
  329. /*R(&ProjectShowOptions{}, "project-shared-images", "Show shared images of a project", func(s *mcclient.ClientSession, args *ProjectShowOptions) error {
  330. query := jsonutils.NewDict()
  331. if len(args.Domain) > 0 {
  332. domainId, err := modules.Domains.GetId(s, args.Domain, nil)
  333. if err != nil {
  334. return err
  335. }
  336. query.Add(jsonutils.NewString(domainId), "domain_id")
  337. }
  338. projectId, err := modules.Projects.GetId(s, args.ID, query)
  339. if err != nil {
  340. return err
  341. }
  342. imgs, err := modules.Images.ListSharedImages(s, projectId)
  343. if err != nil {
  344. return err
  345. }
  346. printList(imgs, modules.Images.GetColumns(s))
  347. return nil
  348. })
  349. type ProjectAddTagsOptions struct {
  350. ID string `help:"ID or name of project"`
  351. Tags []string `help:"tags added to project"`
  352. }
  353. R(&ProjectAddTagsOptions{}, "project-add-tags", "Add project with tags", func(s *mcclient.ClientSession, args *ProjectAddTagsOptions) error {
  354. err := modules.Projects.AddTags(s, args.ID, args.Tags)
  355. if err != nil {
  356. return err
  357. }
  358. return nil
  359. })*/
  360. // Deprecated
  361. type ProjectBatchJoinOptions struct {
  362. Ids []string `help:"user ids or group ids"`
  363. Resource string `help:"resource type" choices:"users|groups"`
  364. Rid string `help:"role id"`
  365. Pid string `help:"project id"`
  366. }
  367. R(&ProjectBatchJoinOptions{}, "project-batch-join", "Batch join users or groups into project with role", func(s *mcclient.ClientSession, args *ProjectBatchJoinOptions) error {
  368. params := jsonutils.Marshal(args)
  369. _, err := modules.Projects.DoProjectBatchJoin(s, params)
  370. if err != nil {
  371. return err
  372. }
  373. return nil
  374. })
  375. type ProjectAddUserGroupOptions struct {
  376. Project string `help:"ID or name of project to add users/groups" positional:"true" optional:"false"`
  377. User []string `help:"ID of user to add"`
  378. Group []string `help:"ID of group to add"`
  379. Role []string `help:"ID of role to add"`
  380. EnableAllUsers bool
  381. }
  382. R(&ProjectAddUserGroupOptions{}, "project-add-user-group", "Batch add users/groups to project", func(s *mcclient.ClientSession, args *ProjectAddUserGroupOptions) error {
  383. input := api.SProjectAddUserGroupInput{}
  384. input.Users = args.User
  385. input.Groups = args.Group
  386. input.Roles = args.Role
  387. input.EnableAllUsers = args.EnableAllUsers
  388. err := input.Validate()
  389. if err != nil {
  390. return err
  391. }
  392. result, err := modules.Projects.PerformAction(s, args.Project, "join", jsonutils.Marshal(input))
  393. if err != nil {
  394. return err
  395. }
  396. printObject(result)
  397. return nil
  398. })
  399. type ProjectRemoveUserGroup struct {
  400. Project string `help:"ID or name of project to remove user/group" optional:"false" positional:"true"`
  401. User string `help:"user to remove"`
  402. Group string `help:"group to remove"`
  403. Role []string `help:"roles to remove"`
  404. }
  405. R(&ProjectRemoveUserGroup{}, "project-remove-user-group", "Remove users/groups from project", func(s *mcclient.ClientSession, args *ProjectRemoveUserGroup) error {
  406. input := api.SProjectRemoveUserGroupInput{}
  407. input.UserRoles = make([]api.SUserRole, len(args.Role))
  408. input.GroupRoles = make([]api.SGroupRole, len(args.Role))
  409. for i := range args.Role {
  410. input.UserRoles[i].User = args.User
  411. input.UserRoles[i].Role = args.Role[i]
  412. input.GroupRoles[i].Group = args.Group
  413. input.GroupRoles[i].Role = args.Role[i]
  414. }
  415. err := input.Validate()
  416. if err != nil {
  417. return err
  418. }
  419. result, err := modules.Projects.PerformAction(s, args.Project, "leave", jsonutils.Marshal(input))
  420. if err != nil {
  421. return err
  422. }
  423. printObject(result)
  424. return nil
  425. })
  426. R(&options.ResourceMetadataOptions{}, "project-add-tag", "Set tag of a project", func(s *mcclient.ClientSession, opts *options.ResourceMetadataOptions) error {
  427. params, err := opts.Params()
  428. if err != nil {
  429. return err
  430. }
  431. result, err := modules.Projects.PerformAction(s, opts.ID, "user-metadata", params)
  432. if err != nil {
  433. return err
  434. }
  435. printObject(result)
  436. return nil
  437. })
  438. R(&options.ResourceMetadataOptions{}, "project-set-tag", "Replace all tags of a project", func(s *mcclient.ClientSession, opts *options.ResourceMetadataOptions) error {
  439. params, err := opts.Params()
  440. if err != nil {
  441. return err
  442. }
  443. result, err := modules.Projects.PerformAction(s, opts.ID, "set-user-metadata", params)
  444. if err != nil {
  445. return err
  446. }
  447. printObject(result)
  448. return nil
  449. })
  450. }