Documentation
¶
Index ¶
- Constants
- Variables
- type DeviceAddressLeaseConfig
- type DeviceAddressLeaseRule
- type HTTPHandler
- func (h *HTTPHandler) DisableDeviceAddressLeaseRule(ctx context.Context, ...) (httpapi.DisableDeviceAddressLeaseRuleResponseObject, error)
- func (h *HTTPHandler) DisableMaxActiveAddressesRule(ctx context.Context, ...) (httpapi.DisableMaxActiveAddressesRuleResponseObject, error)
- func (h *HTTPHandler) GetDeviceAddressLeaseRule(ctx context.Context, request httpapi.GetDeviceAddressLeaseRuleRequestObject) (httpapi.GetDeviceAddressLeaseRuleResponseObject, error)
- func (h *HTTPHandler) GetMaxActiveAddressesRule(ctx context.Context, request httpapi.GetMaxActiveAddressesRuleRequestObject) (httpapi.GetMaxActiveAddressesRuleResponseObject, error)
- func (h *HTTPHandler) PutDeviceAddressLeaseRule(ctx context.Context, request httpapi.PutDeviceAddressLeaseRuleRequestObject) (httpapi.PutDeviceAddressLeaseRuleResponseObject, error)
- func (h *HTTPHandler) PutMaxActiveAddressesRule(ctx context.Context, request httpapi.PutMaxActiveAddressesRuleRequestObject) (httpapi.PutMaxActiveAddressesRuleResponseObject, error)
- type MaxActiveAddressesConfig
- type MaxActiveAddressesRule
- type Repository
- func (r *Repository) CountEnabledAddresses(ctx context.Context, deviceID ids.DeviceID) (int, error)
- func (r *Repository) DisableRule(ctx context.Context, deviceID ids.DeviceID, ruleType RuleType) (*Rule, error)
- func (r *Repository) EnableDeviceAddressLeaseRuleConfig(ctx context.Context, deviceID ids.DeviceID, config DeviceAddressLeaseConfig) (*Rule, error)
- func (r *Repository) EnableMaxActiveAddressesRuleConfig(ctx context.Context, deviceID ids.DeviceID, config MaxActiveAddressesConfig) (*Rule, error)
- func (r *Repository) GetRuleByDeviceAndType(ctx context.Context, deviceID ids.DeviceID, ruleType RuleType) (*Rule, error)
- func (r *Repository) RulesForDevices(ctx context.Context, deviceIDs []ids.DeviceID) (map[ids.DeviceID][]httpapi.DeviceRuleSummary, error)
- type Rule
- type RuleEvent
- type RuleEventType
- type RuleObserver
- type RuleType
- type Service
- func (s *Service) AddRuleObserver(o RuleObserver)
- func (s *Service) DisableDeviceAddressLeaseRule(ctx context.Context, deviceID ids.DeviceID) (*DeviceAddressLeaseRule, error)
- func (s *Service) DisableMaxActiveAddressesRule(ctx context.Context, deviceID ids.DeviceID) (*MaxActiveAddressesRule, error)
- func (s *Service) EnableDeviceAddressLeaseRule(ctx context.Context, deviceID ids.DeviceID, ttlSeconds int) (*DeviceAddressLeaseRule, error)
- func (s *Service) EnableMaxActiveAddressesRule(ctx context.Context, deviceID ids.DeviceID, maxAddresses int) (*MaxActiveAddressesRule, error)
- func (s *Service) GetDeviceAddressLeaseRule(ctx context.Context, deviceID ids.DeviceID) (*DeviceAddressLeaseRule, error)
- func (s *Service) GetDeviceAddressLeaseTTLSeconds(ctx context.Context, deviceID ids.DeviceID) (*int, error)
- func (s *Service) GetMaxActiveAddresses(ctx context.Context, deviceID ids.DeviceID) (*int, error)
- func (s *Service) GetMaxActiveAddressesRule(ctx context.Context, deviceID ids.DeviceID) (*MaxActiveAddressesRule, error)
Constants ¶
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 ¶
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 ids.RuleID
DeviceID ids.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 ¶
func (h *HTTPHandler) DisableDeviceAddressLeaseRule(ctx context.Context, request httpapi.DisableDeviceAddressLeaseRuleRequestObject) (httpapi.DisableDeviceAddressLeaseRuleResponseObject, error)
DisableDeviceAddressLeaseRule disables the device address lease rule for the device.
func (*HTTPHandler) DisableMaxActiveAddressesRule ¶ added in v0.2.0
func (h *HTTPHandler) DisableMaxActiveAddressesRule(ctx context.Context, request httpapi.DisableMaxActiveAddressesRuleRequestObject) (httpapi.DisableMaxActiveAddressesRuleResponseObject, error)
DisableMaxActiveAddressesRule disables the max active addresses rule for the device.
func (*HTTPHandler) GetDeviceAddressLeaseRule ¶
func (h *HTTPHandler) GetDeviceAddressLeaseRule(ctx context.Context, request httpapi.GetDeviceAddressLeaseRuleRequestObject) (httpapi.GetDeviceAddressLeaseRuleResponseObject, error)
GetDeviceAddressLeaseRule returns the device address lease rule for the device.
func (*HTTPHandler) GetMaxActiveAddressesRule ¶ added in v0.2.0
func (h *HTTPHandler) GetMaxActiveAddressesRule(ctx context.Context, request httpapi.GetMaxActiveAddressesRuleRequestObject) (httpapi.GetMaxActiveAddressesRuleResponseObject, error)
GetMaxActiveAddressesRule returns the max active addresses rule for the device.
func (*HTTPHandler) PutDeviceAddressLeaseRule ¶
func (h *HTTPHandler) PutDeviceAddressLeaseRule(ctx context.Context, request httpapi.PutDeviceAddressLeaseRuleRequestObject) (httpapi.PutDeviceAddressLeaseRuleResponseObject, error)
PutDeviceAddressLeaseRule creates or updates the device address lease rule for the device.
func (*HTTPHandler) PutMaxActiveAddressesRule ¶ added in v0.2.0
func (h *HTTPHandler) PutMaxActiveAddressesRule(ctx context.Context, request httpapi.PutMaxActiveAddressesRuleRequestObject) (httpapi.PutMaxActiveAddressesRuleResponseObject, error)
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 ids.RuleID
DeviceID ids.DeviceID
Enabled bool
Config MaxActiveAddressesConfig
CreatedAt time.Time
UpdatedAt time.Time
// ActiveAddressCount is the number of currently enabled addresses for the device,
// regardless of whether this rule is configured. Populated by the service from the
// address domain's live count — see max_active_addresses_view.go.
ActiveAddressCount int
}
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 *database.DB) *Repository
NewRepository creates a new Repository backed by the given sqlx.DB.
func (*Repository) CountEnabledAddresses ¶ added in v0.6.0
CountEnabledAddresses returns the number of currently enabled addresses for a device. Read-only join against the device domain's addresses table, isolated here per ADR-009 so a schema change there is greppable: `git grep -l 'FROM addresses' -- '**/*_view.go'`.
func (*Repository) DisableRule ¶
func (r *Repository) DisableRule(ctx context.Context, deviceID ids.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 ids.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 ids.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 ids.DeviceID, ruleType RuleType) (*Rule, error)
GetRuleByDeviceAndType returns a single rule for the given device and type.
func (*Repository) RulesForDevices ¶ added in v0.6.0
func (r *Repository) RulesForDevices(ctx context.Context, deviceIDs []ids.DeviceID) (map[ids.DeviceID][]httpapi.DeviceRuleSummary, error)
RulesForDevices returns the configured rules of the given devices, keyed by device id and ordered by rule type. Devices with no configured rules are absent from the map rather than mapped to an empty slice.
type Rule ¶
type Rule struct {
ID ids.RuleID `db:"id"`
DeviceID ids.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 RuleEventType ¶
type RuleEventType string
const ( RuleEventTypeEnabled RuleEventType = "rule_enabled" RuleEventTypeDisabled RuleEventType = "rule_disabled" )
type RuleObserver ¶
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 (*Service) AddRuleObserver ¶
func (s *Service) AddRuleObserver(o RuleObserver)
func (*Service) DisableDeviceAddressLeaseRule ¶
func (s *Service) DisableDeviceAddressLeaseRule(ctx context.Context, deviceID ids.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 ids.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 ids.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 ids.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 ids.DeviceID) (*DeviceAddressLeaseRule, error)
GetDeviceAddressLeaseRule returns the device lease rule for the device. If no row exists, returns a disabled default (enabled=false, no config). If the rule exists but has invalid config, returns ErrInvalidRuleConfig.
func (*Service) GetDeviceAddressLeaseTTLSeconds ¶
func (s *Service) GetDeviceAddressLeaseTTLSeconds(ctx context.Context, deviceID ids.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
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 ids.DeviceID) (*MaxActiveAddressesRule, error)
GetMaxActiveAddressesRule returns the max active addresses rule for the device, along with the device's current live address count. If no row exists, returns a disabled default (enabled=false, no config).