rework initialization logic

Signed-off-by: jkoberg <jkoberg@owncloud.com>
This commit is contained in:
jkoberg
2022-02-28 17:15:24 +01:00
parent 4c22c07711
commit 492419ec7b
9 changed files with 64 additions and 68 deletions
+3 -3
View File
@@ -10,7 +10,7 @@ import (
)
// ListRoleAssignments loads and returns all role assignments matching the given assignment identifier.
func (s Store) ListRoleAssignments(accountUUID string) ([]*settingsmsg.UserRoleAssignment, error) {
func (s *Store) ListRoleAssignments(accountUUID string) ([]*settingsmsg.UserRoleAssignment, error) {
s.Init()
assIDs, err := s.mdc.ReadDir(nil, accountPath(accountUUID))
if err != nil {
@@ -36,7 +36,7 @@ func (s Store) ListRoleAssignments(accountUUID string) ([]*settingsmsg.UserRoleA
}
// WriteRoleAssignment appends the given role assignment to the existing assignments of the respective account.
func (s Store) WriteRoleAssignment(accountUUID, roleID string) (*settingsmsg.UserRoleAssignment, error) {
func (s *Store) WriteRoleAssignment(accountUUID, roleID string) (*settingsmsg.UserRoleAssignment, error) {
s.Init()
// as per https://github.com/owncloud/product/issues/103 "Each user can have exactly one role"
err := s.mdc.Delete(nil, accountPath(accountUUID))
@@ -62,7 +62,7 @@ func (s Store) WriteRoleAssignment(accountUUID, roleID string) (*settingsmsg.Use
}
// RemoveRoleAssignment deletes the given role assignment from the existing assignments of the respective account.
func (s Store) RemoveRoleAssignment(assignmentID string) error {
func (s *Store) RemoveRoleAssignment(assignmentID string) error {
s.Init()
accounts, err := s.mdc.ReadDir(nil, accountsFolderLocation)
if err != nil {
@@ -14,19 +14,16 @@ var (
einstein = "a4d07560-a670-4be9-8d60-9b547751a208"
//marie = "3c054db3-eec1-4ca4-b985-bc56dcf560cb"
s = Store{
Logger: logger,
mdc: mdc,
l: &sync.Mutex{},
s = &Store{
l: &sync.Mutex{},
}
logger = olog.NewLogger(
olog.Color(true),
olog.Pretty(true),
olog.Level("info"),
)
mdc = NewMDC()
mdc = NewMDC(s)
bundles = []*settingsmsg.Bundle{
{
+6 -6
View File
@@ -11,7 +11,7 @@ import (
)
// ListBundles returns all bundles in the dataPath folder that match the given type.
func (s Store) ListBundles(bundleType settingsmsg.Bundle_Type, bundleIDs []string) ([]*settingsmsg.Bundle, error) {
func (s *Store) ListBundles(bundleType settingsmsg.Bundle_Type, bundleIDs []string) ([]*settingsmsg.Bundle, error) {
// TODO: this is needed for initialization - we need to find a better way to fix this
if s.mdc == nil && len(bundleIDs) == 1 && bundleIDs[0] == "71881883-1768-46bd-a24d-a356a2afdf7f" {
return []*settingsmsg.Bundle{{
@@ -59,7 +59,7 @@ func (s Store) ListBundles(bundleType settingsmsg.Bundle_Type, bundleIDs []strin
}
// ReadBundle tries to find a bundle by the given id within the dataPath.
func (s Store) ReadBundle(bundleID string) (*settingsmsg.Bundle, error) {
func (s *Store) ReadBundle(bundleID string) (*settingsmsg.Bundle, error) {
s.Init()
b, err := s.mdc.SimpleDownload(nil, bundlePath(bundleID))
if err != nil {
@@ -71,12 +71,12 @@ func (s Store) ReadBundle(bundleID string) (*settingsmsg.Bundle, error) {
}
// ReadSetting tries to find a setting by the given id within the dataPath.
func (s Store) ReadSetting(settingID string) (*settingsmsg.Setting, error) {
func (s *Store) ReadSetting(settingID string) (*settingsmsg.Setting, error) {
return nil, errors.New("not implemented")
}
// WriteBundle sends the givens record to the metadataclient. returns `record` for legacy reasons
func (s Store) WriteBundle(record *settingsmsg.Bundle) (*settingsmsg.Bundle, error) {
func (s *Store) WriteBundle(record *settingsmsg.Bundle) (*settingsmsg.Bundle, error) {
s.Init()
b, err := json.Marshal(record)
if err != nil {
@@ -86,7 +86,7 @@ func (s Store) WriteBundle(record *settingsmsg.Bundle) (*settingsmsg.Bundle, err
}
// AddSettingToBundle adds the given setting to the bundle with the given bundleID.
func (s Store) AddSettingToBundle(bundleID string, setting *settingsmsg.Setting) (*settingsmsg.Setting, error) {
func (s *Store) AddSettingToBundle(bundleID string, setting *settingsmsg.Setting) (*settingsmsg.Setting, error) {
s.Init()
b, err := s.ReadBundle(bundleID)
if err != nil {
@@ -106,7 +106,7 @@ func (s Store) AddSettingToBundle(bundleID string, setting *settingsmsg.Setting)
}
// RemoveSettingFromBundle removes the setting from the bundle with the given ids.
func (s Store) RemoveSettingFromBundle(bundleID string, settingID string) error {
func (s *Store) RemoveSettingFromBundle(bundleID string, settingID string) error {
return errors.New("not implemented")
}
+21 -23
View File
@@ -1,10 +1,9 @@
package store
import (
"sync"
"testing"
olog "github.com/owncloud/ocis/ocis-pkg/log"
"github.com/gofrs/uuid"
settingsmsg "github.com/owncloud/ocis/protogen/gen/ocis/messages/settings/v0"
"github.com/stretchr/testify/require"
)
@@ -103,7 +102,7 @@ var bundleScenarios = []struct {
}
var (
appendTestBundleID = "append-test-bundle"
appendTestBundleID = uuid.Must(uuid.NewV4()).String()
appendTestSetting1 = &settingsmsg.Setting{
Id: "append-test-setting-1",
@@ -139,17 +138,16 @@ var (
)
func TestBundles(t *testing.T) {
mdc := NewMDC()
s := Store{
Logger: olog.NewLogger(
olog.Color(true),
olog.Pretty(true),
olog.Level("info"),
),
//s := &Store{
//Logger: olog.NewLogger(
//olog.Color(true),
//olog.Pretty(true),
//olog.Level("info"),
//),
l: &sync.Mutex{},
mdc: mdc,
}
//l: &sync.Mutex{},
//}
//NewMDC(s)
// write bundles
for i := range bundleScenarios {
@@ -187,17 +185,17 @@ func TestBundles(t *testing.T) {
}
func TestAppendSetting(t *testing.T) {
mdc := NewMDC()
s := Store{
Logger: olog.NewLogger(
olog.Color(true),
olog.Pretty(true),
olog.Level("info"),
),
//mdc := NewMDC()
//s := Store{
//Logger: olog.NewLogger(
//olog.Color(true),
//olog.Pretty(true),
//olog.Level("info"),
//),
l: &sync.Mutex{},
mdc: mdc,
}
//l: &sync.Mutex{},
//mdc: mdc,
//}
// appending to non existing bundle creates new
_, err := s.AddSettingToBundle(appendTestBundleID, appendTestSetting1)
+3 -3
View File
@@ -7,16 +7,16 @@ import (
)
// ListPermissionsByResource collects all permissions from the provided roleIDs that match the requested resource
func (s Store) ListPermissionsByResource(resource *settingsmsg.Resource, roleIDs []string) ([]*settingsmsg.Permission, error) {
func (s *Store) ListPermissionsByResource(resource *settingsmsg.Resource, roleIDs []string) ([]*settingsmsg.Permission, error) {
return nil, errors.New("not implemented")
}
// ReadPermissionByID finds the permission in the roles, specified by the provided roleIDs
func (s Store) ReadPermissionByID(permissionID string, roleIDs []string) (*settingsmsg.Permission, error) {
func (s *Store) ReadPermissionByID(permissionID string, roleIDs []string) (*settingsmsg.Permission, error) {
return nil, errors.New("not implemented")
}
// ReadPermissionByName finds the permission in the roles, specified by the provided roleIDs
func (s Store) ReadPermissionByName(name string, roleIDs []string) (*settingsmsg.Permission, error) {
func (s *Store) ReadPermissionByName(name string, roleIDs []string) (*settingsmsg.Permission, error) {
return nil, errors.New("not implemented")
}
+10 -10
View File
@@ -46,7 +46,7 @@ type Store struct {
}
// Init initialize the store once, later calls are noops
func (s Store) Init() {
func (s *Store) Init() {
if s.mdc != nil {
return
}
@@ -58,7 +58,7 @@ func (s Store) Init() {
//b := backoff.NewExponentialBackOff()
//b.MaxElapsedTime = 4 * time.Second
//backoff.Retry(func() error {
err = s.initMetadataClient()
err = s.initMetadataClient(NewMetadataClient(s.cfg))
//return err
//}, b)
@@ -72,12 +72,12 @@ func (s Store) Init() {
// New creates a new store
func New(cfg *config.Config, initstore func(settings.Manager)) settings.Manager {
s := Store{
Logger: olog.NewLogger(
olog.Color(cfg.Log.Color),
olog.Pretty(cfg.Log.Pretty),
olog.Level(cfg.Log.Level),
olog.File(cfg.Log.File),
),
//Logger: olog.NewLogger(
//olog.Color(cfg.Log.Color),
//olog.Pretty(cfg.Log.Pretty),
//olog.Level(cfg.Log.Level),
//olog.File(cfg.Log.File),
//),
initStore: initstore,
l: &sync.Mutex{},
init: &sync.Once{},
@@ -97,8 +97,8 @@ func NewMetadataClient(cfg *config.Config) MetadataClient {
}
// we need to lazy initialize the MetadataClient because metadata service might not be ready
func (s Store) initMetadataClient() error {
s.mdc = NewMetadataClient(s.cfg)
func (s *Store) initMetadataClient(mdc MetadataClient) error {
s.mdc = mdc
// TODO: this fails because of authentication issues
err := s.mdc.Init(nil, settingsSpaceID)
+5 -3
View File
@@ -42,13 +42,15 @@ type MockedMetadataClient struct {
}
// NewMDC instantiates a mocked MetadataClient
func NewMDC() MetadataClient {
func NewMDC(s *Store) MetadataClient {
var mdc MetadataClient
switch testtype {
case "unit":
return &MockedMetadataClient{data: make(map[string][]byte)}
mdc = &MockedMetadataClient{data: make(map[string][]byte)}
case "integration":
return NewMetadataClient(&config.Config{})
mdc = NewMetadataClient(&config.Config{})
}
s.initMetadataClient(mdc)
return nil
}
+4 -4
View File
@@ -13,12 +13,12 @@ import (
// If the bundleId is empty, it's ignored for filtering.
// If the accountUUID is empty, only values with empty accountUUID are returned.
// If the accountUUID is not empty, values with an empty or with a matching accountUUID are returned.
func (s Store) ListValues(bundleID, accountUUID string) ([]*settingsmsg.Value, error) {
func (s *Store) ListValues(bundleID, accountUUID string) ([]*settingsmsg.Value, error) {
return nil, errors.New("not implemented")
}
// ReadValue tries to find a value by the given valueId within the dataPath
func (s Store) ReadValue(valueID string) (*settingsmsg.Value, error) {
func (s *Store) ReadValue(valueID string) (*settingsmsg.Value, error) {
s.Init()
b, err := s.mdc.SimpleDownload(nil, valuePath(valueID))
if err != nil {
@@ -29,12 +29,12 @@ func (s Store) ReadValue(valueID string) (*settingsmsg.Value, error) {
}
// ReadValueByUniqueIdentifiers tries to find a value given a set of unique identifiers
func (s Store) ReadValueByUniqueIdentifiers(accountUUID, settingID string) (*settingsmsg.Value, error) {
func (s *Store) ReadValueByUniqueIdentifiers(accountUUID, settingID string) (*settingsmsg.Value, error) {
return nil, errors.New("not implemented")
}
// WriteValue writes the given value into a file within the dataPath
func (s Store) WriteValue(value *settingsmsg.Value) (*settingsmsg.Value, error) {
func (s *Store) WriteValue(value *settingsmsg.Value) (*settingsmsg.Value, error) {
s.Init()
b, err := json.Marshal(value)
if err != nil {
+9 -10
View File
@@ -3,7 +3,6 @@ package store
import (
"testing"
olog "github.com/owncloud/ocis/ocis-pkg/log"
settingsmsg "github.com/owncloud/ocis/protogen/gen/ocis/messages/settings/v0"
"github.com/stretchr/testify/require"
)
@@ -46,15 +45,15 @@ var valueScenarios = []struct {
}
func TestValues(t *testing.T) {
mdc := NewMDC()
s := Store{
Logger: olog.NewLogger(
olog.Color(true),
olog.Pretty(true),
olog.Level("info"),
),
mdc: mdc,
}
//mdc := NewMDC()
//s := Store{
//Logger: olog.NewLogger(
//olog.Color(true),
//olog.Pretty(true),
//olog.Level("info"),
//),
//mdc: mdc,
//}
for i := range valueScenarios {
index := i
t.Run(valueScenarios[index].name, func(t *testing.T) {