github.com/vmware/govmomi@v0.43.0/govc/host/add.go (about) 1 /* 2 Copyright (c) 2015-2016 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 host 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 add struct { 32 *flags.FolderFlag 33 *flags.HostConnectFlag 34 35 connect bool 36 } 37 38 func init() { 39 cli.Register("host.add", &add{}) 40 } 41 42 func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) { 43 cmd.FolderFlag, ctx = flags.NewFolderFlag(ctx) 44 cmd.FolderFlag.Register(ctx, f) 45 46 cmd.HostConnectFlag, ctx = flags.NewHostConnectFlag(ctx) 47 cmd.HostConnectFlag.Register(ctx, f) 48 49 f.BoolVar(&cmd.connect, "connect", true, "Immediately connect to host") 50 } 51 52 func (cmd *add) Process(ctx context.Context) error { 53 if err := cmd.FolderFlag.Process(ctx); err != nil { 54 return err 55 } 56 if err := cmd.HostConnectFlag.Process(ctx); err != nil { 57 return err 58 } 59 if cmd.HostName == "" { 60 return flag.ErrHelp 61 } 62 if cmd.UserName == "" { 63 return flag.ErrHelp 64 } 65 if cmd.Password == "" { 66 return flag.ErrHelp 67 } 68 return nil 69 } 70 71 func (cmd *add) Description() string { 72 return `Add host to datacenter. 73 74 The host is added to the folder specified by the 'folder' flag. If not given, 75 this defaults to the host folder in the specified or default datacenter. 76 77 Examples: 78 thumbprint=$(govc about.cert -k -u host.example.com -thumbprint | awk '{print $2}') 79 govc host.add -hostname host.example.com -username root -password pass -thumbprint $thumbprint 80 govc host.add -hostname 10.0.6.1 -username root -password pass -noverify` 81 } 82 83 func (cmd *add) Add(ctx context.Context, parent *object.Folder) error { 84 spec := cmd.Spec(parent.Client()) 85 86 req := types.AddStandaloneHost_Task{ 87 This: parent.Reference(), 88 Spec: spec, 89 AddConnected: cmd.connect, 90 } 91 92 res, err := methods.AddStandaloneHost_Task(ctx, parent.Client(), &req) 93 if err != nil { 94 return err 95 } 96 97 logger := cmd.ProgressLogger(fmt.Sprintf("adding %s to folder %s... ", spec.HostName, parent.InventoryPath)) 98 defer logger.Wait() 99 100 task := object.NewTask(parent.Client(), res.Returnval) 101 _, err = task.WaitForResult(ctx, logger) 102 return err 103 } 104 105 func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error { 106 if f.NArg() != 0 { 107 return flag.ErrHelp 108 } 109 110 folder, err := cmd.FolderOrDefault("host") 111 if err != nil { 112 return err 113 } 114 115 return cmd.Fault(cmd.Add(ctx, folder)) 116 }