github.com/vmware/govmomi@v0.37.2/govc/disk/snapshot/create.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 snapshot
    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/vim25/types"
    27  	"github.com/vmware/govmomi/vslm"
    28  )
    29  
    30  type create struct {
    31  	*flags.DatastoreFlag
    32  }
    33  
    34  func init() {
    35  	cli.Register("disk.snapshot.create", &create{})
    36  }
    37  
    38  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    39  	cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx)
    40  	cmd.DatastoreFlag.Register(ctx, f)
    41  }
    42  
    43  func (cmd *create) Usage() string {
    44  	return "ID DESC"
    45  }
    46  
    47  func (cmd *create) Description() string {
    48  	return `Create snapshot of ID on DS.
    49  
    50  Examples:
    51    govc disk.snapshot.create b9fe5f17-3b87-4a03-9739-09a82ddcc6b0 my-disk-snapshot`
    52  }
    53  
    54  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    55  	ds, err := cmd.Datastore()
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	m := vslm.NewObjectManager(ds.Client())
    61  	id := f.Arg(0)
    62  
    63  	task, err := m.CreateSnapshot(ctx, ds, id, f.Arg(1))
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	logger := cmd.ProgressLogger(fmt.Sprintf("Snapshot %s...", id))
    69  
    70  	res, err := task.WaitForResult(ctx, logger)
    71  	logger.Wait()
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	fmt.Println(res.Result.(types.ID).Id)
    77  
    78  	return nil
    79  }