109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go.infratographer.com/x/events"
|
|
"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"
|
|
)
|
|
|
|
const (
|
|
// TypeOrganization defines the organization type.
|
|
TypeOrganization = "organization"
|
|
|
|
// TypeProject defines the project type.
|
|
TypeProject = "project"
|
|
|
|
// TypeUser defines the user type.
|
|
TypeUser = "user"
|
|
)
|
|
|
|
// DefaultPrefixMap is the default id prefix to type relationship.
|
|
var DefaultPrefixMap = map[string]string{
|
|
"metlorg": TypeOrganization,
|
|
"metlprj": TypeProject,
|
|
"metlusr": TypeUser,
|
|
}
|
|
|
|
// Service defines a bridge service methods
|
|
type Service interface {
|
|
// IsOrganizationID checks if the provided id has an id prefix which is an organization.
|
|
IsOrganizationID(id gidx.PrefixedID) bool
|
|
// TouchOrganization triggers a sync of an organization.
|
|
TouchOrganization(ctx context.Context, id gidx.PrefixedID) error
|
|
// DeleteOrganization deletes an organization and all of its resources.
|
|
DeleteOrganization(ctx context.Context, id gidx.PrefixedID) error
|
|
|
|
// IsProjectID checks if the provided id has an id prefix which is a project.
|
|
IsProjectID(id gidx.PrefixedID) bool
|
|
// TouchProject triggers a sync of an organization
|
|
TouchProject(ctx context.Context, id gidx.PrefixedID) error
|
|
// DeleteProject deletes the project and all of its resources.
|
|
DeleteProject(ctx context.Context, id gidx.PrefixedID) error
|
|
|
|
// IsUser checks if the provided id has an id prefix which is a user.
|
|
IsUser(id gidx.PrefixedID) bool
|
|
// TouchUser triggers a sync of a user and their permissions.
|
|
TouchUser(ctx context.Context, id gidx.PrefixedID) error
|
|
// DeleteUser deletes the user and their permissions.
|
|
DeleteUser(ctx context.Context, id gidx.PrefixedID) error
|
|
}
|
|
|
|
var _ Service = &service{}
|
|
|
|
type service struct {
|
|
logger *zap.SugaredLogger
|
|
publisher *events.Publisher
|
|
metal *metal.Client
|
|
perms *permissions.Client
|
|
idPrefixMap map[string]string
|
|
|
|
rootResource rootResource
|
|
roles map[string][]string
|
|
}
|
|
|
|
type rootResource struct {
|
|
id gidx.PrefixedID
|
|
}
|
|
|
|
func (r rootResource) PrefixedID() gidx.PrefixedID {
|
|
return r.id
|
|
}
|
|
|
|
func New(publisher *events.Publisher, metal *metal.Client, perms *permissions.Client, options ...Option) (Service, error) {
|
|
svc := &service{
|
|
publisher: publisher,
|
|
metal: metal,
|
|
perms: perms,
|
|
idPrefixMap: make(map[string]string),
|
|
}
|
|
|
|
for _, opt := range options {
|
|
if err := opt(svc); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if svc.logger == nil {
|
|
svc.logger = zap.NewNop().Sugar()
|
|
}
|
|
|
|
if svc.rootResource.PrefixedID() == gidx.NullPrefixedID {
|
|
return nil, ErrRootTenantRequired
|
|
}
|
|
|
|
if svc.idPrefixMap == nil || len(svc.idPrefixMap) == 0 {
|
|
svc.idPrefixMap = DefaultPrefixMap
|
|
}
|
|
|
|
if svc.roles == nil {
|
|
svc.roles = make(map[string][]string)
|
|
}
|
|
|
|
return svc, nil
|
|
}
|