Skip to content

Commit

Permalink
Merge pull request #195 from projectsyn/tenant-filter
Browse files Browse the repository at this point in the history
Fix filtering clusters by tenant
  • Loading branch information
bastjan authored Sep 2, 2022
2 parents fc7897a + 76608c5 commit 84846c0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkg/service/api_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ var (
"some": "value",
"monitoring.syn.tools/sla": "247",
},
Labels: map[string]string{
synv1alpha1.LabelNameTenant: tenantA.Name,
},
},
Spec: synv1alpha1.ClusterSpec{
DisplayName: "Sample Cluster A",
Expand Down Expand Up @@ -95,6 +98,9 @@ var (
Annotations: map[string]string{
"existing": "annotation",
},
Labels: map[string]string{
synv1alpha1.LabelNameTenant: tenantB.Name,
},
},
Spec: synv1alpha1.ClusterSpec{
DisplayName: "Sample Cluster B",
Expand Down
9 changes: 7 additions & 2 deletions pkg/service/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ const (
)

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

filterOptions := []client.ListOption{client.InNamespace(s.namespace)}
if p.Tenant != nil && *p.Tenant != "" {
filterOptions = append(filterOptions, client.MatchingLabels{synv1alpha1.LabelNameTenant: *p.Tenant})
}

clusterList := &synv1alpha1.ClusterList{}
err := ctx.client.List(ctx.Request().Context(), clusterList, client.InNamespace(s.namespace))
err := ctx.client.List(ctx.Request().Context(), clusterList, filterOptions...)
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/service/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ func TestListCluster(t *testing.T) {
assert.Contains(t, *clusters[0].Annotations, "some")
}

func TestListCluster_FilteredByTenant(t *testing.T) {
e, _ := setupTest(t)

result := testutil.NewRequest().
Get("/clusters?tenant="+tenantA.Name).
WithHeader(echo.HeaderAuthorization, bearerToken).
Go(t, e)
require.Equal(t, http.StatusOK, result.Code())
clusters := make([]api.Cluster, 0)
err := result.UnmarshalJsonToObject(&clusters)
assert.NoError(t, err)
assert.Len(t, clusters, 1)
assert.Equal(t, clusterA.Spec.DisplayName, *clusters[0].DisplayName)
}

func TestListClusterMissingBearer(t *testing.T) {
e, _ := setupTest(t)

Expand Down

0 comments on commit 84846c0

Please sign in to comment.