-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hide internal labels from UI (#18638)
* hide labels from UI * add test cases for hidding teleport.internal/ labels * move labels logic to separate file * move constant to right api/types/constants.go * add godoc to makeLabels and rework label constants Co-authored-by: Jim Bishopp <jim@goteleport.com>
- Loading branch information
1 parent
7b4c99b
commit f0dbc34
Showing
7 changed files
with
439 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2020 Gravitational, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package ui | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/gravitational/teleport/api/types" | ||
) | ||
|
||
func TestMakeAppsLabelFilter(t *testing.T) { | ||
type testCase struct { | ||
types.Apps | ||
expected []App | ||
name string | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "Single App with teleport.internal/ label", | ||
Apps: types.Apps{ | ||
&types.AppV3{ | ||
Metadata: types.Metadata{ | ||
Name: "App1", | ||
Labels: map[string]string{ | ||
"first": "value1", | ||
"teleport.internal/dd": "hidden1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: []App{ | ||
{ | ||
Name: "App1", | ||
Labels: []Label{ | ||
{ | ||
Name: "first", | ||
Value: "value1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
config := MakeAppsConfig{ | ||
Apps: tc.Apps, | ||
} | ||
apps := MakeApps(config) | ||
|
||
for i, app := range apps { | ||
expectedLabels := tc.expected[i].Labels | ||
|
||
require.Equal(t, expectedLabels, app.Labels) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2020 Gravitational, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package ui | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
"github.com/gravitational/teleport/api/types" | ||
) | ||
|
||
// makeLabels is a function that transforms map[string]string arguments passed to it to sorted slice of Labels. | ||
// It also removes all Teleport internal labels from output. | ||
func makeLabels(labelMaps ...map[string]string) []Label { | ||
length := 0 | ||
for _, labelMap := range labelMaps { | ||
length += len(labelMap) | ||
} | ||
|
||
labels := make([]Label, 0, length) | ||
|
||
for _, labelMap := range labelMaps { | ||
for name, value := range labelMap { | ||
if strings.HasPrefix(name, types.TeleportInternalLabelPrefix) { | ||
continue | ||
} | ||
|
||
labels = append(labels, Label{Name: name, Value: value}) | ||
} | ||
} | ||
|
||
sort.Sort(sortedLabels(labels)) | ||
|
||
return labels | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
Copyright 2020 Gravitational, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package ui | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/gravitational/teleport/api/types" | ||
) | ||
|
||
func TestMakeLabels(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
labelMaps []map[string]string | ||
expected []Label | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "Single map single label case", | ||
labelMaps: []map[string]string{ | ||
{ | ||
"label1": "value1", | ||
}, | ||
}, | ||
expected: []Label{ | ||
{ | ||
Name: "label1", | ||
Value: "value1", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Single map multiple labels case", | ||
labelMaps: []map[string]string{ | ||
{ | ||
"label1": "value1", | ||
"label2": "value2", | ||
}, | ||
}, | ||
expected: []Label{ | ||
{ | ||
Name: "label1", | ||
Value: "value1", | ||
}, | ||
{ | ||
Name: "label2", | ||
Value: "value2", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Multiple maps single label case", | ||
labelMaps: []map[string]string{ | ||
{ | ||
"label1": "value1", | ||
}, | ||
{ | ||
"label2": "value2", | ||
}, | ||
}, | ||
expected: []Label{ | ||
{ | ||
Name: "label1", | ||
Value: "value1", | ||
}, | ||
{ | ||
Name: "label2", | ||
Value: "value2", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Multiple maps with internal labels", | ||
labelMaps: []map[string]string{ | ||
{ | ||
"label1": "value1", | ||
"teleport.internal/label3": "value3", | ||
}, | ||
{ | ||
"label2": "value2", | ||
"teleport.internal/label4": "value4", | ||
}, | ||
}, | ||
expected: []Label{ | ||
{ | ||
Name: "label1", | ||
Value: "value1", | ||
}, | ||
{ | ||
Name: "label2", | ||
Value: "value2", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
labels := makeLabels(tc.labelMaps...) | ||
|
||
require.Equal(t, tc.expected, labels) | ||
}) | ||
} | ||
} | ||
|
||
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) | ||
} |
Oops, something went wrong.