github.com/vmware/govmomi@v0.43.0/govc/pool/destroy.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 pool
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/find"
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  )
    27  
    28  type destroy struct {
    29  	*flags.DatacenterFlag
    30  
    31  	children bool
    32  }
    33  
    34  func init() {
    35  	cli.Register("pool.destroy", &destroy{})
    36  }
    37  
    38  func (cmd *destroy) Register(ctx context.Context, f *flag.FlagSet) {
    39  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    40  	cmd.DatacenterFlag.Register(ctx, f)
    41  
    42  	f.BoolVar(&cmd.children, "children", false, "Remove all children pools")
    43  }
    44  
    45  func (cmd *destroy) Process(ctx context.Context) error {
    46  	if err := cmd.DatacenterFlag.Process(ctx); err != nil {
    47  		return err
    48  	}
    49  	return nil
    50  }
    51  
    52  func (cmd *destroy) Usage() string {
    53  	return "POOL..."
    54  }
    55  
    56  func (cmd *destroy) Description() string {
    57  	return "Destroy one or more resource POOLs.\n" + poolNameHelp
    58  }
    59  
    60  func (cmd *destroy) Run(ctx context.Context, f *flag.FlagSet) error {
    61  	if f.NArg() == 0 {
    62  		return flag.ErrHelp
    63  	}
    64  
    65  	finder, err := cmd.Finder()
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	for _, arg := range f.Args() {
    71  		pools, err := finder.ResourcePoolList(ctx, arg)
    72  		if err != nil {
    73  			if _, ok := err.(*find.NotFoundError); ok {
    74  				// Ignore if pool cannot be found
    75  				continue
    76  			}
    77  
    78  			return err
    79  		}
    80  
    81  		for _, pool := range pools {
    82  			if cmd.children {
    83  				err = pool.DestroyChildren(ctx)
    84  				if err != nil {
    85  					return err
    86  				}
    87  			} else {
    88  				task, err := pool.Destroy(ctx)
    89  				if err != nil {
    90  					return err
    91  				}
    92  				err = task.Wait(ctx)
    93  				if err != nil {
    94  					return err
    95  				}
    96  			}
    97  		}
    98  	}
    99  
   100  	return nil
   101  }