rolepolicies.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. )
  21. func init() {
  22. type RolePolicyListOptions struct {
  23. api.RolePolicyListInput
  24. }
  25. R(&RolePolicyListOptions{}, "role-policy-list", "List associated policies of a role", func(s *mcclient.ClientSession, args *RolePolicyListOptions) error {
  26. results, err := modules.RolePolicies.List(s, jsonutils.Marshal(args))
  27. if err != nil {
  28. return err
  29. }
  30. printList(results, modules.RolePolicies.GetColumns(s))
  31. return nil
  32. })
  33. type RoleAddPolicyOptions struct {
  34. ID string `json:"-" help:"role id or name to add policy"`
  35. api.RolePerformAddPolicyInput
  36. }
  37. R(&RoleAddPolicyOptions{}, "role-add-policy", "Add policy to a role", func(s *mcclient.ClientSession, args *RoleAddPolicyOptions) error {
  38. result, err := modules.RolesV3.PerformAction(s, args.ID, "add-policy", jsonutils.Marshal(args))
  39. if err != nil {
  40. return err
  41. }
  42. printObject(result)
  43. return nil
  44. })
  45. type RoleRemovePolicyOptions struct {
  46. ID string `json:"-" help:"role id or name to remove policy"`
  47. api.RolePerformRemovePolicyInput
  48. }
  49. R(&RoleRemovePolicyOptions{}, "role-remove-policy", "Remove policy from a role", func(s *mcclient.ClientSession, args *RoleRemovePolicyOptions) error {
  50. result, err := modules.RolesV3.PerformAction(s, args.ID, "remove-policy", jsonutils.Marshal(args))
  51. if err != nil {
  52. return err
  53. }
  54. printObject(result)
  55. return nil
  56. })
  57. type RoleSetPolicyOptions struct {
  58. ID string `json:"-" help:"role id or name to set policies"`
  59. api.RolePerformSetPoliciesInput
  60. }
  61. R(&RoleSetPolicyOptions{}, "role-set-policies", "Set policies for a role", func(s *mcclient.ClientSession, args *RoleSetPolicyOptions) error {
  62. result, err := modules.RolesV3.PerformAction(s, args.ID, "set-policies", jsonutils.Marshal(args))
  63. if err != nil {
  64. return err
  65. }
  66. printObject(result)
  67. return nil
  68. })
  69. type RolePolicyDeleteOptions struct {
  70. ID string `json:"-" help:"id or role policy binding"`
  71. }
  72. R(&RolePolicyDeleteOptions{}, "role-policy-delete", "Delete role policy binding", func(s *mcclient.ClientSession, args *RolePolicyDeleteOptions) error {
  73. result, err := modules.RolePolicies.Delete(s, args.ID, nil)
  74. if err != nil {
  75. return err
  76. }
  77. printObject(result)
  78. return nil
  79. })
  80. }