github.com/vmware/govmomi@v0.51.0/cli/permissions/remove.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package permissions
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  
    11  	"github.com/vmware/govmomi/cli"
    12  	"github.com/vmware/govmomi/fault"
    13  	"github.com/vmware/govmomi/vim25/types"
    14  )
    15  
    16  type remove struct {
    17  	*PermissionFlag
    18  
    19  	types.Permission
    20  	force bool
    21  }
    22  
    23  func init() {
    24  	cli.Register("permissions.remove", &remove{})
    25  }
    26  
    27  func (cmd *remove) Register(ctx context.Context, f *flag.FlagSet) {
    28  	cmd.PermissionFlag, ctx = NewPermissionFlag(ctx)
    29  	cmd.PermissionFlag.Register(ctx, f)
    30  
    31  	f.StringVar(&cmd.Principal, "principal", "", "User or group for which the permission is defined")
    32  	f.BoolVar(&cmd.Group, "group", false, "True, if principal refers to a group name; false, for a user name")
    33  	f.BoolVar(&cmd.force, "f", false, "Ignore NotFound fault if permission for this entity and user or group does not exist")
    34  }
    35  
    36  func (cmd *remove) Process(ctx context.Context) error {
    37  	if err := cmd.PermissionFlag.Process(ctx); err != nil {
    38  		return err
    39  	}
    40  	return nil
    41  }
    42  
    43  func (cmd *remove) Usage() string {
    44  	return "[PATH]..."
    45  }
    46  
    47  func (cmd *remove) Description() string {
    48  	return `Removes a permission rule from managed entities.
    49  
    50  Examples:
    51    govc permissions.remove -principal root
    52    govc permissions.remove -principal $USER@vsphere.local /dc1/host/cluster1`
    53  }
    54  
    55  func (cmd *remove) Run(ctx context.Context, f *flag.FlagSet) error {
    56  	refs, err := cmd.ManagedObjects(ctx, f.Args())
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	m, err := cmd.Manager(ctx)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	for _, ref := range refs {
    67  		err = m.RemoveEntityPermission(ctx, ref, cmd.Principal, cmd.Group)
    68  		if err != nil {
    69  			if cmd.force {
    70  				if fault.Is(err, &types.NotFound{}) {
    71  					continue
    72  				}
    73  			}
    74  			return err
    75  		}
    76  	}
    77  
    78  	return nil
    79  }