mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AccessControl: Remove unused error from GetResourcesMetadata
(#43710)
* AccessControl: Remove unused error from GetResourcesMetadata Co-authored-by: J Guerreiro <joao.guerreiro@grafana.com>
This commit is contained in:
parent
b4204628e4
commit
92c568e9f7
@ -78,12 +78,7 @@ func (hs *HTTPServer) getDataSourceAccessControlMetadata(c *models.ReqContext, d
|
|||||||
key := fmt.Sprintf("%d", dsID)
|
key := fmt.Sprintf("%d", dsID)
|
||||||
dsIDs := map[string]bool{key: true}
|
dsIDs := map[string]bool{key: true}
|
||||||
|
|
||||||
metadata, err := accesscontrol.GetResourcesMetadata(c.Req.Context(), userPermissions, "datasources", dsIDs)
|
return accesscontrol.GetResourcesMetadata(c.Req.Context(), userPermissions, "datasources", dsIDs)[key], nil
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return metadata[key], err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hs *HTTPServer) GetDataSourceById(c *models.ReqContext) response.Response {
|
func (hs *HTTPServer) GetDataSourceById(c *models.ReqContext) response.Response {
|
||||||
|
@ -115,7 +115,7 @@ func (hs *HTTPServer) getUserAccessControlMetadata(c *models.ReqContext, resourc
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return accesscontrol.GetResourcesMetadata(c.Req.Context(), userPermissions, "users", resourceIDs)
|
return accesscontrol.GetResourcesMetadata(c.Req.Context(), userPermissions, "users", resourceIDs), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /api/orgs/:orgId/users
|
// GET /api/orgs/:orgId/users
|
||||||
|
@ -68,12 +68,7 @@ func (hs *HTTPServer) getGlobalUserAccessControlMetadata(c *models.ReqContext, u
|
|||||||
key := fmt.Sprintf("%d", userID)
|
key := fmt.Sprintf("%d", userID)
|
||||||
userIDs := map[string]bool{key: true}
|
userIDs := map[string]bool{key: true}
|
||||||
|
|
||||||
metadata, err := accesscontrol.GetResourcesMetadata(c.Req.Context(), userPermissions, "global:users", userIDs)
|
return accesscontrol.GetResourcesMetadata(c.Req.Context(), userPermissions, "global:users", userIDs)[key], nil
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return metadata[key], err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /api/users/lookup
|
// GET /api/users/lookup
|
||||||
|
@ -146,7 +146,7 @@ func addActionToMetadata(allMetadata map[string]Metadata, action, id string) map
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetResourcesMetadata returns a map of accesscontrol metadata, listing for each resource, users available actions
|
// GetResourcesMetadata returns a map of accesscontrol metadata, listing for each resource, users available actions
|
||||||
func GetResourcesMetadata(ctx context.Context, permissions []*Permission, resource string, resourceIDs map[string]bool) (map[string]Metadata, error) {
|
func GetResourcesMetadata(ctx context.Context, permissions []*Permission, resource string, resourceIDs map[string]bool) map[string]Metadata {
|
||||||
allScope := GetResourceAllScope(resource)
|
allScope := GetResourceAllScope(resource)
|
||||||
allIDScope := GetResourceAllIDScope(resource)
|
allIDScope := GetResourceAllIDScope(resource)
|
||||||
|
|
||||||
@ -171,5 +171,5 @@ func GetResourcesMetadata(ctx context.Context, permissions []*Permission, resour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setupTestEnv(b *testing.B, resourceCount, permissionPerResource int) ([]*Permission, map[string]bool) {
|
func setupTestEnv(b *testing.B, resourceCount, permissionPerResource int) ([]*Permission, map[string]bool) {
|
||||||
@ -31,10 +30,8 @@ func benchGetMetadata(b *testing.B, resourceCount, permissionPerResource int) {
|
|||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
var metadata map[string]Metadata
|
var metadata map[string]Metadata
|
||||||
var err error
|
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
metadata, err = GetResourcesMetadata(context.Background(), permissions, "resources", ids)
|
metadata = GetResourcesMetadata(context.Background(), permissions, "resources", ids)
|
||||||
require.NoError(b, err)
|
|
||||||
assert.Len(b, metadata, resourceCount)
|
assert.Len(b, metadata, resourceCount)
|
||||||
for _, resourceMetadata := range metadata {
|
for _, resourceMetadata := range metadata {
|
||||||
assert.Len(b, resourceMetadata, permissionPerResource)
|
assert.Len(b, resourceMetadata, permissionPerResource)
|
||||||
@ -53,7 +50,7 @@ func BenchmarkGetResourcesMetadata_10_1000000(b *testing.B) {
|
|||||||
benchGetMetadata(b, 10, 1000000)
|
benchGetMetadata(b, 10, 1000000)
|
||||||
} // ~3.89s/op
|
} // ~3.89s/op
|
||||||
|
|
||||||
// Lots of resources (worst case)
|
// Lots of resources
|
||||||
func BenchmarkGetResourcesMetadata_1000_10(b *testing.B) { benchGetMetadata(b, 1000, 10) } // ~0,0023s/op
|
func BenchmarkGetResourcesMetadata_1000_10(b *testing.B) { benchGetMetadata(b, 1000, 10) } // ~0,0023s/op
|
||||||
func BenchmarkGetResourcesMetadata_10000_10(b *testing.B) { benchGetMetadata(b, 10000, 10) } // ~0.021s/op
|
func BenchmarkGetResourcesMetadata_10000_10(b *testing.B) { benchGetMetadata(b, 10000, 10) } // ~0.021s/op
|
||||||
func BenchmarkGetResourcesMetadata_100000_10(b *testing.B) { benchGetMetadata(b, 100000, 10) } // ~0.22s/op
|
func BenchmarkGetResourcesMetadata_100000_10(b *testing.B) { benchGetMetadata(b, 100000, 10) } // ~0.22s/op
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetResourcesMetadata(t *testing.T) {
|
func TestGetResourcesMetadata(t *testing.T) {
|
||||||
@ -95,9 +94,7 @@ func TestGetResourcesMetadata(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.desc, func(t *testing.T) {
|
t.Run(tt.desc, func(t *testing.T) {
|
||||||
metadata, err := GetResourcesMetadata(context.Background(), tt.permissions, tt.resource, tt.resourcesIDs)
|
metadata := GetResourcesMetadata(context.Background(), tt.permissions, tt.resource, tt.resourcesIDs)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
assert.EqualValues(t, tt.expected, metadata)
|
assert.EqualValues(t, tt.expected, metadata)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user