github.com/vmware/govmomi@v0.51.0/cli/vm/dataset/update.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 dataset
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"flag"
    11  
    12  	"github.com/vmware/govmomi/cli"
    13  	"github.com/vmware/govmomi/cli/flags"
    14  	"github.com/vmware/govmomi/vapi/vm/dataset"
    15  )
    16  
    17  type update struct {
    18  	*flags.VirtualMachineFlag
    19  
    20  	description              string
    21  	host                     dataset.Access
    22  	guest                    dataset.Access
    23  	omitFromSnapshotAndClone *bool
    24  }
    25  
    26  func init() {
    27  	cli.Register("vm.dataset.update", &update{})
    28  }
    29  
    30  func FindDataSetId(ctx context.Context, mgr *dataset.Manager, vmId string, nameOrId string) (string, error) {
    31  	l, err := mgr.ListDataSets(ctx, vmId)
    32  	if err != nil {
    33  		return "", err
    34  	}
    35  	for _, summary := range l {
    36  		if nameOrId == summary.DataSet || nameOrId == summary.Name {
    37  			return summary.DataSet, nil
    38  		}
    39  	}
    40  	return nameOrId, nil
    41  }
    42  
    43  func (cmd *update) Register(ctx context.Context, f *flag.FlagSet) {
    44  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    45  	cmd.VirtualMachineFlag.Register(ctx, f)
    46  	f.StringVar(&cmd.description, "d", "", "Description")
    47  	f.StringVar((*string)(&cmd.host), "host-access", "", hostAccessUsage())
    48  	f.StringVar((*string)(&cmd.guest), "guest-access", "", guestAccessUsage())
    49  	f.Var(flags.NewOptionalBool(&cmd.omitFromSnapshotAndClone), "omit-from-snapshot", "Omit the data set from snapshots and clones of the VM")
    50  }
    51  
    52  func (cmd *update) Process(ctx context.Context) error {
    53  	return cmd.VirtualMachineFlag.Process(ctx)
    54  }
    55  
    56  func (cmd *update) Usage() string {
    57  	return "NAME"
    58  }
    59  
    60  func (cmd *update) Description() string {
    61  	return `Update data set.
    62  
    63  Examples:
    64    govc vm.dataset.update -vm $vm -d "New description." -guest-access READ_ONLY com.example.project2
    65    govc vm.dataset.update -vm $vm -omit-from-snapshot=false com.example.project3`
    66  }
    67  
    68  func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error {
    69  	if f.NArg() != 1 {
    70  		return flag.ErrHelp
    71  	}
    72  
    73  	vm, err := cmd.VirtualMachineFlag.VirtualMachine()
    74  	if err != nil {
    75  		return err
    76  	}
    77  	if vm == nil {
    78  		return flag.ErrHelp
    79  	}
    80  	vmId := vm.Reference().Value
    81  
    82  	if cmd.host != "" && !validateDataSetAccess(cmd.host) {
    83  		return errors.New("please specify valid host access")
    84  	}
    85  	if cmd.guest != "" && !validateDataSetAccess(cmd.guest) {
    86  		return errors.New("please specify valid guest access")
    87  	}
    88  
    89  	c, err := cmd.RestClient()
    90  	if err != nil {
    91  		return err
    92  	}
    93  	mgr := dataset.NewManager(c)
    94  
    95  	id, err := FindDataSetId(ctx, mgr, vmId, f.Arg(0))
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	// Update only the fields which the user asked for
   101  	updateSpec := dataset.UpdateSpec{}
   102  	if cmd.description != "" {
   103  		updateSpec.Description = &cmd.description
   104  	}
   105  	if cmd.host != "" {
   106  		updateSpec.Host = &cmd.host
   107  	}
   108  	if cmd.guest != "" {
   109  		updateSpec.Guest = &cmd.guest
   110  	}
   111  	if cmd.omitFromSnapshotAndClone != nil {
   112  		updateSpec.OmitFromSnapshotAndClone = cmd.omitFromSnapshotAndClone
   113  	}
   114  
   115  	err = mgr.UpdateDataSet(ctx, vmId, id, &updateSpec)
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	return nil
   121  }