Skip to content

Commit

Permalink
feat: added a way to get a whitelist from cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SantFlamel committed Dec 23, 2019
1 parent b21fa4f commit 458f579
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
8 changes: 2 additions & 6 deletions cmd/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ type config struct {
}

type cleanupConfig struct {
Whitelist []string `yaml:"whitelist"`
Delete bool `yaml:"delete"`
AddAnonymousToWhitelist bool `json:"add_anonymous_to_whitelist"`
Delete bool `yaml:"delete"`
AddAnonymousToWhitelist bool `json:"add_anonymous_to_whitelist"`
}

func getDefault() config {
Expand All @@ -26,8 +25,5 @@ func getDefault() config {
Port: "6379",
ConnectionLimit: 512,
},
Cleanup: cleanupConfig{
Whitelist: []string{},
},
}
}
1 change: 0 additions & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ func main() {
}

if *cleanup {
logger.Debugf("User whitelist: %#v", confCleanup.Whitelist)
if err := handleCleanup(logger, dataBase, confCleanup); err != nil {
logger.Error(err)
}
Expand Down
26 changes: 18 additions & 8 deletions cmd/cli/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package main

import (
"encoding/json"
"fmt"
"os"

"github.com/moira-alert/moira"
)

type usersCleaning struct {
Users []string `json:"users"`
Whitelist []string `json:"whitelist"`
}

func transferUserSubscriptionsAndContacts(database moira.Database, from, to string) error {
contactIDs, err := database.GetUserContactIDs(from)
if err != nil {
Expand Down Expand Up @@ -80,25 +86,29 @@ func deleteUser(database moira.Database, user string) error {
}

func handleCleanup(logger moira.Logger, database moira.Database, config cleanupConfig) error {
var users []string
clean := new(usersCleaning)

reader := json.NewDecoder(os.Stdin)

if err := reader.Decode(&users); err != nil {
if err := reader.Decode(clean); err != nil {
return err
}

return usersCleanup(logger, database, users, config)
fmt.Printf("%#v\n", clean)

return nil

//nolint
return usersCleanup(logger, database, clean.Whitelist, clean.Users, config)
}

func usersCleanup(logger moira.Logger, database moira.Database, users []string, config cleanupConfig) error {
func usersCleanup(logger moira.Logger, database moira.Database, users, whitelist []string, config cleanupConfig) error {
if config.AddAnonymousToWhitelist {
config.Whitelist = append(config.Whitelist, "")
whitelist = append(whitelist, "")
}

usersMap := make(map[string]bool, len(users)+len(config.Whitelist))
usersMap := make(map[string]bool, len(users)+len(whitelist))

for _, user := range append(users, config.Whitelist...) {
for _, user := range append(users, whitelist...) {
usersMap[user] = true
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/cli/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestUpdateUsers(t *testing.T) {
}

database := redis.NewDatabase(logger, conf.Redis.GetSettings(), redis.Cli)
conf.Cleanup.Whitelist = []string{"Nikolay", ""}

users := []string{"Aleksey", "Arkadiy", "Emil"}

Expand All @@ -40,7 +39,7 @@ func TestUpdateUsers(t *testing.T) {
}(t)

Convey("Test off notifications", func() {
So(usersCleanup(logger, database, users, conf.Cleanup), ShouldBeNil)
So(usersCleanup(logger, database, users, []string{"Nikolay", ""}, conf.Cleanup), ShouldBeNil)
for _, contact := range contacts {
subscription, err := database.GetSubscription("subscription_" + contact.ID)

Expand All @@ -56,7 +55,7 @@ func TestUpdateUsers(t *testing.T) {

Convey("Verify deletion of contacts and subscriptions", func() {
conf.Cleanup.Delete = true
So(usersCleanup(logger, database, users, conf.Cleanup), ShouldBeNil)
So(usersCleanup(logger, database, []string{}, users, conf.Cleanup), ShouldBeNil)
for _, contact := range contacts {
if !strings.Contains(contact.User, "Another") {
continue
Expand Down

0 comments on commit 458f579

Please sign in to comment.