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