github.com/vmware/govmomi@v0.37.2/govc/permissions/remove.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/soap"
    25  	"github.com/vmware/govmomi/vim25/types"
    26  )
    27  
    28  type remove struct {
    29  	*PermissionFlag
    30  
    31  	types.Permission
    32  	force bool
    33  }
    34  
    35  func init() {
    36  	cli.Register("permissions.remove", &remove{})
    37  }
    38  
    39  func (cmd *remove) 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.force, "f", false, "Ignore NotFound fault if permission for this entity and user or group does not exist")
    46  }
    47  
    48  func (cmd *remove) Process(ctx context.Context) error {
    49  	if err := cmd.PermissionFlag.Process(ctx); err != nil {
    50  		return err
    51  	}
    52  	return nil
    53  }
    54  
    55  func (cmd *remove) Usage() string {
    56  	return "[PATH]..."
    57  }
    58  
    59  func (cmd *remove) Description() string {
    60  	return `Removes a permission rule from managed entities.
    61  
    62  Examples:
    63    govc permissions.remove -principal root
    64    govc permissions.remove -principal $USER@vsphere.local /dc1/host/cluster1`
    65  }
    66  
    67  func (cmd *remove) Run(ctx context.Context, f *flag.FlagSet) error {
    68  	refs, err := cmd.ManagedObjects(ctx, f.Args())
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	m, err := cmd.Manager(ctx)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	for _, ref := range refs {
    79  		err = m.RemoveEntityPermission(ctx, ref, cmd.Principal, cmd.Group)
    80  		if err != nil {
    81  			if cmd.force && soap.IsSoapFault(err) {
    82  				_, ok := soap.ToSoapFault(err).VimFault().(types.NotFound)
    83  				if ok {
    84  					continue
    85  				}
    86  			}
    87  			return err
    88  		}
    89  	}
    90  
    91  	return nil
    92  }