github.com/fafucoder/cilium@v1.6.11/cilium/cmd/identity_get.go (about)

     1  // Copyright 2017-2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"text/tabwriter"
    21  
    22  	identityApi "github.com/cilium/cilium/api/v1/client/policy"
    23  	"github.com/cilium/cilium/api/v1/models"
    24  	"github.com/cilium/cilium/pkg/api"
    25  	"github.com/cilium/cilium/pkg/command"
    26  	"github.com/cilium/cilium/pkg/labels"
    27  
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  var (
    32  	lookupLabels []string
    33  )
    34  
    35  func printIdentities(identities []*models.Identity) {
    36  	if command.OutputJSON() {
    37  		if err := command.PrintOutput(identities); err != nil {
    38  			Fatalf("Unable to provide JSON output: %s", err)
    39  		}
    40  	} else {
    41  		w := tabwriter.NewWriter(os.Stdout, 2, 0, 3, ' ', 0)
    42  
    43  		fmt.Fprintf(w, "ID\tLABELS\n")
    44  		for _, identity := range identities {
    45  			lbls := labels.NewLabelsFromModel(identity.Labels)
    46  			first := true
    47  			for _, lbl := range lbls.GetPrintableModel() {
    48  				if first {
    49  					fmt.Fprintf(w, "%d\t%s\n", identity.ID, lbl)
    50  					first = false
    51  				} else {
    52  					fmt.Fprintf(w, "\t%s\n", lbl)
    53  				}
    54  			}
    55  		}
    56  
    57  		w.Flush()
    58  	}
    59  }
    60  
    61  // identityGetCmd represents the identity_get command
    62  var identityGetCmd = &cobra.Command{
    63  	Use:   "get",
    64  	Short: "Retrieve information about an identity",
    65  	Run: func(cmd *cobra.Command, args []string) {
    66  		if len(lookupLabels) > 0 {
    67  			params := identityApi.NewGetIdentityParams().WithLabels(lookupLabels).WithTimeout(api.ClientTimeout)
    68  			if id, err := client.Policy.GetIdentity(params); err != nil {
    69  				Fatalf("Cannot get identity for labels %s: %s\n", lookupLabels, err)
    70  			} else {
    71  				printIdentities(id.Payload)
    72  			}
    73  		} else {
    74  			if len(args) < 1 || args[0] == "" {
    75  				Usagef(cmd, "Invalid identity ID")
    76  			}
    77  
    78  			params := identityApi.NewGetIdentityIDParams().WithID(args[0]).WithTimeout(api.ClientTimeout)
    79  			if id, err := client.Policy.GetIdentityID(params); err != nil {
    80  				Fatalf("Cannot get identity for given ID %s: %s\n", id, err)
    81  			} else {
    82  				printIdentities([]*models.Identity{id.Payload})
    83  			}
    84  		}
    85  	},
    86  }
    87  
    88  func init() {
    89  	identityCmd.AddCommand(identityGetCmd)
    90  	identityGetCmd.Flags().StringSliceVar(&lookupLabels, "label", []string{}, "Label to lookup")
    91  	command.AddJSONOutput(identityGetCmd)
    92  }