rule

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AttrKeyComponent = logging.AttrKeyComponent
	AttrKeyOperation = logging.AttrKeyOperation
	AttrKeyError     = logging.AttrKeyError

	AttrKeyRuleID               = "rule_id"
	AttrKeyRuleType             = "rule_type"
	AttrKeyDeviceID             = "device_id"
	AttrDeviceAutoExpiryRuleTTL = "device_autoexpiry_ttl_sec"
)

Slog attribute key names for the rule domain. Use these constants when logging so keys are consistent and typo-safe across services.

Variables

View Source
var (
	// ErrRuleNotFound is returned when a requested rule does not exist.
	ErrRuleNotFound = errors.New("rule not found")

	// ErrInvalidRuleConfig is returned when a rule's configuration cannot be parsed
	// or violates basic invariants.
	ErrInvalidRuleConfig = errors.New("invalid rule config")

	// ErrInvalidTTL is returned when a AddressTTLSeconds value is non-positive.
	ErrInvalidTTL = errors.New("ttl_seconds must be greater than 0")

	// ErrInvalidMaxAddresses is returned when max_addresses is less than 1.
	ErrInvalidMaxAddresses = errors.New("max_addresses must be at least 1")
)

Functions

This section is empty.

Types

type DeviceAddressLeaseConfig

type DeviceAddressLeaseConfig struct {
	TTLSeconds int `json:"ttl_seconds"`
}

DeviceAddressLeaseConfig holds configuration values for the device lease rule.

func NewDeviceAddressLeaseConfig

func NewDeviceAddressLeaseConfig(addressTTLSeconds int) (DeviceAddressLeaseConfig, error)

NewDeviceAddressLeaseConfig Creates an addess lease config and validates the parameters

type DeviceAddressLeaseRule

type DeviceAddressLeaseRule struct {
	ID        RuleID
	DeviceID  device.DeviceID
	Enabled   bool
	Config    DeviceAddressLeaseConfig
	CreatedAt time.Time
	UpdatedAt time.Time
}

DeviceAddressLeaseRule represents the domain model for the device lease rule.

type HTTPHandler

type HTTPHandler struct {
	// contains filtered or unexported fields
}

HTTPHandler handles HTTP requests for rule endpoints.

func NewHTTPHandler

func NewHTTPHandler(ruleService *Service, logger *slog.Logger) *HTTPHandler

NewHTTPHandler returns a new rule HTTP handler.

func (*HTTPHandler) DisableDeviceAddressLeaseRule

DisableDeviceAddressLeaseRule disables the device address lease rule for the device.

func (*HTTPHandler) DisableMaxActiveAddressesRule added in v0.2.0

DisableMaxActiveAddressesRule disables the max active addresses rule for the device.

func (*HTTPHandler) GetDeviceAddressLeaseRule

GetDeviceAddressLeaseRule returns the device address lease rule for the device.

func (*HTTPHandler) GetMaxActiveAddressesRule added in v0.2.0

GetMaxActiveAddressesRule returns the max active addresses rule for the device.

func (*HTTPHandler) PutDeviceAddressLeaseRule

PutDeviceAddressLeaseRule creates or updates the device address lease rule for the device.

func (*HTTPHandler) PutMaxActiveAddressesRule added in v0.2.0

PutMaxActiveAddressesRule creates or updates the max active addresses rule for the device.

type MaxActiveAddressesConfig added in v0.2.0

type MaxActiveAddressesConfig struct {
	MaxAddresses int `json:"max_addresses"`
}

MaxActiveAddressesConfig holds configuration for the max active addresses rule.

func NewMaxActiveAddressesConfig added in v0.2.0

func NewMaxActiveAddressesConfig(maxAddresses int) (MaxActiveAddressesConfig, error)

NewMaxActiveAddressesConfig validates and creates a MaxActiveAddressesConfig.

type MaxActiveAddressesRule added in v0.2.0

type MaxActiveAddressesRule struct {
	ID        RuleID
	DeviceID  device.DeviceID
	Enabled   bool
	Config    MaxActiveAddressesConfig
	CreatedAt time.Time
	UpdatedAt time.Time
}

MaxActiveAddressesRule represents the domain model for the max active addresses rule.

type Repository

type Repository struct {
	// contains filtered or unexported fields
}

Repository provides SQL-backed persistence for device rules.

func NewRepository

func NewRepository(db *sqlx.DB) *Repository

NewRepository creates a new Repository backed by the given sqlx.DB.

func (*Repository) DisableRule

func (r *Repository) DisableRule(ctx context.Context, deviceID device.DeviceID, ruleType RuleType) (*Rule, error)

DisableRule sets enabled=false for the rule identified by (device_id, rule_type).

func (*Repository) EnableDeviceAddressLeaseRuleConfig

func (r *Repository) EnableDeviceAddressLeaseRuleConfig(ctx context.Context, deviceID device.DeviceID, config DeviceAddressLeaseConfig) (*Rule, error)

EnableDeviceAddressLeaseRuleConfig creates or updates the device lease rule for a device using the structured params. It is responsible for mapping the config into the JSON shape stored in the database.

func (*Repository) EnableMaxActiveAddressesRuleConfig added in v0.2.0

func (r *Repository) EnableMaxActiveAddressesRuleConfig(ctx context.Context, deviceID device.DeviceID, config MaxActiveAddressesConfig) (*Rule, error)

