Skip to content

Commit

Permalink
rework initialization logic
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
kobergj committed Feb 28, 2022
1 parent 4c22c07 commit 492419e
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 70 deletions.
6 changes: 3 additions & 3 deletions settings/pkg/store/metadata/assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions settings/pkg/store/metadata/assignments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down
12 changes: 6 additions & 6 deletions settings/pkg/store/metadata/bundles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{{
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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")
}

Expand Down
48 changes: 23 additions & 25 deletions settings/pkg/store/metadata/bundles_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"),
),

l: &sync.Mutex{},
mdc: mdc,
}
//s := &Store{
//Logger: olog.NewLogger(
//olog.Color(true),
//olog.Pretty(true),
//olog.Level("info"),
//),

//l: &sync.Mutex{},
//}
//NewMDC(s)

// write bundles
for i := range bundleScenarios {
Expand Down Expand Up @@ -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"),
),

l: &sync.Mutex{},
mdc: mdc,
}
//mdc := NewMDC()
//s := Store{
//Logger: olog.NewLogger(
//olog.Color(true),
//olog.Pretty(true),
//olog.Level("info"),
//),

//l: &sync.Mutex{},
//mdc: mdc,
//}

// appending to non existing bundle creates new
_, err := s.AddSettingToBundle(appendTestBundleID, appendTestSetting1)
Expand Down
6 changes: 3 additions & 3 deletions settings/pkg/store/metadata/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
20 changes: 10 additions & 10 deletions settings/pkg/store/metadata/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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{},
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions settings/pkg/store/metadata/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions settings/pkg/store/metadata/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
19 changes: 9 additions & 10 deletions settings/pkg/store/metadata/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 492419e

Please sign in to comment.