diff --git a/lib/teleterm/apiserver/handler/handler.go b/lib/teleterm/apiserver/handler/handler.go index c6a2efdc16043..8225992d78ba9 100644 --- a/lib/teleterm/apiserver/handler/handler.go +++ b/lib/teleterm/apiserver/handler/handler.go @@ -23,6 +23,7 @@ import ( api "github.com/gravitational/teleport/gen/proto/go/teleport/lib/teleterm/v1" "github.com/gravitational/teleport/lib/teleterm/daemon" + "github.com/gravitational/teleport/lib/ui" ) // New creates an instance of Handler @@ -59,17 +60,13 @@ type Handler struct { Config } -// sortedLabels is a sort wrapper that sorts labels by name -type APILabels []*api.Label - -func (s APILabels) Len() int { - return len(s) -} - -func (s APILabels) Less(i, j int) bool { - return s[i].Name < s[j].Name -} - -func (s APILabels) Swap(i, j int) { - s[i], s[j] = s[j], s[i] +func makeAPILabels(uiLabels []ui.Label) []*api.Label { + apiLabels := make([]*api.Label, 0, len(uiLabels)) + for _, uiLabel := range uiLabels { + apiLabels = append(apiLabels, &api.Label{ + Name: uiLabel.Name, + Value: uiLabel.Value, + }) + } + return apiLabels } diff --git a/lib/teleterm/apiserver/handler/handler_apps.go b/lib/teleterm/apiserver/handler/handler_apps.go index c8eb7ca0e4263..80645291c041a 100644 --- a/lib/teleterm/apiserver/handler/handler_apps.go +++ b/lib/teleterm/apiserver/handler/handler_apps.go @@ -18,13 +18,13 @@ package handler import ( "context" - "sort" "github.com/gravitational/trace" "github.com/gravitational/teleport/api/types" api "github.com/gravitational/teleport/gen/proto/go/teleport/lib/teleterm/v1" "github.com/gravitational/teleport/lib/teleterm/clusters" + "github.com/gravitational/teleport/lib/ui" ) // GetApps gets apps with filters and returns paginated results @@ -77,14 +77,7 @@ func newAPIApp(clusterApp clusters.App) *api.App { }) } - apiLabels := APILabels{} - for name, value := range app.GetAllLabels() { - apiLabels = append(apiLabels, &api.Label{ - Name: name, - Value: value, - }) - } - sort.Sort(apiLabels) + apiLabels := makeAPILabels(ui.MakeLabelsWithoutInternalPrefixes(app.GetAllLabels())) return &api.App{ Uri: clusterApp.URI.String(), @@ -103,14 +96,7 @@ func newAPIApp(clusterApp clusters.App) *api.App { func newSAMLIdPServiceProviderAPIApp(clusterApp clusters.SAMLIdPServiceProvider) *api.App { provider := clusterApp.Provider - apiLabels := APILabels{} - for name, value := range provider.GetAllLabels() { - apiLabels = append(apiLabels, &api.Label{ - Name: name, - Value: value, - }) - } - sort.Sort(apiLabels) + apiLabels := makeAPILabels(ui.MakeLabelsWithoutInternalPrefixes(provider.GetAllLabels())) // Keep in sync with lib/web/ui/app.go. return &api.App{ diff --git a/lib/teleterm/apiserver/handler/handler_databases.go b/lib/teleterm/apiserver/handler/handler_databases.go index 681181b17395b..1fb22011d44c2 100644 --- a/lib/teleterm/apiserver/handler/handler_databases.go +++ b/lib/teleterm/apiserver/handler/handler_databases.go @@ -20,12 +20,12 @@ package handler import ( "context" - "sort" "github.com/gravitational/trace" api "github.com/gravitational/teleport/gen/proto/go/teleport/lib/teleterm/v1" "github.com/gravitational/teleport/lib/teleterm/clusters" + "github.com/gravitational/teleport/lib/ui" ) // GetDatabases gets databases with filters and returns paginated results @@ -84,15 +84,7 @@ func (s *Handler) ListDatabaseUsers(ctx context.Context, req *api.ListDatabaseUs } func newAPIDatabase(db clusters.Database) *api.Database { - apiLabels := APILabels{} - for name, value := range db.GetAllLabels() { - apiLabels = append(apiLabels, &api.Label{ - Name: name, - Value: value, - }) - } - - sort.Sort(apiLabels) + apiLabels := makeAPILabels(ui.MakeLabelsWithoutInternalPrefixes(db.GetAllLabels())) return &api.Database{ Uri: db.URI.String(), diff --git a/lib/teleterm/apiserver/handler/handler_kubes.go b/lib/teleterm/apiserver/handler/handler_kubes.go index 6642d49e19b71..0a2f3389cfcbf 100644 --- a/lib/teleterm/apiserver/handler/handler_kubes.go +++ b/lib/teleterm/apiserver/handler/handler_kubes.go @@ -20,7 +20,6 @@ package handler import ( "context" - "sort" "github.com/gravitational/trace" @@ -74,22 +73,11 @@ func (s *Handler) ListKubernetesResources(ctx context.Context, req *api.ListKube } func newAPIKube(kube clusters.Kube) *api.Kube { - apiLabels := APILabels{} - for name, value := range kube.KubernetesCluster.GetStaticLabels() { - apiLabels = append(apiLabels, &api.Label{ - Name: name, - Value: value, - }) - } - - for name, cmd := range kube.KubernetesCluster.GetDynamicLabels() { - apiLabels = append(apiLabels, &api.Label{ - Name: name, - Value: cmd.GetResult(), - }) - } - - sort.Sort(apiLabels) + staticLabels := kube.KubernetesCluster.GetStaticLabels() + dynamicLabels := kube.KubernetesCluster.GetDynamicLabels() + apiLabels := makeAPILabels( + ui.MakeLabelsWithoutInternalPrefixes(staticLabels, ui.TransformCommandLabels(dynamicLabels)), + ) return &api.Kube{ Name: kube.KubernetesCluster.GetName(), @@ -99,14 +87,7 @@ func newAPIKube(kube clusters.Kube) *api.Kube { } func newApiKubeResource(resource *types.KubernetesResourceV1, kubeCluster string, resourceURI uri.ResourceURI) *api.KubeResource { - uiLabels := ui.MakeLabelsWithoutInternalPrefixes(resource.GetStaticLabels()) - apiLabels := APILabels{} - for _, uiLabel := range uiLabels { - apiLabels = append(apiLabels, &api.Label{ - Name: uiLabel.Name, - Value: uiLabel.Value, - }) - } + apiLabels := makeAPILabels(ui.MakeLabelsWithoutInternalPrefixes(resource.GetStaticLabels())) return &api.KubeResource{ Uri: resourceURI.AppendKube(kubeCluster).AppendKubeResourceNamespace(resource.GetName()).String(), diff --git a/lib/teleterm/apiserver/handler/handler_servers.go b/lib/teleterm/apiserver/handler/handler_servers.go index 17230d0e1e529..ac21e9082c31b 100644 --- a/lib/teleterm/apiserver/handler/handler_servers.go +++ b/lib/teleterm/apiserver/handler/handler_servers.go @@ -20,12 +20,12 @@ package handler import ( "context" - "sort" "github.com/gravitational/trace" api "github.com/gravitational/teleport/gen/proto/go/teleport/lib/teleterm/v1" "github.com/gravitational/teleport/lib/teleterm/clusters" + "github.com/gravitational/teleport/lib/ui" ) // GetServers accepts parameterized input to enable searching, sorting, and pagination @@ -47,24 +47,11 @@ func (s *Handler) GetServers(ctx context.Context, req *api.GetServersRequest) (* } func newAPIServer(server clusters.Server) *api.Server { - apiLabels := APILabels{} serverLabels := server.GetStaticLabels() - for name, value := range serverLabels { - apiLabels = append(apiLabels, &api.Label{ - Name: name, - Value: value, - }) - } - serverCmdLabels := server.GetCmdLabels() - for name, cmd := range serverCmdLabels { - apiLabels = append(apiLabels, &api.Label{ - Name: name, - Value: cmd.GetResult(), - }) - } - - sort.Sort(apiLabels) + apiLabels := makeAPILabels( + ui.MakeLabelsWithoutInternalPrefixes(serverLabels, ui.TransformCommandLabels(serverCmdLabels)), + ) return &api.Server{ Uri: server.URI.String(), diff --git a/lib/ui/labels.go b/lib/ui/labels.go index 1da4c91498064..8b123a580705c 100644 --- a/lib/ui/labels.go +++ b/lib/ui/labels.go @@ -87,3 +87,13 @@ func (s sortedLabels) Less(i, j int) bool { func (s sortedLabels) Swap(i, j int) { s[i], s[j] = s[j], s[i] } + +func TransformCommandLabels(commandLabels map[string]types.CommandLabel) map[string]string { + labels := make(map[string]string, len(commandLabels)) + + for name, cmd := range commandLabels { + labels[name] = cmd.GetResult() + } + + return labels +} diff --git a/lib/ui/labels_test.go b/lib/ui/labels_test.go index e86704ccce794..02fb7223ae241 100644 --- a/lib/ui/labels_test.go +++ b/lib/ui/labels_test.go @@ -22,6 +22,8 @@ import ( "testing" "github.com/stretchr/testify/require" + + "github.com/gravitational/teleport/api/types" ) func TestMakeLabels(t *testing.T) { @@ -142,3 +144,17 @@ func TestMakeLabels(t *testing.T) { }) } } + +func TestTransformCommandLabels(t *testing.T) { + commandLabels := map[string]types.CommandLabel{ + "label1": &types.CommandLabelV2{ + Result: "value1", + }, + } + labels := TransformCommandLabels(commandLabels) + expected := map[string]string{ + "label1": "value1", + } + + require.Equal(t, expected, labels) +} diff --git a/lib/web/ui/labels.go b/lib/web/ui/labels.go deleted file mode 100644 index bb94474ab0b18..0000000000000 --- a/lib/web/ui/labels.go +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Teleport - * Copyright (C) 2023 Gravitational, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package ui - -import ( - "github.com/gravitational/teleport/api/types" -) - -func transformCommandLabels(commandLabels map[string]types.CommandLabel) map[string]string { - labels := make(map[string]string, len(commandLabels)) - - for name, cmd := range commandLabels { - labels[name] = cmd.GetResult() - } - - return labels -} diff --git a/lib/web/ui/labels_test.go b/lib/web/ui/labels_test.go deleted file mode 100644 index 296d91f4fd6af..0000000000000 --- a/lib/web/ui/labels_test.go +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Teleport - * Copyright (C) 2023 Gravitational, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package ui - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "github.com/gravitational/teleport/api/types" -) - -func TestTransformCommandLabels(t *testing.T) { - commandLabels := map[string]types.CommandLabel{ - "label1": &types.CommandLabelV2{ - Result: "value1", - }, - } - labels := transformCommandLabels(commandLabels) - expected := map[string]string{ - "label1": "value1", - } - - require.Equal(t, expected, labels) -} diff --git a/lib/web/ui/server.go b/lib/web/ui/server.go index af1adf66d3190..df68f3bcff277 100644 --- a/lib/web/ui/server.go +++ b/lib/web/ui/server.go @@ -71,7 +71,7 @@ type AWSMetadata struct { func MakeServer(clusterName string, server types.Server, logins []string, requiresRequest bool) Server { serverLabels := server.GetStaticLabels() serverCmdLabels := server.GetCmdLabels() - uiLabels := ui.MakeLabelsWithoutInternalPrefixes(serverLabels, transformCommandLabels(serverCmdLabels)) + uiLabels := ui.MakeLabelsWithoutInternalPrefixes(serverLabels, ui.TransformCommandLabels(serverCmdLabels)) uiServer := Server{ Kind: server.GetKind(), @@ -131,7 +131,7 @@ type KubeCluster struct { func MakeKubeCluster(cluster types.KubeCluster, accessChecker services.AccessChecker, requiresRequest bool) KubeCluster { staticLabels := cluster.GetStaticLabels() dynamicLabels := cluster.GetDynamicLabels() - uiLabels := ui.MakeLabelsWithoutInternalPrefixes(staticLabels, transformCommandLabels(dynamicLabels)) + uiLabels := ui.MakeLabelsWithoutInternalPrefixes(staticLabels, ui.TransformCommandLabels(dynamicLabels)) kubeUsers, kubeGroups := getAllowedKubeUsersAndGroupsForCluster(accessChecker, cluster) return KubeCluster{ Kind: cluster.GetKind(), @@ -166,7 +166,7 @@ func MakeKubeClusters(clusters []types.KubeCluster, accessChecker services.Acces for _, cluster := range clusters { staticLabels := cluster.GetStaticLabels() dynamicLabels := cluster.GetDynamicLabels() - uiLabels := ui.MakeLabelsWithoutInternalPrefixes(staticLabels, transformCommandLabels(dynamicLabels)) + uiLabels := ui.MakeLabelsWithoutInternalPrefixes(staticLabels, ui.TransformCommandLabels(dynamicLabels)) kubeUsers, kubeGroups := getAllowedKubeUsersAndGroupsForCluster(accessChecker, cluster)