EnableMaxActiveAddressesRuleConfig creates or updates the max active addresses rule for a device.

func (*Repository) GetRuleByDeviceAndType

func (r *Repository) GetRuleByDeviceAndType(ctx context.Context, deviceID device.DeviceID, ruleType RuleType) (*Rule, error)

GetRuleByDeviceAndType returns a single rule for the given device and type.

type Rule

type Rule struct {
	ID        RuleID          `db:"id"`
	DeviceID  device.DeviceID `db:"device_id"`
	RuleType  RuleType        `db:"rule_type"`
	Enabled   bool            `db:"enabled"`
	Config    json.RawMessage `db:"config"`
	CreatedAt time.Time       `db:"created_at"`
	UpdatedAt time.Time       `db:"updated_at"`
}

Rule maps to a row in the device_rules table.

func (*Rule) ToDeviceAddressLeaseRule

func (r *Rule) ToDeviceAddressLeaseRule() (*DeviceAddressLeaseRule, error)

ToDeviceAddressLeaseRule converts the raw Rule row into a DeviceAddressLeaseRule by parsing and validating the JSON config.

func (*Rule) ToMaxActiveAddressesRule added in v0.2.0

func (r *Rule) ToMaxActiveAddressesRule() (*MaxActiveAddressesRule, error)

ToMaxActiveAddressesRule converts the raw Rule row into a MaxActiveAddressesRule.

type RuleEvent

type RuleEvent struct {
	Type       RuleEventType
	DeviceID   device.DeviceID
	RuleType   RuleType
	TTLSeconds *int
	OccurredAt time.Time
}

type RuleEventType

type RuleEventType string
const (
	RuleEventTypeEnabled  RuleEventType = "rule_enabled"
	RuleEventTypeDisabled RuleEventType = "rule_disabled"
)

type RuleID

type RuleID int64

RuleID represents the primary key of a row in the device_rules table.

type RuleObserver

type RuleObserver interface {
	OnRuleEvent(ctx context.Context, event RuleEvent)
}

type RuleType

type RuleType string

RuleType is a string identifier for a rule kind (e.g. "device_lease").

const (
	// RuleTypeDeviceAddressLease controls automatic expiry of IP addresses for a device.
	RuleTypeDeviceAddressLease RuleType = "device_lease"

	// RuleTypeMaxActiveAddresses limits the number of simultaneously enabled IP addresses per device.
	RuleTypeMaxActiveAddresses RuleType = "max_active_addresses"
)

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(repo repository, logger *slog.Logger) *Service

func (*Service) AddRuleObserver

func (s *Service) AddRuleObserver(o RuleObserver)

func (*Service) DisableDeviceAddressLeaseRule

func (s *Service) DisableDeviceAddressLeaseRule(ctx context.Context, deviceID device.DeviceID) (*DeviceAddressLeaseRule, error)

DisableDeviceAddressLeaseRule sets enabled to false for the device lease rule for the device. Returns the updated rule or ErrRuleNotFound if no rule exists.

func (*Service) DisableMaxActiveAddressesRule added in v0.2.0

func (s *Service) DisableMaxActiveAddressesRule(ctx context.Context, deviceID device.DeviceID) (*MaxActiveAddressesRule, error)

DisableMaxActiveAddressesRule sets enabled to false for the max active addresses rule for the device.

func (*Service) EnableDeviceAddressLeaseRule

func (s *Service) EnableDeviceAddressLeaseRule(
	ctx context.Context,
	deviceID device.DeviceID,
	ttlSeconds int,
) (*DeviceAddressLeaseRule, error)

EnableDeviceAddressLeaseRule creates or updates the device lease rule for the device. ttlSeconds must be positive; enabled controls whether the rule is active.

func (*Service) EnableMaxActiveAddressesRule added in v0.2.0

func (s *Service) EnableMaxActiveAddressesRule(ctx context.Context, deviceID device.DeviceID, maxAddresses int) (*MaxActiveAddressesRule, error)

EnableMaxActiveAddressesRule creates or updates the max active addresses rule for the device.

func (*Service) GetDeviceAddressLeaseRule

func (s *Service) GetDeviceAddressLeaseRule(ctx context.Context, deviceID device.DeviceID) (*DeviceAddressLeaseRule, error)

GetDeviceAddressLeaseRule returns the device lease rule for the device, or ErrRuleNotFound if none exists. If the rule exists but has invalid config, returns ErrInvalidRuleConfig.

func (*Service) GetDeviceAddressLeaseTTLSeconds

func (s *Service) GetDeviceAddressLeaseTTLSeconds(ctx context.Context, deviceID device.DeviceID) (*int, error)

GetDeviceAddressLeaseTTLSeconds returns the TTL in seconds to apply for address leases for the given device, or nil if no active rule exists.

func (*Service) GetMaxActiveAddresses added in v0.2.0

func (s *Service) GetMaxActiveAddresses(ctx context.Context, deviceID device.DeviceID) (*int, error)

GetMaxActiveAddresses returns the maximum number of active addresses for the device, or nil if no active rule.

func (*Service) GetMaxActiveAddressesRule added in v0.2.0

func (s *Service) GetMaxActiveAddressesRule(ctx context.Context, deviceID device.DeviceID) (*MaxActiveAddressesRule, error)

GetMaxActiveAddressesRule returns the max active addresses rule for the device, or ErrRuleNotFound if none exists.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL