...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/rules/commands.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/rules

     1  //nolint:dupl
     2  package rulesengine
     3  
     4  import (
     5  	"context"
     6  	"errors"
     7  	"fmt"
     8  )
     9  
    10  // AddCommands updates the dataset with given names. returns a list of
    11  // values where the values remain unchanged in the dataset.
    12  func (reng RulesEngine) AddCommands(ctx context.Context, commands []PostCommandPayload) (AddNameResult, error) {
    13  	//validation
    14  	if len(commands) == 0 {
    15  		return AddNameResult{}, fmt.Errorf("empty command list")
    16  	}
    17  	if len(commands) > maxCommands {
    18  		return AddNameResult{}, fmt.Errorf("total number of commands %d exceeds max %d", len(commands), maxCommands)
    19  	}
    20  	var retErr error
    21  	for i, command := range commands {
    22  		if err := command.Validate(); err != nil {
    23  			retErr = errors.Join(retErr, fmt.Errorf("invalid command at %d: %w", i, err))
    24  		}
    25  	}
    26  	if retErr != nil {
    27  		return AddNameResult{}, retErr
    28  	}
    29  
    30  	comms := []string{}
    31  	for _, comm := range commands {
    32  		comms = append(comms, comm.Name)
    33  	}
    34  	return reng.ds.AddCommands(ctx, comms)
    35  }
    36  
    37  func (reng RulesEngine) DeleteCommand(ctx context.Context, name string) (DeleteResult, error) {
    38  	// validation
    39  	if name == "" {
    40  		return DeleteResult{}, fmt.Errorf("empty command name")
    41  	}
    42  	return reng.ds.DeleteCommand(ctx, name)
    43  }
    44  
    45  func (reng RulesEngine) ReadCommands(ctx context.Context) ([]Command, error) {
    46  	return reng.ds.ReadAllCommands(ctx)
    47  }
    48  
    49  // Takes in a list of command names and returns all commands in db that match.
    50  // If the names list is empty, then all commands are returned.
    51  func (reng RulesEngine) ReadCommandsWithFilter(ctx context.Context, names []string) ([]Command, error) {
    52  	return reng.ds.ReadCommandsWithFilter(ctx, names)
    53  }
    54  
    55  func (reng RulesEngine) ReadCommand(ctx context.Context, name string) (Command, error) {
    56  	// validation
    57  	if name == "" {
    58  		return Command{}, fmt.Errorf("empty command name")
    59  	}
    60  	return reng.ds.ReadCommand(ctx, name)
    61  }
    62  

View as plain text