github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/session_info.go (about)

     1  package govcd
     2  
     3  /*
     4   * Copyright 2021 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"strings"
    11  
    12  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    13  	"github.com/vmware/go-vcloud-director/v2/util"
    14  )
    15  
    16  // ExtendedSessionInfo collects data regarding a VCD connection
    17  type ExtendedSessionInfo struct {
    18  	User           string
    19  	Org            string
    20  	Roles          []string
    21  	Rights         []string
    22  	Version        string
    23  	ConnectionType string
    24  }
    25  
    26  // GetSessionInfo collects the basic session information for a VCD connection
    27  func (client *Client) GetSessionInfo() (*types.CurrentSessionInfo, error) {
    28  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointSessionCurrent
    29  
    30  	// We get the maximum supported version, as early versions of the API return less data
    31  	apiVersion, err := client.MaxSupportedVersion()
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	urlRef, err := client.OpenApiBuildEndpoint(endpoint)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	var info types.CurrentSessionInfo
    42  
    43  	err = client.OpenApiGetItem(apiVersion, urlRef, nil, &info, nil)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return &info, nil
    49  }
    50  
    51  // GetExtendedSessionInfo collects extended session information for support and debugging
    52  // It will try to collect as much data as possible, failing only if the minimum data can't
    53  // be collected.
    54  func (vcdClient *VCDClient) GetExtendedSessionInfo() (*ExtendedSessionInfo, error) {
    55  	var extendedSessionInfo ExtendedSessionInfo
    56  	sessionInfo, err := vcdClient.Client.GetSessionInfo()
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	switch {
    61  	case vcdClient.Client.UsingBearerToken:
    62  		extendedSessionInfo.ConnectionType = "Bearer token"
    63  	case vcdClient.Client.UsingAccessToken:
    64  		extendedSessionInfo.ConnectionType = "API Access token"
    65  	default:
    66  		extendedSessionInfo.ConnectionType = "Username + password"
    67  	}
    68  	version, err := vcdClient.Client.GetVcdFullVersion()
    69  	if err == nil {
    70  		extendedSessionInfo.Version = version.Version.String()
    71  	}
    72  	if sessionInfo.User.Name == "" {
    73  		return nil, fmt.Errorf("no user reference found")
    74  	}
    75  	extendedSessionInfo.User = sessionInfo.User.Name
    76  
    77  	if sessionInfo.Org.Name == "" {
    78  		return nil, fmt.Errorf("no Org reference found")
    79  	}
    80  	extendedSessionInfo.Org = sessionInfo.Org.Name
    81  
    82  	if len(sessionInfo.Roles) == 0 {
    83  		return &extendedSessionInfo, nil
    84  	}
    85  	extendedSessionInfo.Roles = append(extendedSessionInfo.Roles, sessionInfo.Roles...)
    86  	org, err := vcdClient.GetAdminOrgById(sessionInfo.Org.ID)
    87  	if err != nil {
    88  		return &extendedSessionInfo, err
    89  	}
    90  	for _, roleRef := range sessionInfo.RoleRefs {
    91  		role, err := org.GetRoleById(roleRef.ID)
    92  		if err != nil {
    93  			continue
    94  		}
    95  		rights, err := role.GetRights(nil)
    96  		if err != nil {
    97  			continue
    98  		}
    99  		for _, right := range rights {
   100  			extendedSessionInfo.Rights = append(extendedSessionInfo.Rights, right.Name)
   101  		}
   102  	}
   103  	return &extendedSessionInfo, nil
   104  }
   105  
   106  // LogSessionInfo prints session information into the default logs
   107  func (client *VCDClient) LogSessionInfo() {
   108  
   109  	// If logging is disabled, there is no point in collecting session info
   110  	if util.EnableLogging {
   111  		info, err := client.GetExtendedSessionInfo()
   112  		if err != nil {
   113  			util.Logger.Printf("no session info collected: %s\n", err)
   114  			return
   115  		}
   116  		text, err := json.MarshalIndent(info, " ", " ")
   117  		if err != nil {
   118  			util.Logger.Printf("error formatting session info %s\n", err)
   119  			return
   120  		}
   121  		util.Logger.Println(strings.Repeat("*", 80))
   122  		util.Logger.Println("START SESSION INFO")
   123  		util.Logger.Println(strings.Repeat("*", 80))
   124  		util.Logger.Printf("%s\n", text)
   125  		util.Logger.Println(strings.Repeat("*", 80))
   126  		util.Logger.Println("END SESSION INFO")
   127  		util.Logger.Println(strings.Repeat("*", 80))
   128  	}
   129  }