authorization_manager.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright (c) 2015 VMware, Inc. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package object
  14. import (
  15. "context"
  16. "github.com/vmware/govmomi/vim25"
  17. "github.com/vmware/govmomi/vim25/methods"
  18. "github.com/vmware/govmomi/vim25/mo"
  19. "github.com/vmware/govmomi/vim25/types"
  20. )
  21. type AuthorizationManager struct {
  22. Common
  23. }
  24. func NewAuthorizationManager(c *vim25.Client) *AuthorizationManager {
  25. m := AuthorizationManager{
  26. Common: NewCommon(c, *c.ServiceContent.AuthorizationManager),
  27. }
  28. return &m
  29. }
  30. type AuthorizationRoleList []types.AuthorizationRole
  31. func (l AuthorizationRoleList) ById(id int32) *types.AuthorizationRole {
  32. for _, role := range l {
  33. if role.RoleId == id {
  34. return &role
  35. }
  36. }
  37. return nil
  38. }
  39. func (l AuthorizationRoleList) ByName(name string) *types.AuthorizationRole {
  40. for _, role := range l {
  41. if role.Name == name {
  42. return &role
  43. }
  44. }
  45. return nil
  46. }
  47. func (m AuthorizationManager) RoleList(ctx context.Context) (AuthorizationRoleList, error) {
  48. var am mo.AuthorizationManager
  49. err := m.Properties(ctx, m.Reference(), []string{"roleList"}, &am)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return AuthorizationRoleList(am.RoleList), nil
  54. }
  55. func (m AuthorizationManager) RetrieveEntityPermissions(ctx context.Context, entity types.ManagedObjectReference, inherited bool) ([]types.Permission, error) {
  56. req := types.RetrieveEntityPermissions{
  57. This: m.Reference(),
  58. Entity: entity,
  59. Inherited: inherited,
  60. }
  61. res, err := methods.RetrieveEntityPermissions(ctx, m.Client(), &req)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return res.Returnval, nil
  66. }
  67. func (m AuthorizationManager) RemoveEntityPermission(ctx context.Context, entity types.ManagedObjectReference, user string, isGroup bool) error {
  68. req := types.RemoveEntityPermission{
  69. This: m.Reference(),
  70. Entity: entity,
  71. User: user,
  72. IsGroup: isGroup,
  73. }
  74. _, err := methods.RemoveEntityPermission(ctx, m.Client(), &req)
  75. return err
  76. }
  77. func (m AuthorizationManager) SetEntityPermissions(ctx context.Context, entity types.ManagedObjectReference, permission []types.Permission) error {
  78. req := types.SetEntityPermissions{
  79. This: m.Reference(),
  80. Entity: entity,
  81. Permission: permission,
  82. }
  83. _, err := methods.SetEntityPermissions(ctx, m.Client(), &req)
  84. return err
  85. }
  86. func (m AuthorizationManager) RetrieveRolePermissions(ctx context.Context, id int32) ([]types.Permission, error) {
  87. req := types.RetrieveRolePermissions{
  88. This: m.Reference(),
  89. RoleId: id,
  90. }
  91. res, err := methods.RetrieveRolePermissions(ctx, m.Client(), &req)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return res.Returnval, nil
  96. }
  97. func (m AuthorizationManager) RetrieveAllPermissions(ctx context.Context) ([]types.Permission, error) {
  98. req := types.RetrieveAllPermissions{
  99. This: m.Reference(),
  100. }
  101. res, err := methods.RetrieveAllPermissions(ctx, m.Client(), &req)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return res.Returnval, nil
  106. }
  107. func (m AuthorizationManager) AddRole(ctx context.Context, name string, ids []string) (int32, error) {
  108. req := types.AddAuthorizationRole{
  109. This: m.Reference(),
  110. Name: name,
  111. PrivIds: ids,
  112. }
  113. res, err := methods.AddAuthorizationRole(ctx, m.Client(), &req)
  114. if err != nil {
  115. return -1, err
  116. }
  117. return res.Returnval, nil
  118. }
  119. func (m AuthorizationManager) RemoveRole(ctx context.Context, id int32, failIfUsed bool) error {
  120. req := types.RemoveAuthorizationRole{
  121. This: m.Reference(),
  122. RoleId: id,
  123. FailIfUsed: failIfUsed,
  124. }
  125. _, err := methods.RemoveAuthorizationRole(ctx, m.Client(), &req)
  126. return err
  127. }
  128. func (m AuthorizationManager) UpdateRole(ctx context.Context, id int32, name string, ids []string) error {
  129. req := types.UpdateAuthorizationRole{
  130. This: m.Reference(),
  131. RoleId: id,
  132. NewName: name,
  133. PrivIds: ids,
  134. }
  135. _, err := methods.UpdateAuthorizationRole(ctx, m.Client(), &req)
  136. return err
  137. }