github.com/vmware/govmomi@v0.37.1/govc/permissions/set.go (about) 1 /* 2 Copyright (c) 2015 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package permissions 18 19 import ( 20 "context" 21 "flag" 22 23 "github.com/vmware/govmomi/govc/cli" 24 "github.com/vmware/govmomi/vim25/types" 25 ) 26 27 type set struct { 28 *PermissionFlag 29 30 types.Permission 31 32 role string 33 } 34 35 func init() { 36 cli.Register("permissions.set", &set{}) 37 } 38 39 func (cmd *set) Register(ctx context.Context, f *flag.FlagSet) { 40 cmd.PermissionFlag, ctx = NewPermissionFlag(ctx) 41 cmd.PermissionFlag.Register(ctx, f) 42 43 f.StringVar(&cmd.Principal, "principal", "", "User or group for which the permission is defined") 44 f.BoolVar(&cmd.Group, "group", false, "True, if principal refers to a group name; false, for a user name") 45 f.BoolVar(&cmd.Propagate, "propagate", true, "Whether or not this permission propagates down the hierarchy to sub-entities") 46 f.StringVar(&cmd.role, "role", "Admin", "Permission role name") 47 } 48 49 func (cmd *set) Process(ctx context.Context) error { 50 if err := cmd.PermissionFlag.Process(ctx); err != nil { 51 return err 52 } 53 return nil 54 } 55 56 func (cmd *set) Usage() string { 57 return "[PATH]..." 58 } 59 60 func (cmd *set) Description() string { 61 return `Set the permissions managed entities. 62 63 Examples: 64 govc permissions.set -principal root -role Admin 65 govc permissions.set -principal $USER@vsphere.local -role Admin /dc1/host/cluster1` 66 } 67 68 func (cmd *set) Run(ctx context.Context, f *flag.FlagSet) error { 69 refs, err := cmd.ManagedObjects(ctx, f.Args()) 70 if err != nil { 71 return err 72 } 73 74 m, err := cmd.Manager(ctx) 75 if err != nil { 76 return err 77 } 78 79 role, err := cmd.Role(cmd.role) 80 if err != nil { 81 return err 82 } 83 84 cmd.Permission.RoleId = role.RoleId 85 86 perms := []types.Permission{cmd.Permission} 87 88 for _, ref := range refs { 89 err = m.SetEntityPermissions(ctx, ref, perms) 90 if err != nil { 91 return err 92 } 93 } 94 95 return nil 96 }