Skip to content

Commit

Permalink
[v13] show discovered name in non-verbose request search
Browse files Browse the repository at this point in the history
backports #30196 to branch/v13.

* factor out discovered name func from tsh and tctl into tool/common
  • Loading branch information
GavinFrazar committed Sep 16, 2023
1 parent 46a54d4 commit 02e0da0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 41 deletions.
16 changes: 16 additions & 0 deletions tool/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,19 @@ func FormatLabels(labels map[string]string, verbose bool) string {
namespaced = append(namespaced, teleportNamespaced...)
return strings.Join(append(result, namespaced...), ",")
}

// FormatResourceName returns the resource's name or its name as originally
// discovered in the cloud by the Teleport Discovery Service.
// In verbose mode, it always returns the resource name.
// In non-verbose mode, if the resource came from discovery and has the
// discovered name label, it returns the discovered name.
func FormatResourceName(r types.ResourceWithLabels, verbose bool) string {
if !verbose {
// return the (shorter) discovered name in non-verbose mode.
discoveredName, ok := r.GetAllLabels()[types.DiscoveredNameLabel]
if ok && discoveredName != "" {
return discoveredName
}
}
return r.GetName()
}
23 changes: 4 additions & 19 deletions tool/tctl/common/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ func (c *databaseServerCollection) writeText(w io.Writer, verbose bool) error {
labels := common.FormatLabels(server.GetDatabase().GetAllLabels(), verbose)
rows = append(rows, []string{
server.GetHostname(),
nameOrDiscoveredName(server.GetDatabase(), verbose),
common.FormatResourceName(server.GetDatabase(), verbose),
server.GetDatabase().GetProtocol(),
server.GetDatabase().GetURI(),
labels,
Expand Down Expand Up @@ -732,7 +732,7 @@ func (c *databaseCollection) writeText(w io.Writer, verbose bool) error {
for _, database := range c.databases {
labels := common.FormatLabels(database.GetAllLabels(), verbose)
rows = append(rows, []string{
nameOrDiscoveredName(database, verbose),
common.FormatResourceName(database, verbose),
database.GetProtocol(),
database.GetURI(),
labels,
Expand Down Expand Up @@ -877,7 +877,7 @@ func (c *kubeServerCollection) writeText(w io.Writer, verbose bool) error {
}
labels := common.FormatLabels(kube.GetAllLabels(), verbose)
rows = append(rows, []string{
nameOrDiscoveredName(kube, verbose),
common.FormatResourceName(kube, verbose),
labels,
server.GetTeleportVersion(),
})
Expand Down Expand Up @@ -929,7 +929,7 @@ func (c *kubeClusterCollection) writeText(w io.Writer, verbose bool) error {
for _, cluster := range c.clusters {
labels := common.FormatLabels(cluster.GetAllLabels(), verbose)
rows = append(rows, []string{
nameOrDiscoveredName(cluster, verbose),
common.FormatResourceName(cluster, verbose),
labels,
})
}
Expand Down Expand Up @@ -1192,18 +1192,3 @@ func (c *userGroupCollection) writeText(w io.Writer, verbose bool) error {
_, err := t.AsBuffer().WriteTo(w)
return trace.Wrap(err)
}

// nameOrDiscoveredName returns the resource's name or its name as originally
// discovered in the cloud by the Teleport Discovery Service.
// In verbose mode, it always returns the resource name.
// In non-verbose mode, if the resource came from discovery and has the
// discovered name label, it returns the discovered name.
func nameOrDiscoveredName(r types.ResourceWithLabels, verbose bool) string {
if !verbose {
originalName, ok := r.GetAllLabels()[types.DiscoveredNameLabel]
if ok && originalName != "" {
return originalName
}
}
return r.GetName()
}
4 changes: 2 additions & 2 deletions tool/tsh/access_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func onRequestSearch(cf *CLIConf) error {
resourceIDs = append(resourceIDs, resourceID)

row = []string{
resource.GetName(),
common.FormatResourceName(resource, cf.Verbose),
r.Spec.Namespace,
common.FormatLabels(resource.GetAllLabels(), cf.Verbose),
resourceID,
Expand All @@ -494,7 +494,7 @@ func onRequestSearch(cf *CLIConf) error {
hostName = r.GetHostname()
}
row = []string{
resource.GetName(),
common.FormatResourceName(resource, cf.Verbose),
hostName,
common.FormatLabels(resource.GetAllLabels(), cf.Verbose),
resourceID,
Expand Down
8 changes: 2 additions & 6 deletions tool/tsh/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,13 +1008,9 @@ func getKubeClusterTextRow(kc types.KubeCluster, selectedCluster string, verbose
if selectedCluster != "" && kc.GetName() == selectedCluster {
selectedMark = "*"
}
printName := kc.GetName()
if d, ok := getDiscoveredName(kc); ok && !verbose {
// print the (shorter) discovered name in non-verbose mode.
printName = d
}
displayName := common.FormatResourceName(kc, verbose)
labels := common.FormatLabels(kc.GetAllLabels(), verbose)
row = append(row, printName, labels, selectedMark)
row = append(row, displayName, labels, selectedMark)
return row
}

Expand Down
20 changes: 6 additions & 14 deletions tool/tsh/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2800,23 +2800,15 @@ func formatUsersForDB(database types.Database, accessChecker services.AccessChec
return fmt.Sprintf("%v, except: %v", dbUsers.Allowed, dbUsers.Denied)
}

func getDiscoveredName(r types.ResourceWithLabels) (string, bool) {
name, ok := r.GetAllLabels()[types.DiscoveredNameLabel]
return name, ok
}

func getDatabaseRow(proxy, cluster, clusterFlag string, database types.Database, active []tlsca.RouteToDatabase, accessChecker services.AccessChecker, verbose bool) []string {
name := database.GetName()
printName := name
if d, ok := getDiscoveredName(database); ok && !verbose && d != name {
printName = d
}
displayName := common.FormatResourceName(database, verbose)
var connect string
for _, a := range active {
if a.ServiceName == name {
a.ServiceName = printName
// format the db name with the print name
printName = formatActiveDB(a)
a.ServiceName = displayName
// format the db name with the display name
displayName = formatActiveDB(a)
// then revert it for connect string
a.ServiceName = name
switch a.Protocol {
Expand All @@ -2838,7 +2830,7 @@ func getDatabaseRow(proxy, cluster, clusterFlag string, database types.Database,
labels := common.FormatLabels(database.GetAllLabels(), verbose)
if verbose {
row = append(row,
printName,
displayName,
database.GetDescription(),
database.GetProtocol(),
database.GetType(),
Expand All @@ -2849,7 +2841,7 @@ func getDatabaseRow(proxy, cluster, clusterFlag string, database types.Database,
)
} else {
row = append(row,
printName,
displayName,
database.GetDescription(),
formatUsersForDB(database, accessChecker),
labels,
Expand Down

0 comments on commit 02e0da0

Please sign in to comment.