github.com/vmware/govmomi@v0.37.2/govc/namespace/vmclass/create.go (about)

     1  /*
     2  Copyright (c) 2024-2024 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 vmclass
    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/vapi/namespace"
    26  )
    27  
    28  type create struct {
    29  	*flags.ClientFlag
    30  
    31  	spec namespace.VirtualMachineClassCreateSpec
    32  }
    33  
    34  func init() {
    35  	cli.Register("namespace.vmclass.create", &create{})
    36  }
    37  
    38  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    39  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    40  	cmd.ClientFlag.Register(ctx, f)
    41  
    42  	f.Int64Var(&cmd.spec.CpuCount, "cpus", 0, "The number of CPUs.")
    43  	f.Int64Var(&cmd.spec.MemoryMb, "memory", 0, "The amount of memory (in MB).")
    44  }
    45  
    46  func (*create) Usage() string {
    47  	return "NAME"
    48  }
    49  
    50  func (cmd *create) Description() string {
    51  	return `Creates a new virtual machine class.
    52  
    53  The name of the virtual machine class has DNS_LABEL restrictions
    54  as specified in "https://tools.ietf.org/html/rfc1123". It
    55  must be an alphanumeric (a-z and 0-9) string and with maximum length
    56  of 63 characters and with the '-' character allowed anywhere except
    57  the first or last character. This name is unique in this vCenter server.
    58  
    59  Examples:
    60    govc namespace.vmclass.create -cpus=8 -memory=8192 test-class-01`
    61  }
    62  
    63  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    64  	cmd.spec.Id = f.Arg(0)
    65  	if f.NArg() != 1 {
    66  		return flag.ErrHelp
    67  	}
    68  
    69  	rc, err := cmd.RestClient()
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	nm := namespace.NewManager(rc)
    75  
    76  	return nm.CreateVmClass(ctx, cmd.spec)
    77  }