github.com/vmware/govmomi@v0.37.2/govc/cluster/override/remove.go (about)

     1  /*
     2  Copyright (c) 2017 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 override
    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/types"
    26  )
    27  
    28  type remove struct {
    29  	*flags.ClusterFlag
    30  	*flags.VirtualMachineFlag
    31  }
    32  
    33  func init() {
    34  	cli.Register("cluster.override.remove", &remove{})
    35  }
    36  
    37  func (cmd *remove) Register(ctx context.Context, f *flag.FlagSet) {
    38  	cmd.ClusterFlag, ctx = flags.NewClusterFlag(ctx)
    39  	cmd.ClusterFlag.Register(ctx, f)
    40  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    41  	cmd.VirtualMachineFlag.Register(ctx, f)
    42  }
    43  
    44  func (cmd *remove) Description() string {
    45  	return `Remove cluster VM overrides.
    46  
    47  Examples:
    48    govc cluster.override.remove -cluster cluster_1 -vm vm_1`
    49  }
    50  
    51  func (cmd *remove) Process(ctx context.Context) error {
    52  	if err := cmd.ClusterFlag.Process(ctx); err != nil {
    53  		return err
    54  	}
    55  	return cmd.VirtualMachineFlag.Process(ctx)
    56  }
    57  
    58  func (cmd *remove) Run(ctx context.Context, f *flag.FlagSet) error {
    59  	vm, err := cmd.VirtualMachine()
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	if vm == nil {
    65  		return flag.ErrHelp
    66  	}
    67  
    68  	cluster, err := cmd.Cluster()
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	config, err := cluster.Configuration(ctx)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	spec := &types.ClusterConfigSpecEx{}
    79  	ref := vm.Reference()
    80  
    81  	for _, c := range config.DrsVmConfig {
    82  		if c.Key == ref {
    83  			spec.DrsVmConfigSpec = []types.ClusterDrsVmConfigSpec{
    84  				{
    85  					ArrayUpdateSpec: types.ArrayUpdateSpec{
    86  						Operation: types.ArrayUpdateOperationRemove,
    87  						RemoveKey: ref,
    88  					},
    89  				},
    90  			}
    91  			break
    92  		}
    93  	}
    94  
    95  	for _, c := range config.DasVmConfig {
    96  		if c.Key == ref {
    97  			spec.DasVmConfigSpec = []types.ClusterDasVmConfigSpec{
    98  				{
    99  					ArrayUpdateSpec: types.ArrayUpdateSpec{
   100  						Operation: types.ArrayUpdateOperationRemove,
   101  						RemoveKey: ref,
   102  					},
   103  				},
   104  			}
   105  			break
   106  		}
   107  	}
   108  
   109  	for _, c := range config.VmOrchestration {
   110  		if c.Vm == ref {
   111  			spec.VmOrchestrationSpec = []types.ClusterVmOrchestrationSpec{
   112  				{
   113  					ArrayUpdateSpec: types.ArrayUpdateSpec{
   114  						Operation: types.ArrayUpdateOperationRemove,
   115  						RemoveKey: ref,
   116  					},
   117  				},
   118  			}
   119  			break
   120  		}
   121  	}
   122  
   123  	return cmd.Reconfigure(ctx, spec)
   124  }