github.com/vmware/govmomi@v0.51.0/cli/library/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 library
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  
    12  	"github.com/vmware/govmomi/cli"
    13  	"github.com/vmware/govmomi/cli/flags"
    14  	"github.com/vmware/govmomi/vapi/library"
    15  )
    16  
    17  type create struct {
    18  	*flags.DatastoreFlag
    19  	library library.Library
    20  	sub     library.Subscription
    21  	pub     library.Publication
    22  }
    23  
    24  func init() {
    25  	cli.Register("library.create", &create{})
    26  }
    27  
    28  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    29  	cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx)
    30  	cmd.DatastoreFlag.Register(ctx, f)
    31  
    32  	cmd.sub.AutomaticSyncEnabled = new(bool)
    33  	cmd.sub.OnDemand = new(bool)
    34  
    35  	f.Var(flags.NewOptionalString(&cmd.library.Description), "d", "Description of library")
    36  	f.StringVar(&cmd.sub.SubscriptionURL, "sub", "", "Subscribe to library URL")
    37  	f.StringVar(&cmd.sub.UserName, "sub-username", "", "Subscription username")
    38  	f.StringVar(&cmd.sub.Password, "sub-password", "", "Subscription password")
    39  	f.StringVar(&cmd.sub.SslThumbprint, "thumbprint", "", "SHA-1 thumbprint of the host's SSL certificate")
    40  	f.BoolVar(cmd.sub.AutomaticSyncEnabled, "sub-autosync", true, "Automatic synchronization")
    41  	f.BoolVar(cmd.sub.OnDemand, "sub-ondemand", false, "Download content on demand")
    42  	f.Var(flags.NewOptionalBool(&cmd.pub.Published), "pub", "Publish library")
    43  	f.StringVar(&cmd.pub.UserName, "pub-username", "", "Publication username")
    44  	f.StringVar(&cmd.pub.Password, "pub-password", "", "Publication password")
    45  	f.StringVar(&cmd.library.SecurityPolicyID, "policy", "", "Security Policy ID")
    46  }
    47  
    48  func (cmd *create) Usage() string {
    49  	return "NAME"
    50  }
    51  
    52  func (cmd *create) Description() string {
    53  	return `Create library.
    54  
    55  Examples:
    56    govc library.create library_name
    57    govc library.create -sub http://server/path/lib.json library_name
    58    govc library.create -pub library_name`
    59  }
    60  
    61  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    62  	if f.NArg() != 1 {
    63  		return flag.ErrHelp
    64  	}
    65  
    66  	ds, err := cmd.Datastore()
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	cmd.library.Name = f.Arg(0)
    72  	cmd.library.Type = "LOCAL"
    73  	cmd.library.Storage = []library.StorageBacking{
    74  		{
    75  			DatastoreID: ds.Reference().Value,
    76  			Type:        "DATASTORE",
    77  		},
    78  	}
    79  
    80  	if cmd.sub.SubscriptionURL != "" {
    81  		cmd.library.Subscription = &cmd.sub
    82  		cmd.library.Type = "SUBSCRIBED"
    83  		cmd.sub.AuthenticationMethod = "NONE"
    84  		if cmd.sub.Password != "" {
    85  			cmd.sub.AuthenticationMethod = "BASIC"
    86  		}
    87  	}
    88  
    89  	if cmd.pub.Published != nil && *cmd.pub.Published {
    90  		cmd.library.Publication = &cmd.pub
    91  		cmd.pub.AuthenticationMethod = "NONE"
    92  		if cmd.pub.Password != "" {
    93  			cmd.sub.AuthenticationMethod = "BASIC"
    94  		}
    95  	}
    96  
    97  	c, err := cmd.RestClient()
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	id, err := library.NewManager(c).CreateLibrary(ctx, cmd.library)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	fmt.Println(id)
   108  	return nil
   109  }