Skip to content

Commit 84846c0

Browse files
authored
Merge pull request #195 from projectsyn/tenant-filter
Fix filtering clusters by tenant
2 parents fc7897a + 76608c5 commit 84846c0

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

pkg/service/api_service_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ var (
6565
"some": "value",
6666
"monitoring.syn.tools/sla": "247",
6767
},
68+
Labels: map[string]string{
69+
synv1alpha1.LabelNameTenant: tenantA.Name,
70+
},
6871
},
6972
Spec: synv1alpha1.ClusterSpec{
7073
DisplayName: "Sample Cluster A",
@@ -95,6 +98,9 @@ var (
9598
Annotations: map[string]string{
9699
"existing": "annotation",
97100
},
101+
Labels: map[string]string{
102+
synv1alpha1.LabelNameTenant: tenantB.Name,
103+
},
98104
},
99105
Spec: synv1alpha1.ClusterSpec{
100106
DisplayName: "Sample Cluster B",

pkg/service/cluster.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ const (
2525
)
2626

2727
// ListClusters lists all clusters
28-
func (s *APIImpl) ListClusters(c echo.Context, _ api.ListClustersParams) error {
28+
func (s *APIImpl) ListClusters(c echo.Context, p api.ListClustersParams) error {
2929
ctx := c.(*APIContext)
3030

31+
filterOptions := []client.ListOption{client.InNamespace(s.namespace)}
32+
if p.Tenant != nil && *p.Tenant != "" {
33+
filterOptions = append(filterOptions, client.MatchingLabels{synv1alpha1.LabelNameTenant: *p.Tenant})
34+
}
35+
3136
clusterList := &synv1alpha1.ClusterList{}
32-
err := ctx.client.List(ctx.Request().Context(), clusterList, client.InNamespace(s.namespace))
37+
err := ctx.client.List(ctx.Request().Context(), clusterList, filterOptions...)
3338
if err != nil {
3439
return err
3540
}

pkg/service/cluster_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ func TestListCluster(t *testing.T) {
3939
assert.Contains(t, *clusters[0].Annotations, "some")
4040
}
4141

42+
func TestListCluster_FilteredByTenant(t *testing.T) {
43+
e, _ := setupTest(t)
44+
45+
result := testutil.NewRequest().
46+
Get("/clusters?tenant="+tenantA.Name).
47+
WithHeader(echo.HeaderAuthorization, bearerToken).
48+
Go(t, e)
49+
require.Equal(t, http.StatusOK, result.Code())
50+
clusters := make([]api.Cluster, 0)
51+
err := result.UnmarshalJsonToObject(&clusters)
52+
assert.NoError(t, err)
53+
assert.Len(t, clusters, 1)
54+
assert.Equal(t, clusterA.Spec.DisplayName, *clusters[0].DisplayName)
55+
}
56+
4257
func TestListClusterMissingBearer(t *testing.T) {
4358
e, _ := setupTest(t)
4459

0 commit comments

Comments
 (0)