github.com/vmware/govmomi@v0.37.2/govc/datastore/cluster/change.go (about)

     1  /*
     2  Copyright (c) 2018 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 cluster
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"strings"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/object"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  func DrsBehaviorUsage() string {
    31  	drsModes := []string{
    32  		string(types.StorageDrsPodConfigInfoBehaviorManual),
    33  		string(types.StorageDrsPodConfigInfoBehaviorAutomated),
    34  	}
    35  
    36  	return "Storage DRS behavior: " + strings.Join(drsModes, ", ")
    37  }
    38  
    39  type change struct {
    40  	*flags.DatacenterFlag
    41  
    42  	types.StorageDrsConfigSpec
    43  }
    44  
    45  func init() {
    46  	cli.Register("datastore.cluster.change", &change{})
    47  }
    48  
    49  func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
    50  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    51  	cmd.DatacenterFlag.Register(ctx, f)
    52  
    53  	cmd.PodConfigSpec = new(types.StorageDrsPodConfigSpec)
    54  
    55  	f.Var(flags.NewOptionalBool(&cmd.PodConfigSpec.Enabled), "drs-enabled", "Enable Storage DRS")
    56  
    57  	f.StringVar((*string)(&cmd.PodConfigSpec.DefaultVmBehavior), "drs-mode", "", DrsBehaviorUsage())
    58  }
    59  
    60  func (cmd *change) Usage() string {
    61  	return "CLUSTER..."
    62  }
    63  
    64  func (cmd *change) Description() string {
    65  	return `Change configuration of the given datastore clusters.
    66  
    67  Examples:
    68    govc datastore.cluster.change -drs-enabled ClusterA
    69    govc datastore.cluster.change -drs-enabled=false ClusterB`
    70  }
    71  
    72  func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
    73  	client, err := cmd.Client()
    74  	if err != nil {
    75  		return err
    76  	}
    77  	finder, err := cmd.Finder()
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	m := object.NewStorageResourceManager(client)
    83  
    84  	for _, path := range f.Args() {
    85  		clusters, err := finder.DatastoreClusterList(ctx, path)
    86  		if err != nil {
    87  			return err
    88  		}
    89  
    90  		for _, cluster := range clusters {
    91  			task, err := m.ConfigureStorageDrsForPod(ctx, cluster, cmd.StorageDrsConfigSpec, true)
    92  			if err != nil {
    93  				return err
    94  			}
    95  
    96  			_, err = task.WaitForResult(ctx, nil)
    97  			if err != nil {
    98  				return err
    99  			}
   100  		}
   101  	}
   102  
   103  	return nil
   104  }