Skip to content

Commit

Permalink
[ENHANCEMENT] Add query param 'metadata_only' on almost all query end…
Browse files Browse the repository at this point in the history
…points

Signed-off-by: Augustin Husson <husson.augustin@gmail.com>
  • Loading branch information
Nexucis committed May 14, 2024
1 parent d7ef3f5 commit 1aeae1d
Show file tree
Hide file tree
Showing 50 changed files with 561 additions and 101 deletions.
13 changes: 13 additions & 0 deletions internal/api/dashboard/cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package dashboard

import (
"context"
"github.com/perses/perses/pkg/model/api"
"testing"
"time"

Expand All @@ -32,6 +33,18 @@ func (d *mockDAO) List(_ *ephemeraldashboard.Query) ([]*v1.EphemeralDashboard, e
return d.dashboards, nil
}

func (d *mockDAO) MetadataList(_ *ephemeraldashboard.Query) ([]api.Entity, error) {
var result []api.Entity
for _, dashboard := range d.dashboards {
result = append(result, &v1.PartialProjectEntity{
Kind: dashboard.Kind,
Metadata: dashboard.Metadata,
Spec: struct{}{},
})
}
return result, nil
}

func (d *mockDAO) Delete(project string, name string) error {
for ed := range d.dashboards {
if d.dashboards[ed].Metadata.Project == project && d.dashboards[ed].Metadata.Name == name {
Expand Down
1 change: 1 addition & 0 deletions internal/api/database/model/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

type Query interface {
GetMetadataOnlyQueryParam() bool
}

type DAO interface {
Expand Down
11 changes: 11 additions & 0 deletions internal/api/impl/v1/dashboard/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package dashboard
import (
databaseModel "github.com/perses/perses/internal/api/database/model"
"github.com/perses/perses/internal/api/interface/v1/dashboard"
"github.com/perses/perses/pkg/model/api"
v1 "github.com/perses/perses/pkg/model/api/v1"
)

Expand Down Expand Up @@ -58,3 +59,13 @@ func (d *dao) List(q *dashboard.Query) ([]*v1.Dashboard, error) {
err := d.client.Query(q, &result)
return result, err
}

func (d *dao) MetadataList(q *dashboard.Query) ([]api.Entity, error) {
var list []*v1.PartialProjectEntity
err := d.client.Query(q, &list)
result := make([]api.Entity, 0, len(list))
for _, el := range list {
result = append(result, el)
}
return result, err
}
29 changes: 21 additions & 8 deletions internal/api/impl/v1/dashboard/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package dashboard

import (
"fmt"
"github.com/perses/perses/pkg/model/api"

"github.com/brunoga/deep"
apiInterface "github.com/perses/perses/internal/api/interface"
Expand Down Expand Up @@ -113,19 +114,19 @@ func (s *service) Get(_ apiInterface.PersesContext, parameters apiInterface.Para
}

func (s *service) List(_ apiInterface.PersesContext, q *dashboard.Query, params apiInterface.Parameters) ([]*v1.Dashboard, error) {
// Query is copied because it can be modified by the toolbox.go: listWhenPermissionIsActivated(...) and need to `q` need to keep initial value
query, err := deep.Copy(q)
query, err := manageQuery(q, params)
if err != nil {
return nil, fmt.Errorf("unable to copy the query: %w", err)
return nil, err
}
return s.list(query, params)
return s.dao.List(query)
}

func (s *service) list(q *dashboard.Query, params apiInterface.Parameters) ([]*v1.Dashboard, error) {
if len(q.Project) == 0 {
q.Project = params.Project
func (s *service) MetadataList(_ apiInterface.PersesContext, q *dashboard.Query, params apiInterface.Parameters) ([]api.Entity, error) {
query, err := manageQuery(q, params)
if err != nil {
return nil, err
}
return s.dao.List(q)
return s.dao.MetadataList(query)
}

func (s *service) Validate(entity *v1.Dashboard) error {
Expand Down Expand Up @@ -155,3 +156,15 @@ func (s *service) collectProjectVariables(project string) ([]*v1.Variable, error
func (s *service) collectGlobalVariables() ([]*v1.GlobalVariable, error) {
return s.globalVarDAO.List(&globalvariable.Query{})
}

func manageQuery(q *dashboard.Query, params apiInterface.Parameters) (*dashboard.Query, error) {
// Query is copied because it can be modified by the toolbox.go: listWhenPermissionIsActivated(...) and need to `q` need to keep initial value
query, err := deep.Copy(q)
if err != nil {
return nil, fmt.Errorf("unable to copy the query: %w", err)
}
if len(query.Project) == 0 {
query.Project = params.Project
}
return query, nil
}
29 changes: 16 additions & 13 deletions internal/api/impl/v1/datasource/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package datasource

import (
"fmt"

"github.com/brunoga/deep"
apiInterface "github.com/perses/perses/internal/api/interface"
"github.com/perses/perses/internal/api/interface/v1/datasource"
Expand Down Expand Up @@ -101,23 +100,15 @@ func (s *service) Get(_ apiInterface.PersesContext, parameters apiInterface.Para
return s.dao.Get(parameters.Project, parameters.Name)
}
func (s *service) List(_ apiInterface.PersesContext, q *datasource.Query, params apiInterface.Parameters) ([]*v1.Datasource, error) {
// Query is copied because it can be modified by the toolbox.go: listWhenPermissionIsActivated(...) and need to `q` need to keep initial value
query, err := deep.Copy(q)
query, err := manageQuery(q, params)
if err != nil {
return nil, fmt.Errorf("unable to copy the query: %w", err)
}
return s.list(query, params)
}

func (s *service) list(q *datasource.Query, params apiInterface.Parameters) ([]*v1.Datasource, error) {
if len(q.Project) == 0 {
q.Project = params.Project
return nil, err
}
dtsList, err := s.dao.List(q)
dtsList, err := s.dao.List(query)
if err != nil {
return nil, err
}
return v1.FilterDatasource(q.Kind, q.Default, dtsList), nil
return v1.FilterDatasource(query.Kind, query.Default, dtsList), nil
}

func (s *service) validate(entity *v1.Datasource) error {
Expand All @@ -133,3 +124,15 @@ func (s *service) validate(entity *v1.Datasource) error {
}
return validate.Datasource(entity, list, s.sch)
}

func manageQuery(q *datasource.Query, params apiInterface.Parameters) (*datasource.Query, error) {
// Query is copied because it can be modified by the toolbox.go: listWhenPermissionIsActivated(...) and need to `q` need to keep initial value
query, err := deep.Copy(q)
if err != nil {
return nil, fmt.Errorf("unable to copy the query: %w", err)
}
if len(query.Project) == 0 {
query.Project = params.Project
}
return query, nil
}
11 changes: 11 additions & 0 deletions internal/api/impl/v1/ephemeraldashboard/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ephemeraldashboard
import (
databaseModel "github.com/perses/perses/internal/api/database/model"
"github.com/perses/perses/internal/api/interface/v1/ephemeraldashboard"
"github.com/perses/perses/pkg/model/api"
v1 "github.com/perses/perses/pkg/model/api/v1"
)

Expand Down Expand Up @@ -58,3 +59,13 @@ func (d *dao) List(q *ephemeraldashboard.Query) ([]*v1.EphemeralDashboard, error
err := d.client.Query(q, &result)
return result, err
}

func (d *dao) MetadataList(q *ephemeraldashboard.Query) ([]api.Entity, error) {
var list []*v1.PartialProjectEntity
err := d.client.Query(q, &list)
result := make([]api.Entity, 0, len(list))
for _, el := range list {
result = append(result, el)
}
return result, err
}
28 changes: 20 additions & 8 deletions internal/api/impl/v1/ephemeraldashboard/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package ephemeraldashboard

import (
"fmt"
"github.com/perses/perses/pkg/model/api"

"github.com/brunoga/deep"
apiInterface "github.com/perses/perses/internal/api/interface"
Expand Down Expand Up @@ -113,19 +114,19 @@ func (s *service) Get(_ apiInterface.PersesContext, parameters apiInterface.Para
}

func (s *service) List(_ apiInterface.PersesContext, q *ephemeraldashboard.Query, params apiInterface.Parameters) ([]*v1.EphemeralDashboard, error) {
// Query is copied because it can be modified by the toolbox.go: listWhenPermissionIsActivated(...) and need to `q` need to keep initial value
query, err := deep.Copy(q)
query, err := manageQuery(q, params)
if err != nil {
return nil, fmt.Errorf("unable to copy the query: %w", err)
return nil, err
}
return s.list(query, params)
return s.dao.List(query)
}

func (s *service) list(q *ephemeraldashboard.Query, params apiInterface.Parameters) ([]*v1.EphemeralDashboard, error) {
if len(q.Project) == 0 {
q.Project = params.Project
func (s *service) MetadataList(_ apiInterface.PersesContext, q *ephemeraldashboard.Query, params apiInterface.Parameters) ([]api.Entity, error) {
query, err := manageQuery(q, params)
if err != nil {
return nil, err
}
return s.dao.List(q)
return s.dao.MetadataList(query)
}

func (s *service) Validate(entity *v1.EphemeralDashboard) error {
Expand Down Expand Up @@ -155,3 +156,14 @@ func (s *service) collectProjectVariables(project string) ([]*v1.Variable, error
func (s *service) collectGlobalVariables() ([]*v1.GlobalVariable, error) {
return s.globalVarDAO.List(&globalvariable.Query{})
}

func manageQuery(q *ephemeraldashboard.Query, params apiInterface.Parameters) (*ephemeraldashboard.Query, error) {
query, err := deep.Copy(q)
if err != nil {
return nil, fmt.Errorf("unable to copy the query: %w", err)
}
if len(query.Project) == 0 {
query.Project = params.Project
}
return query, nil
}
11 changes: 11 additions & 0 deletions internal/api/impl/v1/folder/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package folder
import (
databaseModel "github.com/perses/perses/internal/api/database/model"
"github.com/perses/perses/internal/api/interface/v1/folder"
"github.com/perses/perses/pkg/model/api"
v1 "github.com/perses/perses/pkg/model/api/v1"
)

Expand Down Expand Up @@ -58,3 +59,13 @@ func (d *dao) List(q *folder.Query) ([]*v1.Folder, error) {
err := d.client.Query(q, &result)
return result, err
}

func (d *dao) MetadataList(q *folder.Query) ([]api.Entity, error) {
var list []*v1.PartialProjectEntity
err := d.client.Query(q, &list)
result := make([]api.Entity, 0, len(list))
for _, el := range list {
result = append(result, el)
}
return result, err
}
28 changes: 20 additions & 8 deletions internal/api/impl/v1/folder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ package folder

import (
"fmt"

"github.com/brunoga/deep"
apiInterface "github.com/perses/perses/internal/api/interface"
"github.com/perses/perses/internal/api/interface/v1/folder"
"github.com/perses/perses/pkg/model/api"
v1 "github.com/perses/perses/pkg/model/api/v1"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -92,17 +92,29 @@ func (s *service) Get(_ apiInterface.PersesContext, parameters apiInterface.Para
}

func (s *service) List(_ apiInterface.PersesContext, q *folder.Query, params apiInterface.Parameters) ([]*v1.Folder, error) {
query, err := manageQuery(q, params)
if err != nil {
return nil, err
}
return s.dao.List(query)
}

func (s *service) MetadataList(_ apiInterface.PersesContext, q *folder.Query, params apiInterface.Parameters) ([]api.Entity, error) {
query, err := manageQuery(q, params)
if err != nil {
return nil, err
}
return s.dao.MetadataList(query)
}

func manageQuery(q *folder.Query, params apiInterface.Parameters) (*folder.Query, error) {
// Query is copied because it can be modified by the toolbox.go: listWhenPermissionIsActivated(...) and need to `q` need to keep initial value
query, err := deep.Copy(q)
if err != nil {
return nil, fmt.Errorf("unable to copy the query: %w", err)
}
return s.list(query, params)
}

func (s *service) list(q *folder.Query, params apiInterface.Parameters) ([]*v1.Folder, error) {
if len(q.Project) == 0 {
q.Project = params.Project
if len(query.Project) == 0 {
query.Project = params.Project
}
return s.dao.List(q)
return query, nil
}
11 changes: 11 additions & 0 deletions internal/api/impl/v1/globalrole/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package globalrole
import (
databaseModel "github.com/perses/perses/internal/api/database/model"
"github.com/perses/perses/internal/api/interface/v1/globalrole"
"github.com/perses/perses/pkg/model/api"
v1 "github.com/perses/perses/pkg/model/api/v1"
)

Expand Down Expand Up @@ -54,3 +55,13 @@ func (d *dao) List(q *globalrole.Query) ([]*v1.GlobalRole, error) {
err := d.client.Query(q, &result)
return result, err
}

func (d *dao) MetadataList(q *globalrole.Query) ([]api.Entity, error) {
var list []*v1.PartialEntity
err := d.client.Query(q, &list)
result := make([]api.Entity, 0, len(list))
for _, el := range list {
result = append(result, el)
}
return result, err
}
5 changes: 5 additions & 0 deletions internal/api/impl/v1/globalrole/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package globalrole

import (
"fmt"
"github.com/perses/perses/pkg/model/api"

"github.com/brunoga/deep"
apiInterface "github.com/perses/perses/internal/api/interface"
Expand Down Expand Up @@ -110,3 +111,7 @@ func (s *service) Get(_ apiInterface.PersesContext, parameters apiInterface.Para
func (s *service) List(_ apiInterface.PersesContext, q *globalrole.Query, _ apiInterface.Parameters) ([]*v1.GlobalRole, error) {
return s.dao.List(q)
}

func (s *service) MetadataList(_ apiInterface.PersesContext, q *globalrole.Query, _ apiInterface.Parameters) ([]api.Entity, error) {
return s.dao.MetadataList(q)
}
11 changes: 11 additions & 0 deletions internal/api/impl/v1/globalrolebinding/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package globalrolebinding
import (
databaseModel "github.com/perses/perses/internal/api/database/model"
"github.com/perses/perses/internal/api/interface/v1/globalrolebinding"
"github.com/perses/perses/pkg/model/api"
v1 "github.com/perses/perses/pkg/model/api/v1"
)

Expand Down Expand Up @@ -54,3 +55,13 @@ func (d *dao) List(q *globalrolebinding.Query) ([]*v1.GlobalRoleBinding, error)
err := d.client.Query(q, &result)
return result, err
}

func (d *dao) MetadataList(q *globalrolebinding.Query) ([]api.Entity, error) {
var list []*v1.PartialEntity
err := d.client.Query(q, &list)
result := make([]api.Entity, 0, len(list))
for _, el := range list {
result = append(result, el)
}
return result, err
}
5 changes: 5 additions & 0 deletions internal/api/impl/v1/globalrolebinding/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package globalrolebinding

import (
"fmt"
"github.com/perses/perses/pkg/model/api"

"github.com/brunoga/deep"
databaseModel "github.com/perses/perses/internal/api/database/model"
Expand Down Expand Up @@ -133,6 +134,10 @@ func (s *service) List(_ apiInterface.PersesContext, q *globalrolebinding.Query,
return s.dao.List(q)
}

func (s *service) MetadataList(_ apiInterface.PersesContext, q *globalrolebinding.Query, _ apiInterface.Parameters) ([]api.Entity, error) {
return s.dao.MetadataList(q)
}

// Validating role and subjects are existing
func (s *service) validateGlobalRoleBinding(globalRoleBinding *v1.GlobalRoleBinding) error {
if _, err := s.globalRoleDAO.Get(globalRoleBinding.Spec.Role); err != nil {
Expand Down
Loading

0 comments on commit 1aeae1d

Please sign in to comment.