github.com/vmware/govmomi@v0.37.1/govc/license/assign.go (about) 1 /* 2 Copyright (c) 2015 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 license 18 19 import ( 20 "context" 21 "flag" 22 23 "github.com/vmware/govmomi/govc/cli" 24 "github.com/vmware/govmomi/govc/flags" 25 "github.com/vmware/govmomi/license" 26 "github.com/vmware/govmomi/vim25/types" 27 ) 28 29 type assign struct { 30 *flags.ClientFlag 31 *flags.OutputFlag 32 *flags.HostSystemFlag 33 *flags.ClusterFlag 34 35 name string 36 remove bool 37 } 38 39 func init() { 40 cli.Register("license.assign", &assign{}) 41 } 42 43 func (cmd *assign) Register(ctx context.Context, f *flag.FlagSet) { 44 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 45 cmd.ClientFlag.Register(ctx, f) 46 47 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 48 cmd.OutputFlag.Register(ctx, f) 49 50 cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) 51 cmd.HostSystemFlag.Register(ctx, f) 52 53 cmd.ClusterFlag, ctx = flags.NewClusterFlag(ctx) 54 cmd.ClusterFlag.Register(ctx, f) 55 56 f.StringVar(&cmd.name, "name", "", "Display name") 57 f.BoolVar(&cmd.remove, "remove", false, "Remove assignment") 58 } 59 60 func (cmd *assign) Process(ctx context.Context) error { 61 if err := cmd.ClientFlag.Process(ctx); err != nil { 62 return err 63 } 64 if err := cmd.OutputFlag.Process(ctx); err != nil { 65 return err 66 } 67 if err := cmd.HostSystemFlag.Process(ctx); err != nil { 68 return err 69 } 70 return cmd.ClusterFlag.Process(ctx) 71 } 72 73 func (cmd *assign) Usage() string { 74 return "KEY" 75 } 76 77 func (cmd *assign) Description() string { 78 return `Assign licenses to HOST or CLUSTER. 79 80 Examples: 81 govc license.assign $VCSA_LICENSE_KEY 82 govc license.assign -host a_host.example.com $ESX_LICENSE_KEY 83 govc license.assign -cluster a_cluster $VSAN_LICENSE_KEY` 84 } 85 86 func (cmd *assign) Run(ctx context.Context, f *flag.FlagSet) error { 87 if f.NArg() != 1 { 88 return flag.ErrHelp 89 } 90 91 key := f.Arg(0) 92 93 client, err := cmd.Client() 94 if err != nil { 95 return err 96 } 97 98 m, err := license.NewManager(client).AssignmentManager(ctx) 99 if err != nil { 100 return err 101 } 102 103 host, err := cmd.HostSystemIfSpecified() 104 if err != nil { 105 return err 106 } 107 108 var id string 109 110 if host == nil { 111 cluster, cerr := cmd.ClusterIfSpecified() 112 if cerr != nil { 113 return cerr 114 } 115 if cluster == nil { 116 // Default to vCenter UUID 117 id = client.ServiceContent.About.InstanceUuid 118 } else { 119 id = cluster.Reference().Value 120 } 121 } else { 122 id = host.Reference().Value 123 } 124 125 if cmd.remove { 126 return m.Remove(ctx, id) 127 } 128 129 info, err := m.Update(ctx, id, key, cmd.name) 130 if err != nil { 131 return err 132 } 133 134 return cmd.WriteResult(licenseOutput([]types.LicenseManagerLicenseInfo{*info})) 135 }