Skip to content

Commit

Permalink
start updating configs to match bb template [#346]
Browse files Browse the repository at this point in the history
  • Loading branch information
roberlander2 committed Jan 26, 2023
1 parent 70734dd commit 5c4ee7c
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 354 deletions.
31 changes: 30 additions & 1 deletion core/administration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

package core

import "groups/core/model"
import (
"groups/core/model"
"time"

"github.com/google/uuid"
)

func (app *Application) getTODO() error {
return nil
Expand All @@ -28,3 +33,27 @@ func (app *Application) getGroupsUnprotected(clientID string, filter model.Group

return groups, nil
}

func (app *Application) getManagedGroupConfigs(clientID string) ([]model.ManagedGroupConfig, error) {
return app.storage.FindManagedGroupConfigs(clientID)
}

func (app *Application) createManagedGroupConfig(config model.ManagedGroupConfig) (*model.ManagedGroupConfig, error) {
config.ID = uuid.NewString()
config.DateCreated = time.Now()
config.DateUpdated = nil
err := app.storage.InsertManagedGroupConfig(config)
return &config, err
}

func (app *Application) updateManagedGroupConfig(config model.ManagedGroupConfig) error {
return app.storage.UpdateManagedGroupConfig(config)
}

func (app *Application) deleteManagedGroupConfig(id string, clientID string) error {
return app.storage.DeleteManagedGroupConfig(id, clientID)
}

func (app *Application) getConfig(configType string, appID string, orgID string) (*model.Config, error) {
return app.storage.FindConfig(configType, appID, orgID)
}
4 changes: 2 additions & 2 deletions core/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ func (app *Application) setupSyncManagedGroupTimer() {

// NewApplication creates new Application
func NewApplication(version string, build string, storage Storage, notifications Notifications, authman Authman, core *corebb.Adapter,
rewards *rewards.Adapter, config *model.ApplicationConfig) *Application {
rewards *rewards.Adapter) *Application {

scheduler := cron.New(cron.WithLocation(time.UTC))
managedGroupTasks := map[string]scheduledTask{}
application := Application{version: version, build: build, storage: storage, notifications: notifications,
authman: authman, corebb: core, rewards: rewards, config: config, scheduler: scheduler, managedGroupTasks: managedGroupTasks}
authman: authman, corebb: core, rewards: rewards, scheduler: scheduler, managedGroupTasks: managedGroupTasks}

//add the drivers ports/interfaces
application.Services = &servicesImpl{app: &application}
Expand Down
65 changes: 31 additions & 34 deletions core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ type Services interface {
SynchronizeAuthman(clientID string) error
SynchronizeAuthmanGroup(clientID string, groupID string) error

GetManagedGroupConfigs(clientID string) ([]model.ManagedGroupConfig, error)
CreateManagedGroupConfig(config model.ManagedGroupConfig) (*model.ManagedGroupConfig, error)
UpdateManagedGroupConfig(config model.ManagedGroupConfig) error
DeleteManagedGroupConfig(id string, clientID string) error

GetSyncConfig(clientID string) (*model.SyncConfig, error)
UpdateSyncConfig(config model.SyncConfig) error

// V3
CheckUserGroupMembershipPermission(clientID string, current *model.User, groupID string) (*model.Group, bool)
FindGroupsV3(clientID string, filter model.GroupsFilter) ([]model.Group, error)
Expand Down Expand Up @@ -220,30 +212,6 @@ func (s *servicesImpl) SynchronizeAuthmanGroup(clientID string, groupID string)
return s.app.synchronizeAuthmanGroup(clientID, groupID)
}

func (s *servicesImpl) GetManagedGroupConfigs(clientID string) ([]model.ManagedGroupConfig, error) {
return s.app.getManagedGroupConfigs(clientID)
}

func (s *servicesImpl) CreateManagedGroupConfig(config model.ManagedGroupConfig) (*model.ManagedGroupConfig, error) {
return s.app.createManagedGroupConfig(config)
}

func (s *servicesImpl) UpdateManagedGroupConfig(config model.ManagedGroupConfig) error {
return s.app.updateManagedGroupConfig(config)
}

func (s *servicesImpl) DeleteManagedGroupConfig(id string, clientID string) error {
return s.app.deleteManagedGroupConfig(id, clientID)
}

func (s *servicesImpl) GetSyncConfig(clientID string) (*model.SyncConfig, error) {
return s.app.getSyncConfig(clientID)
}

func (s *servicesImpl) UpdateSyncConfig(config model.SyncConfig) error {
return s.app.updateSyncConfig(config)
}

// V3

func (s *servicesImpl) CheckUserGroupMembershipPermission(clientID string, current *model.User, groupID string) (*model.Group, bool) {
Expand Down Expand Up @@ -301,6 +269,16 @@ func (s *servicesImpl) GetResearchProfileUserCount(clientID string, current *mod
// Administration exposes administration APIs for the driver adapters
type Administration interface {
GetGroups(clientID string, filter model.GroupsFilter) ([]model.Group, error)

GetConfig(configType string, appID string, orgID string) (*model.Config, error)
CreateConfig(config model.Config) error
UpdateConfig(config model.Config) error
DeleteConfig(configType string, appID string, orgID string) error

GetManagedGroupConfigs(clientID string) ([]model.ManagedGroupConfig, error)
CreateManagedGroupConfig(config model.ManagedGroupConfig) (*model.ManagedGroupConfig, error)
UpdateManagedGroupConfig(config model.ManagedGroupConfig) error
DeleteManagedGroupConfig(id string, clientID string) error
}

type administrationImpl struct {
Expand All @@ -311,16 +289,35 @@ func (s *administrationImpl) GetGroups(clientID string, filter model.GroupsFilte
return s.app.getGroupsUnprotected(clientID, filter)
}

func (s *administrationImpl) GetManagedGroupConfigs(clientID string) ([]model.ManagedGroupConfig, error) {
return s.app.getManagedGroupConfigs(clientID)
}

func (s *administrationImpl) CreateManagedGroupConfig(config model.ManagedGroupConfig) (*model.ManagedGroupConfig, error) {
return s.app.createManagedGroupConfig(config)
}

func (s *administrationImpl) UpdateManagedGroupConfig(config model.ManagedGroupConfig) error {
return s.app.updateManagedGroupConfig(config)
}

func (s *administrationImpl) DeleteManagedGroupConfig(id string, clientID string) error {
return s.app.deleteManagedGroupConfig(id, clientID)
}

func (s *administrationImpl) GetConfig(configType string, appID string, orgID string) (*model.Config, error) {
return s.app.getConfig(configType, appID, orgID)
}

// Storage is used by corebb to storage data - DB storage adapter, file storage adapter etc
type Storage interface {
RegisterStorageListener(listener storage.Listener)

PerformTransaction(transaction func(context storage.TransactionContext) error) error

FindConfig(configType string, appID string, orgID string) (*model.Config, error)
LoadSyncConfigs(context storage.TransactionContext) ([]model.SyncConfig, error)
FindSyncConfigs() ([]model.SyncConfig, error)
FindSyncConfig(clientID string) (*model.SyncConfig, error)
SaveSyncConfig(context storage.TransactionContext, config model.SyncConfig) error

FindSyncTimes(context storage.TransactionContext, clientID string) (*model.SyncTimes, error)
SaveSyncTimes(context storage.TransactionContext, times model.SyncTimes) error
Expand Down
111 changes: 93 additions & 18 deletions core/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,97 @@

package model

import "time"
import (
"time"

// ApplicationConfig wrapper for in memory storage of configuration
type ApplicationConfig struct {
AuthmanAdminUINList []string
ReportAbuseRecipientEmail string
SupportedClientIDs []string
AppID string
OrgID string
"github.com/rokwire/logging-library-go/v2/errors"
"github.com/rokwire/logging-library-go/v2/logutils"
)

const (
// TypeConfig configs type
TypeConfig logutils.MessageDataType = "config"
// TypeEnvConfigData env configs type
TypeEnvConfigData logutils.MessageDataType = "env config data"
// TypeApplicationConfigData application configs type
TypeApplicationConfigData logutils.MessageDataType = "application config data"
// TypeSyncConfigData sync configs type
TypeSyncConfigData logutils.MessageDataType = "sync config data"
// TypeManagedGroupConfigData managed group configs type
TypeManagedGroupConfigData logutils.MessageDataType = "managed group config data"

// ConfigIDEnv is the Config ID for EnvConfigData
ConfigIDEnv string = "env"
// ConfigIDApplication is the Config ID for ApplicationConfigData
ConfigIDApplication string = "application"
// ConfigIDSync is the Config ID for SyncConfigData
ConfigIDSync string = "sync"
// ConfigIDManagedGroup is the Config ID for ManagedGroupConfigData
ConfigIDManagedGroup string = "managed_group"
)

// Config contain generic configs
type Config struct {
ID string `json:"id" bson:"_id"`
Type string `json:"type" bson:"type"`
AppID string `json:"app_id" bson:"app_id"`
OrgID string `json:"org_id" bson:"org_id"`
System bool `json:"system" bson:"system"`
Data interface{} `json:"data" bson:"data"`
DateCreated time.Time `json:"date_created" bson:"date_created"`
DateUpdated *time.Time `json:"date_updated" bson:"date_updated"`
}

// DataAsEnvConfig returns the config Data as an EnvConfigData if the cast succeeds
func (c Config) DataAsEnvConfig() (*EnvConfigData, error) {
data, ok := c.Data.(EnvConfigData)
if !ok {
return nil, errors.ErrorData(logutils.StatusInvalid, TypeEnvConfigData, nil)
}
return &data, nil
}

// DataAsApplicationConfig returns the config Data as an ApplicationConfigData if the cast succeeds
func (c Config) DataAsApplicationConfig() (*ApplicationConfigData, error) {
data, ok := c.Data.(ApplicationConfigData)
if !ok {
return nil, errors.ErrorData(logutils.StatusInvalid, TypeEnvConfigData, nil)
}
return &data, nil
}

// DataAsSyncConfig returns the config Data as a SyncConfigData if the cast succeeds
func (c Config) DataAsSyncConfig() (*SyncConfigData, error) {
data, ok := c.Data.(SyncConfigData)
if !ok {
return nil, errors.ErrorData(logutils.StatusInvalid, TypeSyncConfigData, nil)
}
return &data, nil
}

// DataAsManagedGroupConfig returns the config Data as a ManagedGroupConfigData if the cast succeeds
func (c Config) DataAsManagedGroupConfig() (*ManagedGroupConfigData, error) {
data, ok := c.Data.(ManagedGroupConfigData)
if !ok {
return nil, errors.ErrorData(logutils.StatusInvalid, TypeManagedGroupConfigData, nil)
}
return &data, nil
}

// EnvConfigData contains environment configs for this service
type EnvConfigData struct {
ExampleEnv string `json:"example_env" bson:"example_env"`
}

// ApplicationConfigData defines configs for managing authman sync and sending report emails
type ApplicationConfigData struct {
//TODO: must not be empty
AuthmanAdminUINList []string `json:"authman_admin_uin_list" bson:"authman_admin_uin_list"`
ReportAbuseRecipientEmail string `json:"report_abuse_recipient_email" bson:"report_abuse_recipient_email"`
}

// SyncConfig defines system configs for managed group sync
type SyncConfig struct {
Type string `json:"type" bson:"type"`
ClientID string `json:"client_id" bson:"client_id"`
// SyncConfigData defines system configs for managed group sync
type SyncConfigData struct {
CRON string `json:"cron" bson:"cron"`
TimeThreshold int `json:"time_threshold" bson:"time_threshold"` // Threshold from start_time to be considered same run in minutes
Timeout int `json:"timeout" bson:"timeout"` // Time from start_time to be considered a failed run in minutes
Expand All @@ -37,18 +113,17 @@ type SyncConfig struct {

// SyncTimes defines the times used to prevent concurrent syncs
type SyncTimes struct {
ClientID string `json:"client_id" bson:"client_id"`
AppID string `json:"app_id" bson:"app_id"`
OrgID string `json:"org_id" bson:"org_id"`
StartTime *time.Time `json:"start_time" bson:"start_time"`
EndTime *time.Time `json:"end_time" bson:"end_time"`
}

// ManagedGroupConfig defines a config for a set of managed groups
type ManagedGroupConfig struct {
ID string `json:"id" bson:"_id"`
ClientID string `json:"client_id" bson:"client_id"`
// ManagedGroupConfigData defines a config for a set of managed groups
type ManagedGroupConfigData struct {
AuthmanStems []string `json:"authman_stems" bson:"authman_stems"`
AdminUINs []string `json:"admin_uins" bson:"admin_uins"`
Type string `json:"type" bson:"type"`
DateCreated time.Time `json:"date_created" bson:"date_created"`
DateUpdated *time.Time `json:"date_updated" bson:"date_updated"`
} //@name ManagedGroupConfig
} //@name ManagedGroupConfigData
28 changes: 0 additions & 28 deletions core/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -1198,34 +1198,6 @@ func (app *Application) sendNotification(recipients []notifications.Recipient, t
app.notifications.SendNotification(recipients, topic, title, text, data, nil, appID, orgID)
}

func (app *Application) getManagedGroupConfigs(clientID string) ([]model.ManagedGroupConfig, error) {
return app.storage.FindManagedGroupConfigs(clientID)
}

func (app *Application) createManagedGroupConfig(config model.ManagedGroupConfig) (*model.ManagedGroupConfig, error) {
config.ID = uuid.NewString()
config.DateCreated = time.Now()
config.DateUpdated = nil
err := app.storage.InsertManagedGroupConfig(config)
return &config, err
}

func (app *Application) updateManagedGroupConfig(config model.ManagedGroupConfig) error {
return app.storage.UpdateManagedGroupConfig(config)
}

func (app *Application) deleteManagedGroupConfig(id string, clientID string) error {
return app.storage.DeleteManagedGroupConfig(id, clientID)
}

func (app *Application) getSyncConfig(clientID string) (*model.SyncConfig, error) {
return app.storage.FindSyncConfig(clientID)
}

func (app *Application) updateSyncConfig(config model.SyncConfig) error {
return app.storage.SaveSyncConfig(nil, config)
}

func (app *Application) findGroupMembership(clientID string, groupID string, userID string) (*model.GroupMembership, error) {
return app.storage.FindGroupMembership(clientID, groupID, userID)
}
Expand Down
Loading

0 comments on commit 5c4ee7c

Please sign in to comment.