github.com/vmware/govmomi@v0.37.1/govc/volume/rm.go (about)

     1  /*
     2  Copyright (c) 2020 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 volume
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"flag"
    23  
    24  	"github.com/vmware/govmomi/cns/types"
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/govc/flags"
    27  	"github.com/vmware/govmomi/vim25/soap"
    28  )
    29  
    30  type rm struct {
    31  	*flags.ClientFlag
    32  }
    33  
    34  func init() {
    35  	cli.Register("volume.rm", &rm{})
    36  }
    37  
    38  func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) {
    39  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    40  	cmd.ClientFlag.Register(ctx, f)
    41  }
    42  
    43  func (cmd *rm) Usage() string {
    44  	return "ID"
    45  }
    46  
    47  func (cmd *rm) Description() string {
    48  	return `Remove CNS volume.
    49  
    50  Note: if volume.rm returns not found errors,
    51  consider using 'govc disk.ls -R' to reconcile the datastore inventory.
    52  
    53  Examples:
    54    govc volume.rm f75989dc-95b9-4db7-af96-8583f24bc59d`
    55  }
    56  
    57  func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
    58  	c, err := cmd.CnsClient()
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	ids := []types.CnsVolumeId{{Id: f.Arg(0)}}
    64  
    65  	task, err := c.DeleteVolume(ctx, ids, true)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	info, err := task.WaitForResult(ctx, nil)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	if res, ok := info.Result.(types.CnsVolumeOperationBatchResult); ok {
    76  		for _, r := range res.VolumeResults {
    77  			fault := r.GetCnsVolumeOperationResult().Fault
    78  
    79  			if fault != nil {
    80  				if fault.Fault != nil {
    81  					return soap.WrapVimFault(fault.Fault)
    82  				}
    83  				return errors.New(fault.LocalizedMessage)
    84  			}
    85  		}
    86  	}
    87  
    88  	return nil
    89  }