Commit 1f663e6f authored by ale's avatar ale

Add a FindResource method to the backend interface

Required for fetching a specific resource in LDAP-aware validators.
parent 041e8fab
Pipeline #6352 passed with stages
in 1 minute and 31 seconds
......@@ -85,6 +85,16 @@ func (b *fakeBackend) GetResource(_ context.Context, resourceID ResourceID) (*Ra
return &RawResource{Resource: *r, Owner: owner}, nil
func (b *fakeBackend) FindResource(_ context.Context, req FindResourceRequest) (*RawResource, error) {
for id, r := range b.resources {
if r.Type == req.Type && r.Name == req.Name {
owner := strings.Split(id, "/")[0]
return &RawResource{Resource: *r, Owner: owner}, nil
return nil, nil
func (b *fakeBackend) SearchResource(_ context.Context, pattern string) ([]*RawResource, error) {
var out []*RawResource
for id, r := range b.resources {
......@@ -3,6 +3,7 @@ package ldapbackend
import (
......@@ -525,6 +526,22 @@ func (tx *backendTX) searchResourcesByType(ctx context.Context, pattern, resourc
return out, nil
// FindResource fetches a specific resource by type and name.
func (tx *backendTX) FindResource(ctx context.Context, spec as.FindResourceRequest) (*as.RawResource, error) {
result, err := tx.searchResourcesByType(ctx, spec.Name, spec.Type)
if err != nil {
return nil, err
switch len(result) {
case 0:
return nil, nil
case 1:
return result[0], nil
return nil, errors.New("too many results")
// SearchResource returns all the resources matching the pattern.
func (tx *backendTX) SearchResource(ctx context.Context, pattern string) ([]*as.RawResource, error) {
// Aggregate results for all known resource types.
......@@ -51,6 +51,7 @@ type TX interface {
UpdateResource(context.Context, *Resource) error
CreateResources(context.Context, *User, []*Resource) ([]*Resource, error)
SetResourcePassword(context.Context, *Resource, string) error
FindResource(context.Context, FindResourceRequest) (*RawResource, error)
HasAnyResource(context.Context, []FindResourceRequest) (bool, error)
GetUser(context.Context, string) (*RawUser, error)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment