98 lines
2.0 KiB
Go
98 lines
2.0 KiB
Go
package service
|
|
|
|
import (
|
|
"go.infratographer.com/x/gidx"
|
|
"go.uber.org/zap"
|
|
"golang.org/x/exp/slices"
|
|
|
|
"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 = make(map[string][]string, len(roles))
|
|
|
|
for role, actions := range roles {
|
|
slices.Sort(actions)
|
|
|
|
s.roles[role] = actions
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|