Files
threshold/internal/host/preferred_endpoint.go
shadowdaoandClaude Sonnet 5.5 962d708fdd Rename Go module paths to repo.anhonesthost.net/CyberCoveLLC/threshold
Rename root, api and sdk module paths (imports, go.mod, go_package,
ldflags, build scripts, buf configs) and regenerate protobuf/gateway
code with the project's pinned toolchain. Wire identifiers unchanged.
api gets a local replace for sdk; root/api/sdk require v0.1.0.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 10:33:41 -07:00

91 lines
2.7 KiB
Go

// Copyright IBM Corp. 2020, 2026
// SPDX-License-Identifier: BUSL-1.1
package host
import (
"context"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/errors"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/host/store"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/libs/endpoint"
"google.golang.org/protobuf/proto"
)
// defaultPreferredEndpointTableName defines the default table name for a
// PreferredEndpoint
const defaultPreferredEndpointTableName = "host_set_preferred_endpoint"
// PreferredEndpoint defines an endpoint condition for a host set.
// PreferredEndpoints are value objects of an HostSet, therefore there's no need
// for oplog metadata, since only the HostSet will have metadata because it's
// the root aggregate.
type PreferredEndpoint struct {
*store.PreferredEndpoint
tableName string
}
// NewPreferredEndpoint creates a new in memory preferred endpoint assigned to a
// HostSet. It supports no options.
func NewPreferredEndpoint(ctx context.Context, hostSetId string, priority uint32, condition string) (*PreferredEndpoint, error) {
const op = "host.NewPreferredEndpoint"
pe := &PreferredEndpoint{
PreferredEndpoint: &store.PreferredEndpoint{
HostSetId: hostSetId,
Priority: priority,
Condition: condition,
},
}
if err := pe.validate(ctx, op); err != nil {
return nil, err // intentionally not wrapped
}
return pe, nil
}
// validate the PreferredEndpoint. On success, it will return nil.
func (pe *PreferredEndpoint) validate(ctx context.Context, caller errors.Op) error {
if pe.HostSetId == "" {
return errors.New(ctx, errors.InvalidParameter, caller, "missing host set id")
}
if pe.Priority < 1 {
return errors.New(ctx, errors.InvalidParameter, caller, "invalid priority value")
}
if pe.Condition == "" {
return errors.New(ctx, errors.InvalidParameter, caller, "missing condition")
}
if _, err := endpoint.NewPreferencer(ctx, endpoint.WithPreferenceOrder([]string{pe.Condition})); err != nil {
return errors.Wrap(ctx, err, caller)
}
return nil
}
// AllocPreferredEndpoint makes an empty one in-memory
func AllocPreferredEndpoint() *PreferredEndpoint {
return &PreferredEndpoint{
PreferredEndpoint: &store.PreferredEndpoint{},
}
}
// Clone a PreferredEndpoint
func (pe *PreferredEndpoint) Clone() *PreferredEndpoint {
cp := proto.Clone(pe.PreferredEndpoint)
return &PreferredEndpoint{
PreferredEndpoint: cp.(*store.PreferredEndpoint),
}
}
// TableName returns the table name.
func (pe *PreferredEndpoint) TableName() string {
if pe.tableName != "" {
return pe.tableName
}
return defaultPreferredEndpointTableName
}
// SetTableName sets the table name.
func (pe *PreferredEndpoint) SetTableName(n string) {
pe.tableName = n
}