github.com/vmware/govmomi@v0.37.2/govc/cluster/draft/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 draft
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"io"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/vapi/esx/settings/clusters"
    27  )
    28  
    29  type createResult string
    30  
    31  func (r createResult) Write(w io.Writer) error {
    32  	return nil
    33  }
    34  
    35  type create struct {
    36  	*flags.ClientFlag
    37  	*flags.OutputFlag
    38  
    39  	clusterId string
    40  }
    41  
    42  func init() {
    43  	cli.Register("cluster.draft.create", &create{})
    44  }
    45  
    46  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    47  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    48  	cmd.ClientFlag.Register(ctx, f)
    49  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    50  
    51  	f.StringVar(&cmd.clusterId, "cluster-id", "", "The identifier of the cluster.")
    52  }
    53  
    54  func (cmd *create) Process(ctx context.Context) error {
    55  	return cmd.ClientFlag.Process(ctx)
    56  }
    57  
    58  func (cmd *create) Usage() string {
    59  	return "CLUSTER"
    60  }
    61  
    62  func (cmd *create) Description() string {
    63  	return `Creates a new software draft.
    64  
    65  There can be only one active draft at a time on every cluster.
    66  
    67  Examples:
    68    govc cluster.draft.create -cluster-id=domain-c21`
    69  }
    70  
    71  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    72  	rc, err := cmd.RestClient()
    73  
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	dm := clusters.NewManager(rc)
    79  
    80  	if !cmd.All() {
    81  		cmd.JSON = true
    82  	}
    83  
    84  	if draftId, err := dm.CreateSoftwareDraft(cmd.clusterId); err != nil {
    85  		return err
    86  	} else if err := cmd.WriteResult(createResult(draftId)); err != nil {
    87  		return err
    88  	} else {
    89  		return nil
    90  	}
    91  }