Newer
Older
Andre Sterba
committed
package app
import (
Andre Sterba
committed
"fmt"
Fabian Seidl
committed
"code.fbi.h-da.de/danet/gosdn/controller/config"
Andre Sterba
committed
"code.fbi.h-da.de/danet/gosdn/controller/store"
"github.com/google/uuid"
)
// ManagementService is the app service.
type ManagementService interface {
Register(string, string) (*App, error)
Deregister(string) error
}
// Service manages apps and their access to the event system.
type Service struct {
store ManagementStore
}
// NewAppService creates a new app service.
func NewAppService(store ManagementStore) ManagementService {
return &Service{
store: store,
}
}
// Register checks if the app already exists and if not creates a new one.
func (a *Service) Register(appName, token string) (*App, error) {
Andre Sterba
committed
if token != "SecurePresharedToken" {
return nil, fmt.Errorf("token not valid")
}
exisitingApp, err := a.store.Get(ctx, store.Query{ID: uuid.Nil, Name: appName})
Andre Sterba
committed
if err != nil {
if exisitingApp.ID == uuid.Nil {
return a.createNewApp(appName)
}
return nil, err
}
return &exisitingApp, nil
}
// Deregister deregisters an app.
func (a *Service) Deregister(appName string) error {
ctx := context.Background()
app, err := a.store.Get(ctx, store.Query{Name: appName})
Andre Sterba
committed
if err != nil {
return err
}
Andre Sterba
committed
if err != nil {
return err
}
return nil
}
func (a *Service) createNewApp(appName string) (*App, error) {
Andre Sterba
committed
app := App{
ID: uuid.New(),
Name: appName,
Fabian Seidl
committed
EventSystemCredentials: buildQueueCredentials(),
Andre Sterba
committed
}
// generate app credentials
Andre Sterba
committed
if err != nil {
return nil, err
}
return &app, nil
}
Fabian Seidl
committed
func buildQueueCredentials() string {
return fmt.Sprintf("%s%s:%s@%s:%s/", config.AMQPPrefix, config.AMQPUser, config.AMQPPassword, config.AMQPHost, config.AMQPPort)
}