github.com/vmware/govmomi@v0.37.2/govc/datastore/maintenance/enter.go (about)

     1  /*
     2  Copyright (c) 2019 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 maintenance
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    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/methods"
    28  	"github.com/vmware/govmomi/vim25/types"
    29  )
    30  
    31  type enter struct {
    32  	*flags.DatastoreFlag
    33  }
    34  
    35  func init() {
    36  	cli.Register("datastore.maintenance.enter", &enter{})
    37  }
    38  
    39  func (cmd *enter) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx)
    41  	cmd.DatastoreFlag.Register(ctx, f)
    42  }
    43  
    44  func (cmd *enter) Usage() string {
    45  	return "DATASTORE"
    46  }
    47  
    48  func (cmd *enter) Description() string {
    49  	return `Put DATASTORE in maintenance mode.
    50  
    51  Examples:
    52    govc datastore.cluster.change -drs-mode automated my-datastore-cluster # automatically schedule Storage DRS migration
    53    govc datastore.maintenance.enter -ds my-datastore-cluster/datastore1
    54    # no virtual machines can be powered on and no provisioning operations can be performed on the datastore during this time
    55    govc datastore.maintenance.exit -ds my-datastore-cluster/datastore1`
    56  }
    57  
    58  func (cmd *enter) EnterMaintenanceMode(ctx context.Context, ds *object.Datastore) error {
    59  	req := &types.DatastoreEnterMaintenanceMode{
    60  		This: ds.Reference(),
    61  	}
    62  	res, err := methods.DatastoreEnterMaintenanceMode(ctx, ds.Client(), req)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	logger := cmd.ProgressLogger(fmt.Sprintf("%s entering maintenance mode... ", ds.InventoryPath))
    68  	defer logger.Wait()
    69  
    70  	task := object.NewTask(ds.Client(), *res.Returnval.Task)
    71  	_, err = task.WaitForResult(ctx, logger)
    72  	return err
    73  }
    74  
    75  func (cmd *enter) Run(ctx context.Context, f *flag.FlagSet) error {
    76  	ds, err := cmd.Datastore()
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	return cmd.EnterMaintenanceMode(ctx, ds)
    82  }