Files
bridge/internal/service/options.go

91 lines
1.8 KiB
Go

package service
import (
"go.infratographer.com/x/gidx"
"go.uber.org/zap"
"go.equinixmetal.net/infra9-metal-bridge/internal/metal"
"go.equinixmetal.net/infra9-metal-bridge/internal/permissions"
)
// Option defines a service option.
type Option func(s *service) error
// WithLogger sets the logger for the service handler.
func WithLogger(logger *zap.SugaredLogger) Option {
return func(s *service) error {
s.logger = logger
return nil
}
}
// WithMetalClient sets the Equinix Metal client used by the service.
func WithMetalClient(client *metal.Client) Option {
return func(s *service) error {
s.metal = client
return nil
}
}
// WithPermissionsClient sets the permissions client used by the service.
func WithPermissionsClient(client *permissions.Client) Option {
return func(s *service) error {
s.perms = client
return nil
}
}
// WithPrefixMap sets the id prefix map relating id prefixes to type names.
func WithPrefixMap(idMap map[string]ObjectType) Option {
return func(s *service) error {
s.idPrefixMap = idMap
return nil
}
}
// WithRootTenant sets the root tenant referenced in organization relationships.
func WithRootTenant(sid string) Option {
return func(s *service) error {
id, err := gidx.Parse(sid)
if err != nil {
return err
}
s.rootResource = prefixedID{id}
return nil
}
}
// WithRoles defines the role to action mapping.
func WithRoles(roles map[string][]string) Option {
return func(s *service) error {
s.roles = roles
return nil
}
}
// WithConfig applies all configurations defined in the config.
func WithConfig(config Config) Option {
return func(s *service) error {
var options []Option
if config.RootTenant != "" {
options = append(options, WithRootTenant(config.RootTenant))
}
for _, opt := range options {
if err := opt(s); err != nil {
return err
}
}
return nil
}
}