github.com/vmware/govmomi@v0.43.0/govc/vm/dataset/create.go (about)

     1  /*
     2  Copyright (c) 2023-2023 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 dataset
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"flag"
    23  	"fmt"
    24  	"strings"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/vapi/vm/dataset"
    29  )
    30  
    31  var accessModes = []string{
    32  	string(dataset.AccessNone),
    33  	string(dataset.AccessReadOnly),
    34  	string(dataset.AccessReadWrite),
    35  }
    36  
    37  func hostAccessUsage() string {
    38  	return fmt.Sprintf("Access to the data set entries from the ESXi host and the vCenter (%s)", strings.Join(accessModes, "|"))
    39  }
    40  
    41  func guestAccessUsage() string {
    42  	return fmt.Sprintf("Access to the data set entries from the VM guest OS (%s)", strings.Join(accessModes, "|"))
    43  }
    44  
    45  func validateDataSetAccess(access dataset.Access) bool {
    46  	for _, validAccess := range accessModes {
    47  		if string(access) == validAccess {
    48  			return true
    49  		}
    50  	}
    51  	return false
    52  }
    53  
    54  type create struct {
    55  	*flags.VirtualMachineFlag
    56  	spec dataset.CreateSpec
    57  }
    58  
    59  func init() {
    60  	cli.Register("vm.dataset.create", &create{})
    61  }
    62  
    63  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    64  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    65  	cmd.VirtualMachineFlag.Register(ctx, f)
    66  	f.StringVar(&cmd.spec.Description, "d", "", "Description")
    67  	f.StringVar((*string)(&cmd.spec.Host), "host-access", string(dataset.AccessReadWrite), hostAccessUsage())
    68  	f.StringVar((*string)(&cmd.spec.Guest), "guest-access", string(dataset.AccessReadWrite), guestAccessUsage())
    69  	f.Var(flags.NewOptionalBool(&cmd.spec.OmitFromSnapshotAndClone), "omit-from-snapshot", "Omit the data set from snapshots and clones of the VM (defaults to false)")
    70  }
    71  
    72  func (cmd *create) Process(ctx context.Context) error {
    73  	return cmd.VirtualMachineFlag.Process(ctx)
    74  }
    75  
    76  func (cmd *create) Usage() string {
    77  	return "NAME"
    78  }
    79  
    80  func (cmd *create) Description() string {
    81  	return `Create data set on a VM.
    82  
    83  This command will output the ID of the new data set.
    84  
    85  Examples:
    86    govc vm.dataset.create -vm $vm -d "Data set for project 2" -host-access READ_WRITE -guest-access READ_ONLY com.example.project2
    87    govc vm.dataset.create -vm $vm -d "Data set for project 3" -omit-from-snapshot=false com.example.project3`
    88  }
    89  
    90  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    91  	if f.NArg() != 1 {
    92  		return flag.ErrHelp
    93  	}
    94  
    95  	vm, err := cmd.VirtualMachineFlag.VirtualMachine()
    96  	if err != nil {
    97  		return err
    98  	}
    99  	if vm == nil {
   100  		return flag.ErrHelp
   101  	}
   102  	vmId := vm.Reference().Value
   103  
   104  	cmd.spec.Name = f.Arg(0)
   105  	if !validateDataSetAccess(cmd.spec.Host) {
   106  		return errors.New("please specify valid host access")
   107  	}
   108  	if !validateDataSetAccess(cmd.spec.Guest) {
   109  		return errors.New("please specify valid guest access")
   110  	}
   111  
   112  	c, err := cmd.RestClient()
   113  	if err != nil {
   114  		return err
   115  	}
   116  	mgr := dataset.NewManager(c)
   117  	id, err := mgr.CreateDataSet(ctx, vmId, &cmd.spec)
   118  	if err != nil {
   119  		return err
   120  	}
   121  	fmt.Println(id)
   122  	return nil
   123  }