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