github.com/vmware/govmomi@v0.37.2/govc/license/add.go (about)

     1  /*
     2  Copyright (c) 2014-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  	"fmt"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/license"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  type add struct {
    31  	*flags.ClientFlag
    32  	*flags.OutputFlag
    33  }
    34  
    35  func init() {
    36  	cli.Register("license.add", &add{})
    37  }
    38  
    39  func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    41  	cmd.ClientFlag.Register(ctx, f)
    42  
    43  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    44  	cmd.OutputFlag.Register(ctx, f)
    45  }
    46  
    47  func (cmd *add) Process(ctx context.Context) error {
    48  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    49  		return err
    50  	}
    51  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    52  		return err
    53  	}
    54  	return nil
    55  }
    56  
    57  func (cmd *add) Usage() string {
    58  	return "KEY..."
    59  }
    60  
    61  func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
    62  	client, err := cmd.Client()
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	m := license.NewManager(client)
    68  
    69  	// From the vSphere 5.5 documentation:
    70  	//
    71  	//     To specify the edition type and any optional functions, use
    72  	//     updateLicense for ESX Server and addLicense follow by
    73  	//     LicenseAssingmentManager.updateAssignedLicense for VirtualCenter.
    74  	//
    75  	var addFunc func(ctx context.Context, key string, labels map[string]string) (types.LicenseManagerLicenseInfo, error)
    76  	switch t := client.ServiceContent.About.ApiType; t {
    77  	case "HostAgent":
    78  		addFunc = m.Update
    79  	case "VirtualCenter":
    80  		addFunc = m.Add
    81  	default:
    82  		return fmt.Errorf("unsupported ApiType: %s", t)
    83  	}
    84  
    85  	result := make(licenseOutput, 0)
    86  	for _, v := range f.Args() {
    87  		license, err := addFunc(ctx, v, nil)
    88  		if err != nil {
    89  			return err
    90  		}
    91  
    92  		result = append(result, license)
    93  	}
    94  
    95  	return cmd.WriteResult(licenseOutput(result))
    96  }