From 22ecfba40283fb9061a014c839dad118005c0d98 Mon Sep 17 00:00:00 2001 From: Keepers <104464746+ryanfkeepers@users.noreply.github.com> Date: Wed, 6 Jul 2022 10:29:42 -0600 Subject: [PATCH] implement exchange selector include/exclude funcs (#276) --- src/pkg/selectors/exchange.go | 138 ++++++++++------ src/pkg/selectors/exchange_test.go | 244 +++++++++++++++++++++++++++++ src/pkg/selectors/selectors.go | 12 ++ 3 files changed, 349 insertions(+), 45 deletions(-) diff --git a/src/pkg/selectors/exchange.go b/src/pkg/selectors/exchange.go index 49a07aa24..34d5e2cdf 100644 --- a/src/pkg/selectors/exchange.go +++ b/src/pkg/selectors/exchange.go @@ -1,9 +1,5 @@ package selectors -import ( - "strings" -) - // --------------------------------------------------------------------------- // Selectors // --------------------------------------------------------------------------- @@ -70,66 +66,118 @@ func (s Selector) ToExchangeRestore() (*ExchangeRestore, error) { return &src, nil } -// IncludeContacts selects the specified contacts owned by the user. -func (s *exchange) IncludeContacts(u string, vs ...string) { - // todo -} +// ------------------- +// Exclude/Includes -// IncludeContactFolders selects the specified contactFolders owned by the user. -func (s *exchange) IncludeContactFolders(u string, vs ...string) { - // todo -} - -// IncludeEvents selects the specified events owned by the user. -func (s *exchange) IncludeEvents(u string, vs ...string) { - // todo -} - -// IncludeMail selects the specified mail messages within the given folder, -// owned by the user. -func (s *exchange) IncludeMail(u, f string, vs ...string) { - // todo -} - -// IncludeMailFolders selects the specified mail folders owned by the user. -func (s *exchange) IncludeMailFolders(u string, vs ...string) { - // todo -} - -// IncludeUsers selects the specified users. All of their data is included. -func (s *exchange) IncludeUsers(us ...string) { - // todo +func contactScope(u, f string, vs ...string) map[string]string { + return map[string]string{ + scopeKeyCategory: ExchangeContact.String(), + ExchangeUser.String(): u, + ExchangeContactFolder.String(): f, + ExchangeContact.String(): join(vs...), + } } // ExcludeContacts selects the specified contacts owned by the user. -func (s *exchange) ExcludeContacts(u string, vs ...string) { - // todo +func (s *exchange) ExcludeContacts(user, folder string, vs ...string) { + s.Excludes = append(s.Excludes, contactScope(user, folder, vs...)) +} + +// IncludeContacts selects the specified contacts owned by the user. +func (s *exchange) IncludeContacts(user, folder string, vs ...string) { + s.Includes = append(s.Includes, contactScope(user, folder, vs...)) +} + +func contactFolderScope(u string, vs ...string) map[string]string { + return map[string]string{ + scopeKeyCategory: ExchangeContactFolder.String(), + ExchangeUser.String(): u, + ExchangeContactFolder.String(): join(vs...), + } +} + +// IncludeContactFolders selects the specified contactFolders owned by the user. +func (s *exchange) IncludeContactFolders(user string, vs ...string) { + s.Includes = append(s.Includes, contactFolderScope(user, vs...)) } // ExcludeContactFolders selects the specified contactFolders owned by the user. -func (s *exchange) ExcludeContactFolders(u string, vs ...string) { - // todo +func (s *exchange) ExcludeContactFolders(user string, vs ...string) { + s.Excludes = append(s.Excludes, contactFolderScope(user, vs...)) +} + +func eventScope(u string, vs ...string) map[string]string { + return map[string]string{ + scopeKeyCategory: ExchangeEvent.String(), + ExchangeUser.String(): u, + ExchangeEvent.String(): join(vs...), + } } // ExcludeEvents selects the specified events owned by the user. -func (s *exchange) ExcludeEvents(u string, vs ...string) { - // todo +func (s *exchange) ExcludeEvents(user string, vs ...string) { + s.Excludes = append(s.Excludes, eventScope(user, vs...)) +} + +// IncludeEvents selects the specified events owned by the user. +func (s *exchange) IncludeEvents(user string, vs ...string) { + s.Includes = append(s.Includes, eventScope(user, vs...)) +} + +func mailScope(u, f string, vs ...string) map[string]string { + return map[string]string{ + scopeKeyCategory: ExchangeMail.String(), + ExchangeUser.String(): u, + ExchangeMailFolder.String(): f, + ExchangeMail.String(): join(vs...), + } } // ExcludeMail selects the specified mail messages within the given folder, // owned by the user. -func (s *exchange) ExcludeMail(u, f string, vs ...string) { - // todo +func (s *exchange) ExcludeMail(user, folder string, vs ...string) { + s.Excludes = append(s.Excludes, mailScope(user, folder, vs...)) +} + +// IncludeMail selects the specified mail messages within the given folder, +// owned by the user. +func (s *exchange) IncludeMail(user, folder string, vs ...string) { + s.Includes = append(s.Includes, mailScope(user, folder, vs...)) +} + +func mailFolderScope(u string, vs ...string) map[string]string { + return map[string]string{ + scopeKeyCategory: ExchangeMailFolder.String(), + ExchangeUser.String(): u, + ExchangeMailFolder.String(): join(vs...), + } } // ExcludeMailFolders selects the specified mail folders owned by the user. -func (s *exchange) ExcludeMailFolders(u string, vs ...string) { - // todo +func (s *exchange) ExcludeMailFolders(user string, vs ...string) { + s.Excludes = append(s.Excludes, mailFolderScope(user, vs...)) +} + +// IncludeMailFolders selects the specified mail folders owned by the user. +func (s *exchange) IncludeMailFolders(user string, vs ...string) { + s.Includes = append(s.Includes, mailFolderScope(user, vs...)) +} + +func userScope(vs ...string) map[string]string { + return map[string]string{ + scopeKeyCategory: ExchangeUser.String(), + ExchangeUser.String(): join(vs...), + } } // ExcludeUsers selects the specified users. All of their data is excluded. -func (s *exchange) ExcludeUsers(us ...string) { - // todo +func (s *exchange) ExcludeUsers(vs ...string) { + s.Excludes = append(s.Excludes, userScope(vs...)) +} + +// IncludeUsers selects the specified users. All of their data is included. +func (s *exchange) IncludeUsers(vs ...string) { + s.Includes = append(s.Includes, userScope(vs...)) } // --------------------------------------------------------------------------- @@ -254,5 +302,5 @@ func (s exchangeScope) Get(cat exchangeCategory) []string { if !ok { return []string{None} } - return strings.Split(v, ",") + return split(v) } diff --git a/src/pkg/selectors/exchange_test.go b/src/pkg/selectors/exchange_test.go index 6636cb058..7ea8c3402 100644 --- a/src/pkg/selectors/exchange_test.go +++ b/src/pkg/selectors/exchange_test.go @@ -54,6 +54,250 @@ func (suite *ExchangeSourceSuite) TestToExchangeRestore() { assert.NotZero(t, eb.Scopes()) } +func (suite *ExchangeSourceSuite) TestExchangeSelector_ExcludeContacts() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + user = "user" + folder = All + c1 = "c1" + c2 = "c2" + ) + + sel.ExcludeContacts(user, folder, c1, c2) + scopes := sel.Excludes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], user) + assert.Equal(t, scope[ExchangeContactFolder.String()], folder) + assert.Equal(t, scope[ExchangeContact.String()], join(c1, c2)) +} + +func (suite *ExchangeSourceSuite) TestExchangeSelector_IncludeContacts() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + user = "user" + folder = All + c1 = "c1" + c2 = "c2" + ) + + sel.IncludeContacts(user, folder, c1, c2) + scopes := sel.Includes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], user) + assert.Equal(t, scope[ExchangeContactFolder.String()], folder) + assert.Equal(t, scope[ExchangeContact.String()], join(c1, c2)) + + assert.Equal(t, sel.Scopes()[0].Category(), ExchangeContact) +} + +func (suite *ExchangeSourceSuite) TestExchangeSelector_ExcludeContactFolders() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + user = "user" + f1 = "f1" + f2 = "f2" + ) + + sel.ExcludeContactFolders(user, f1, f2) + scopes := sel.Excludes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], user) + assert.Equal(t, scope[ExchangeContactFolder.String()], join(f1, f2)) +} + +func (suite *ExchangeSourceSuite) TestExchangeSelector_IncludeContactFolders() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + user = "user" + f1 = "f1" + f2 = "f2" + ) + + sel.IncludeContactFolders(user, f1, f2) + scopes := sel.Includes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], user) + assert.Equal(t, scope[ExchangeContactFolder.String()], join(f1, f2)) + + assert.Equal(t, sel.Scopes()[0].Category(), ExchangeContactFolder) +} + +func (suite *ExchangeSourceSuite) TestExchangeSelector_ExcludeEvents() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + user = "user" + e1 = "e1" + e2 = "e2" + ) + + sel.ExcludeEvents(user, e1, e2) + scopes := sel.Excludes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], user) + assert.Equal(t, scope[ExchangeEvent.String()], join(e1, e2)) +} + +func (suite *ExchangeSourceSuite) TestExchangeSelector_IncludeEvents() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + user = "user" + e1 = "e1" + e2 = "e2" + ) + + sel.IncludeEvents(user, e1, e2) + scopes := sel.Includes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], user) + assert.Equal(t, scope[ExchangeEvent.String()], join(e1, e2)) + + assert.Equal(t, sel.Scopes()[0].Category(), ExchangeEvent) +} + +func (suite *ExchangeSourceSuite) TestExchangeSelector_ExcludeMail() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + user = "user" + folder = All + m1 = "m1" + m2 = "m2" + ) + + sel.ExcludeMail(user, folder, m1, m2) + scopes := sel.Excludes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], user) + assert.Equal(t, scope[ExchangeMailFolder.String()], folder) + assert.Equal(t, scope[ExchangeMail.String()], join(m1, m2)) +} + +func (suite *ExchangeSourceSuite) TestExchangeSelector_IncludeMail() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + user = "user" + folder = All + m1 = "m1" + m2 = "m2" + ) + + sel.IncludeMail(user, folder, m1, m2) + scopes := sel.Includes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], user) + assert.Equal(t, scope[ExchangeMailFolder.String()], folder) + assert.Equal(t, scope[ExchangeMail.String()], join(m1, m2)) + + assert.Equal(t, sel.Scopes()[0].Category(), ExchangeMail) +} + +func (suite *ExchangeSourceSuite) TestExchangeSelector_ExcludeMailFolders() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + user = "user" + f1 = "f1" + f2 = "f2" + ) + + sel.ExcludeMailFolders(user, f1, f2) + scopes := sel.Excludes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], user) + assert.Equal(t, scope[ExchangeMailFolder.String()], join(f1, f2)) +} + +func (suite *ExchangeSourceSuite) TestExchangeSelector_IncludeMailFolders() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + user = "user" + f1 = "f1" + f2 = "f2" + ) + + sel.IncludeMailFolders(user, f1, f2) + scopes := sel.Includes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], user) + assert.Equal(t, scope[ExchangeMailFolder.String()], join(f1, f2)) + + assert.Equal(t, sel.Scopes()[0].Category(), ExchangeMailFolder) +} + +func (suite *ExchangeSourceSuite) TestExchangeSelector_ExcludeUsers() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + u1 = "u1" + u2 = "u2" + ) + + sel.ExcludeUsers(u1, u2) + scopes := sel.Excludes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], join(u1, u2)) +} + +func (suite *ExchangeSourceSuite) TestExchangeSelector_IncludeUsers() { + t := suite.T() + sel := NewExchangeBackup() + + const ( + u1 = "u1" + u2 = "u2" + ) + + sel.IncludeUsers(u1, u2) + scopes := sel.Includes + require.Equal(t, 1, len(scopes)) + + scope := scopes[0] + assert.Equal(t, scope[ExchangeUser.String()], join(u1, u2)) + + assert.Equal(t, sel.Scopes()[0].Category(), ExchangeUser) +} + func (suite *ExchangeSourceSuite) TestNewExchangeDestination() { t := suite.T() dest := NewExchangeDestination() diff --git a/src/pkg/selectors/selectors.go b/src/pkg/selectors/selectors.go index 599c3160a..751f74029 100644 --- a/src/pkg/selectors/selectors.go +++ b/src/pkg/selectors/selectors.go @@ -1,6 +1,8 @@ package selectors import ( + "strings" + "github.com/pkg/errors" ) @@ -25,6 +27,8 @@ const ( // None is usesd to express "no data of " // Ex: {user: u1, events: None} => no events for user u1. None = "√ç≈œ´∆¬˚¨π" + + delimiter = "," ) // --------------------------------------------------------------------------- @@ -69,3 +73,11 @@ func badCastErr(cast, is service) error { func existingDestinationErr(category, is string) error { return errors.Wrapf(ErrorDestinationAlreadySet, "%s destination already set to %s", category, is) } + +func join(s ...string) string { + return strings.Join(s, delimiter) +} + +func split(s string) []string { + return strings.Split(s, delimiter) +}