github.com/vmware/govmomi@v0.43.0/govc/disk/attach.go (about)

     1  /*
     2  Copyright (c) 2024-2024 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 disk
    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/vim25/mo"
    26  )
    27  
    28  type attach struct {
    29  	*flags.VirtualMachineFlag
    30  	*flags.DatastoreFlag
    31  }
    32  
    33  func init() {
    34  	cli.Register("disk.attach", &attach{})
    35  }
    36  
    37  func (cmd *attach) Register(ctx context.Context, f *flag.FlagSet) {
    38  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    39  	cmd.VirtualMachineFlag.Register(ctx, f)
    40  	cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx)
    41  	cmd.DatastoreFlag.Register(ctx, f)
    42  }
    43  
    44  func (cmd *attach) Process(ctx context.Context) error {
    45  	if err := cmd.VirtualMachineFlag.Process(ctx); err != nil {
    46  		return err
    47  	}
    48  	return cmd.DatastoreFlag.Process(ctx)
    49  }
    50  
    51  func (cmd *attach) Usage() string {
    52  	return "ID"
    53  }
    54  
    55  func (cmd *attach) Description() string {
    56  	return `Attach disk ID on VM.
    57  
    58  See also: govc vm.disk.attach
    59  
    60  Examples:
    61    govc disk.attach -vm $vm ID
    62    govc disk.attach -vm $vm -ds $ds ID`
    63  }
    64  
    65  func (cmd *attach) Run(ctx context.Context, f *flag.FlagSet) error {
    66  	if f.NArg() != 1 {
    67  		return flag.ErrHelp
    68  	}
    69  
    70  	ds, err := cmd.DatastoreIfSpecified()
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	vm, err := cmd.VirtualMachine()
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	if ds == nil {
    81  		var props mo.VirtualMachine
    82  		err = vm.Properties(ctx, vm.Reference(), []string{"datastore"}, &props)
    83  		if err != nil {
    84  			return err
    85  		}
    86  		if len(props.Datastore) != 1 {
    87  			ds, err = cmd.Datastore() // likely results in MultipleFoundError
    88  			if err != nil {
    89  				return err
    90  			}
    91  		}
    92  	}
    93  
    94  	id := f.Arg(0)
    95  
    96  	return vm.AttachDisk(ctx, id, ds, 0, nil)
    97  }