github.com/vmware/govmomi@v0.51.0/cli/vlcm/depot/offline/create.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package offline
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  
    11  	"github.com/vmware/govmomi/cli"
    12  	"github.com/vmware/govmomi/cli/flags"
    13  	"github.com/vmware/govmomi/vapi/cis/tasks"
    14  	"github.com/vmware/govmomi/vapi/esx/settings/depots"
    15  )
    16  
    17  type create struct {
    18  	*flags.ClientFlag
    19  
    20  	spec depots.SettingsDepotsOfflineCreateSpec
    21  }
    22  
    23  func init() {
    24  	cli.Register("vlcm.depot.offline.create", &create{})
    25  }
    26  
    27  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    28  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    29  	cmd.ClientFlag.Register(ctx, f)
    30  
    31  	f.StringVar(&cmd.spec.Location, "l", "", "The URL to the depot contents. Only applicable when source-type is PULL")
    32  	f.StringVar(&cmd.spec.Description, "d", "", "An optional description")
    33  	f.StringVar(&cmd.spec.OwnerData, "owner-data", "", "Optional data about the depot's owner")
    34  	f.StringVar(&cmd.spec.FileId, "file-id", "", "File identifier. Only used when source-type is PUSH")
    35  	f.StringVar(&cmd.spec.SourceType, "source-type", string(depots.SourceTypePull), "The depot source type. The default is PULL")
    36  }
    37  
    38  func (cmd *create) Process(ctx context.Context) error {
    39  	return cmd.ClientFlag.Process(ctx)
    40  }
    41  
    42  func (cmd *create) Usage() string {
    43  	return "VLCM"
    44  }
    45  
    46  func (cmd *create) Description() string {
    47  	return `Creates an offline image depot.
    48  
    49  Execution will block the terminal for the duration of the task.
    50  
    51  Examples:
    52    govc vlcm.depot.offline.create -l=<https://your.server.com/filepath>
    53    govc vlcm.depot.offline.create -l=<https://your.server.com/filepath> -source-type=PULL
    54    govc vlcm.depot.offline.create -file-id=<your file identifier> -source-type=PUSH
    55    govc vlcm.depot.offline.create -l=<https://your.server.com/filepath> -source-type=PULL -d="This is a depot used for testing" -owner-data="After all, why not? Why shouldn't I keep it?"`
    56  }
    57  
    58  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    59  	rc, err := cmd.RestClient()
    60  
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	dm := depots.NewManager(rc)
    66  
    67  	if taskId, err := dm.CreateOfflineDepot(cmd.spec); err != nil {
    68  		return err
    69  	} else if _, err = tasks.NewManager(rc).WaitForCompletion(ctx, taskId); err != nil {
    70  		return err
    71  	} else {
    72  		return nil
    73  	}
    74  }