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

     1  /*
     2  Copyright (c) 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  	"io"
    24  	"os"
    25  	"text/tabwriter"
    26  
    27  	"github.com/vmware/govmomi/govc/cli"
    28  	"github.com/vmware/govmomi/govc/flags"
    29  	"github.com/vmware/govmomi/license"
    30  	"github.com/vmware/govmomi/vim25/types"
    31  )
    32  
    33  type assigned struct {
    34  	*flags.ClientFlag
    35  	*flags.OutputFlag
    36  
    37  	id string
    38  }
    39  
    40  func init() {
    41  	cli.Register("license.assigned.ls", &assigned{})
    42  }
    43  
    44  func (cmd *assigned) Register(ctx context.Context, f *flag.FlagSet) {
    45  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    46  	cmd.ClientFlag.Register(ctx, f)
    47  
    48  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    49  	cmd.OutputFlag.Register(ctx, f)
    50  
    51  	f.StringVar(&cmd.id, "id", "", "Entity ID")
    52  }
    53  
    54  func (cmd *assigned) Process(ctx context.Context) error {
    55  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    56  		return err
    57  	}
    58  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    59  		return err
    60  	}
    61  	return nil
    62  }
    63  
    64  func (cmd *assigned) Run(ctx context.Context, f *flag.FlagSet) error {
    65  	client, err := cmd.Client()
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	m, err := license.NewManager(client).AssignmentManager(ctx)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	assigned, err := m.QueryAssigned(ctx, cmd.id)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	return cmd.WriteResult(assignedOutput(assigned))
    81  }
    82  
    83  type assignedOutput []types.LicenseAssignmentManagerLicenseAssignment
    84  
    85  func (res assignedOutput) Write(w io.Writer) error {
    86  	tw := tabwriter.NewWriter(os.Stdout, 4, 0, 2, ' ', 0)
    87  	fmt.Fprintf(tw, "Id:\tScope:\tName:\tLicense:\n")
    88  	for _, v := range res {
    89  		fmt.Fprintf(tw, "%s\t", v.EntityId)
    90  		fmt.Fprintf(tw, "%s\t", v.Scope)
    91  		fmt.Fprintf(tw, "%s\t", v.EntityDisplayName)
    92  		fmt.Fprintf(tw, "%s\t", v.AssignedLicense.LicenseKey)
    93  		fmt.Fprintf(tw, "\n")
    94  	}
    95  	return tw.Flush()
    96  }