github.com/vmware/govmomi@v0.51.0/cli/license/add.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package license
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  
    12  	"github.com/vmware/govmomi/cli"
    13  	"github.com/vmware/govmomi/cli/flags"
    14  	"github.com/vmware/govmomi/license"
    15  	"github.com/vmware/govmomi/vim25/types"
    16  )
    17  
    18  type add struct {
    19  	*flags.ClientFlag
    20  	*flags.OutputFlag
    21  }
    22  
    23  func init() {
    24  	cli.Register("license.add", &add{})
    25  }
    26  
    27  func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
    28  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    29  	cmd.ClientFlag.Register(ctx, f)
    30  
    31  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    32  	cmd.OutputFlag.Register(ctx, f)
    33  }
    34  
    35  func (cmd *add) Process(ctx context.Context) error {
    36  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    37  		return err
    38  	}
    39  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    40  		return err
    41  	}
    42  	return nil
    43  }
    44  
    45  func (cmd *add) Usage() string {
    46  	return "KEY..."
    47  }
    48  
    49  func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
    50  	client, err := cmd.Client()
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	m := license.NewManager(client)
    56  
    57  	// From the vSphere 5.5 documentation:
    58  	//
    59  	//     To specify the edition type and any optional functions, use
    60  	//     updateLicense for ESX Server and addLicense follow by
    61  	//     LicenseAssingmentManager.updateAssignedLicense for VirtualCenter.
    62  	//
    63  	var addFunc func(ctx context.Context, key string, labels map[string]string) (types.LicenseManagerLicenseInfo, error)
    64  	switch t := client.ServiceContent.About.ApiType; t {
    65  	case "HostAgent":
    66  		addFunc = m.Update
    67  	case "VirtualCenter":
    68  		addFunc = m.Add
    69  	default:
    70  		return fmt.Errorf("unsupported ApiType: %s", t)
    71  	}
    72  
    73  	result := make(licenseOutput, 0)
    74  	for _, v := range f.Args() {
    75  		license, err := addFunc(ctx, v, nil)
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		result = append(result, license)
    81  	}
    82  
    83  	return cmd.WriteResult(licenseOutput(result))
    84  }