github.com/vmware/govmomi@v0.37.1/govc/fields/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 fields
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"strings"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/object"
    27  )
    28  
    29  type set struct {
    30  	*flags.DatacenterFlag
    31  	add  bool
    32  	kind string
    33  }
    34  
    35  func init() {
    36  	cli.Register("fields.set", &set{})
    37  }
    38  
    39  func (cmd *set) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    41  	cmd.DatacenterFlag.Register(ctx, f)
    42  
    43  	f.StringVar(&cmd.kind, "type", "", "Managed object type on which to add "+
    44  		"the field if it does not exist. This flag is ignored unless -add=true")
    45  	f.BoolVar(&cmd.add, "add", false, "Adds the field if it does not exist. "+
    46  		"Use the -type flag to specify the managed object type to which the "+
    47  		"field is added. Using -add and omitting -kind causes a new, global "+
    48  		"field to be created if a field with the provided name does not "+
    49  		"already exist.")
    50  }
    51  
    52  func (cmd *set) Usage() string {
    53  	return "KEY VALUE PATH..."
    54  }
    55  
    56  func (cmd *set) Description() string {
    57  	return `Set custom field values for PATH.
    58  
    59  Examples:
    60    govc fields.set my-field-name field-value vm/my-vm
    61    govc fields.set -add my-new-global-field-name field-value vm/my-vm
    62    govc fields.set -add -type VirtualMachine my-new-vm-field-name field-value vm/my-vm`
    63  }
    64  
    65  func (cmd *set) Run(ctx context.Context, f *flag.FlagSet) error {
    66  	if f.NArg() < 3 {
    67  		return flag.ErrHelp
    68  	}
    69  
    70  	c, err := cmd.Client()
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	m, err := object.GetCustomFieldsManager(c)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	args := f.Args()
    81  
    82  	key, err := m.FindKey(ctx, args[0])
    83  	if err != nil {
    84  		if !(cmd.add && strings.Contains(err.Error(), "key name not found")) {
    85  			return err
    86  		}
    87  		// Add the missing field.
    88  		def, err := m.Add(ctx, args[0], cmd.kind, nil, nil)
    89  		if err != nil {
    90  			return err
    91  		}
    92  		// Assign the new field's key to the "key" var used below when
    93  		// setting the key/value pair on the provided list of objects.
    94  		key = def.Key
    95  	}
    96  
    97  	val := args[1]
    98  
    99  	objs, err := cmd.ManagedObjects(ctx, args[2:])
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	for _, ref := range objs {
   105  		err := m.Set(ctx, ref, key, val)
   106  		if err != nil {
   107  			return err
   108  		}
   109  	}
   110  
   111  	return nil
   112  }