github.com/vmware/govmomi@v0.37.2/govc/namespace/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 namespace
    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  	libraries flags.StringList
    32  	vmClasses flags.StringList
    33  	spec      namespace.NamespacesInstanceCreateSpec
    34  }
    35  
    36  func init() {
    37  	cli.Register("namespace.create", &create{})
    38  }
    39  
    40  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    42  	cmd.ClientFlag.Register(ctx, f)
    43  
    44  	f.StringVar(&cmd.spec.Cluster, "supervisor", "", "The identifier of the Supervisor.")
    45  	f.Var(&cmd.libraries, "library", "Content library IDs to associate with the vSphere Namespace.")
    46  	f.Var(&cmd.vmClasses, "vm-class", "Virtual machine class IDs to associate with the vSphere Namespace.")
    47  }
    48  
    49  func (*create) Usage() string {
    50  	return "NAME"
    51  }
    52  
    53  func (cmd *create) Process(ctx context.Context) error {
    54  	cmd.spec.VmServiceSpec.ContentLibraries = cmd.libraries
    55  	cmd.spec.VmServiceSpec.VmClasses = cmd.vmClasses
    56  
    57  	return cmd.ClientFlag.Process(ctx)
    58  }
    59  
    60  func (cmd *create) Description() string {
    61  	return `Creates a new vSphere Namespace on a Supervisor.
    62  
    63  The '-library' and '-vm-class' flags can each be specified multiple times.
    64  
    65  Examples:
    66    govc namespace.create -supervisor=domain-c1 test-namespace
    67    govc namespace.create -supervisor=domain-c1 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 test-namespace
    68    govc namespace.create -supervisor=domain-c1 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 test-namespace
    69    govc namespace.create -supervisor=domain-c1 -vm-class=best-effort-2xlarge test-namespace
    70    govc namespace.create -supervisor=domain-c1 -vm-class=best-effort-2xlarge -vm-class best-effort-4xlarge test-namespace
    71    govc namespace.create -supervisor=domain-c1 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -vm-class=best-effort-2xlarge -vm-class=best-effort-4xlarge test-namespace`
    72  }
    73  
    74  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    75  	cmd.spec.Namespace = f.Arg(0)
    76  	if f.NArg() != 1 {
    77  		return flag.ErrHelp
    78  	}
    79  
    80  	rc, err := cmd.RestClient()
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	nm := namespace.NewManager(rc)
    86  
    87  	return nm.CreateNamespace(ctx, cmd.spec)
    88  }