github.com/vmware/govmomi@v0.37.2/govc/object/method.go (about)

     1  /*
     2  Copyright (c) 2017 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 object
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/govc/flags"
    25  	"github.com/vmware/govmomi/object"
    26  )
    27  
    28  type method struct {
    29  	*flags.DatacenterFlag
    30  
    31  	name   string
    32  	reason string
    33  	source string
    34  	enable bool
    35  }
    36  
    37  func init() {
    38  	cli.Register("object.method", &method{})
    39  }
    40  
    41  func (cmd *method) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    43  	cmd.DatacenterFlag.Register(ctx, f)
    44  
    45  	f.StringVar(&cmd.name, "name", "", "Method name")
    46  	f.StringVar(&cmd.reason, "reason", "", "Reason for disabling method")
    47  	f.StringVar(&cmd.source, "source", "govc", "Source ID")
    48  	f.BoolVar(&cmd.enable, "enable", true, "Enable method")
    49  }
    50  
    51  func (cmd *method) Usage() string {
    52  	return "PATH..."
    53  }
    54  
    55  func (cmd *method) Description() string {
    56  	return `Enable or disable methods for managed objects.
    57  
    58  Examples:
    59    govc object.method -name Destroy_Task -enable=false /dc1/vm/foo
    60    govc object.collect /dc1/vm/foo disabledMethod | grep --color Destroy_Task
    61    govc object.method -name Destroy_Task -enable /dc1/vm/foo`
    62  }
    63  
    64  func (cmd *method) Process(ctx context.Context) error {
    65  	if err := cmd.DatacenterFlag.Process(ctx); err != nil {
    66  		return err
    67  	}
    68  	return nil
    69  }
    70  
    71  func (cmd *method) Run(ctx context.Context, f *flag.FlagSet) error {
    72  	if f.NArg() == 0 {
    73  		return flag.ErrHelp
    74  	}
    75  
    76  	if cmd.name == "" {
    77  		return flag.ErrHelp
    78  	}
    79  
    80  	c, err := cmd.Client()
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	objs, err := cmd.ManagedObjects(ctx, f.Args())
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	m := object.NewAuthorizationManager(c)
    91  
    92  	if cmd.enable {
    93  		return m.EnableMethods(ctx, objs, []string{cmd.name}, cmd.source)
    94  	}
    95  
    96  	method := []object.DisabledMethodRequest{
    97  		{
    98  			Method: cmd.name,
    99  			Reason: cmd.reason,
   100  		},
   101  	}
   102  
   103  	return m.DisableMethods(ctx, objs, method, cmd.source)
   104  }