Skip to content

Commit

Permalink
cloud: added group replacement;
Browse files Browse the repository at this point in the history
  • Loading branch information
applike-ss committed Jul 18, 2023
1 parent f5ae06e commit 1a9987e
Show file tree
Hide file tree
Showing 80 changed files with 420 additions and 325 deletions.
1 change: 1 addition & 0 deletions pkg/application/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func Default(options ...Option) kernel.Kernel {
WithMetadataServer,
WithConsumerMessagesPerRunnerMetrics,
WithKernelSettingsFromConfig,
WithLoggerGroupTag,
WithLoggerApplicationTag,
WithLoggerContextFieldsMessageEncoder,
WithLoggerContextFieldsResolver(log.ContextLoggerFieldsResolver),
Expand Down
12 changes: 12 additions & 0 deletions pkg/application/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ func WithKernelSettingsFromConfig(app *App) {
})
}

func WithLoggerGroupTag(app *App) {
app.addLoggerOption(func(config cfg.GosoConf, logger log.GosoLogger) error {
if !config.IsSet("app_group") {
return errors.New("can not get application group from config to set it on logger")
}

return logger.Option(log.WithFields(map[string]interface{}{
"group": config.GetString("app_group"),
}))
})
}

func WithLoggerApplicationTag(app *App) {
app.addLoggerOption(func(config cfg.GosoConf, logger log.GosoLogger) error {
if !config.IsSet("app_name") {
Expand Down
1 change: 1 addition & 0 deletions pkg/application/testdata/config.dist.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
env: test

app_name: test_app
app_group: test_group
app_project: test_project
app_family: test_family
19 changes: 17 additions & 2 deletions pkg/cfg/application_identifiers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package cfg

import "fmt"
import (
"strings"

"github.com/justtrackio/gosoline/pkg/funk"
)

type AppId struct {
Project string `cfg:"project" default:"{app_project}" json:"project"`
Environment string `cfg:"environment" default:"{env}" json:"environment"`
Family string `cfg:"family" default:"{app_family}" json:"family"`
Group string `cfg:"group" default:"{app_group}" json:"group"`
Application string `cfg:"application" default:"{app_name}" json:"application"`
}

Expand All @@ -14,6 +19,7 @@ func GetAppIdFromConfig(config Config) AppId {
Project: config.GetString("app_project"),
Environment: config.GetString("env"),
Family: config.GetString("app_family"),
Group: config.GetString("app_group"),
Application: config.GetString("app_name"),
}
}
Expand All @@ -31,11 +37,20 @@ func (i *AppId) PadFromConfig(config Config) {
i.Family = config.GetString("app_family")
}

if len(i.Group) == 0 {
i.Group = config.GetString("app_group")
}

if len(i.Application) == 0 {
i.Application = config.GetString("app_name")
}
}

func (i *AppId) String() string {
return fmt.Sprintf("%s-%s-%s-%s", i.Project, i.Environment, i.Family, i.Application)
elements := []string{i.Project, i.Environment, i.Family, i.Group, i.Application}
elements = funk.Filter(elements, func(element string) bool {
return len(element) > 0
})

return strings.Join(elements, "-")
}
4 changes: 4 additions & 0 deletions pkg/cfg/application_identifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestGetAppIdFromConfig(t *testing.T) {
config := new(cfgMocks.Config)
config.On("GetString", "app_project").Return("prj")
config.On("GetString", "app_family").Return("fam")
config.On("GetString", "app_group").Return("grp")
config.On("GetString", "app_name").Return("name")
config.On("GetString", "env").Return("test")

Expand All @@ -21,6 +22,7 @@ func TestGetAppIdFromConfig(t *testing.T) {
Project: "prj",
Environment: "test",
Family: "fam",
Group: "grp",
Application: "name",
}, appId)

Expand All @@ -31,6 +33,7 @@ func TestAppId_PadFromConfig(t *testing.T) {
config := new(cfgMocks.Config)
config.On("GetString", "app_project").Return("prj")
config.On("GetString", "app_family").Return("fam")
config.On("GetString", "app_group").Return("grp")
config.On("GetString", "app_name").Return("name")
config.On("GetString", "env").Return("test")

Expand All @@ -41,6 +44,7 @@ func TestAppId_PadFromConfig(t *testing.T) {
Project: "prj",
Environment: "test",
Family: "fam",
Group: "grp",
Application: "name",
}, appId)

Expand Down
4 changes: 2 additions & 2 deletions pkg/cloud/aws/kinesis/metadata_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ type metadataRepository struct {
func NewMetadataRepository(ctx context.Context, config cfg.Config, logger log.Logger, stream Stream, clientId ClientId, settings Settings) (MetadataRepository, error) {
ddbSettings := &ddb.Settings{
ModelId: mdl.ModelId{
Application: "kinsumer",
Name: "metadata",
Group: "kinsumer",
Name: "metadata",
},
Main: ddb.MainSettings{
Model: &FullRecord{},
Expand Down
13 changes: 7 additions & 6 deletions pkg/cloud/aws/kinesis/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type StreamNameSettingsAware interface {
}

type StreamNamingSettings struct {
Pattern string `cfg:"pattern,nodecode" default:"{project}-{env}-{family}-{app}-{name}"`
Pattern string `cfg:"pattern,nodecode" default:"{project}-{env}-{family}-{group}-{streamName}"`
}

func GetStreamName(config cfg.Config, settings StreamNameSettingsAware) (Stream, error) {
Expand All @@ -31,11 +31,12 @@ func GetStreamName(config cfg.Config, settings StreamNameSettingsAware) (Stream,
name := namingSettings.Pattern

values := map[string]string{
"project": appId.Project,
"env": appId.Environment,
"family": appId.Family,
"app": appId.Application,
"name": settings.GetStreamName(),
"project": appId.Project,
"env": appId.Environment,
"family": appId.Family,
"group": appId.Group,
"app": appId.Application,
"streamName": settings.GetStreamName(),
}

for key, val := range values {
Expand Down
9 changes: 5 additions & 4 deletions pkg/cloud/aws/kinesis/naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ func (s *GetStreamNameTestSuite) SetupTest() {
s.settings = &kinesis.Settings{
AppId: cfg.AppId{
Project: "justtrack",
Environment: "test",
Environment: "env",
Family: "gosoline",
Group: "grp",
Application: "producer",
},
ClientName: "default",
Expand All @@ -40,12 +41,12 @@ func (s *GetStreamNameTestSuite) setupConfig(settings map[string]interface{}) {
func (s *GetStreamNameTestSuite) TestDefault() {
name, err := kinesis.GetStreamName(s.config, s.settings)
s.NoError(err)
s.EqualValues("justtrack-test-gosoline-producer-event", name)
s.EqualValues("justtrack-env-gosoline-grp-event", string(name))
}

func (s *GetStreamNameTestSuite) TestDefaultWithPattern() {
s.setupConfig(map[string]interface{}{
"cloud.aws.kinesis.clients.default.naming.pattern": "{app}-{name}",
"cloud.aws.kinesis.clients.default.naming.pattern": "{app}-{streamName}",
})

name, err := kinesis.GetStreamName(s.config, s.settings)
Expand All @@ -56,7 +57,7 @@ func (s *GetStreamNameTestSuite) TestDefaultWithPattern() {
func (s *GetStreamNameTestSuite) TestSpecificClientWithPattern() {
s.settings.ClientName = "specific"
s.setupConfig(map[string]interface{}{
"cloud.aws.kinesis.clients.specific.naming.pattern": "{app}-{name}",
"cloud.aws.kinesis.clients.specific.naming.pattern": "{app}-{streamName}",
})

name, err := kinesis.GetStreamName(s.config, s.settings)
Expand Down
3 changes: 2 additions & 1 deletion pkg/cloud/aws/sns/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s TopicNameSettings) GetTopicId() string {
}

type TopicNamingSettings struct {
Pattern string `cfg:"pattern,nodecode" default:"{project}-{env}-{family}-{app}-{topicId}"`
Pattern string `cfg:"pattern,nodecode" default:"{project}-{env}-{family}-{group}-{topicId}"`
}

func GetTopicName(config cfg.Config, topicSettings TopicNameSettingsAware) (string, error) {
Expand All @@ -51,6 +51,7 @@ func GetTopicName(config cfg.Config, topicSettings TopicNameSettingsAware) (stri
"project": appId.Project,
"env": appId.Environment,
"family": appId.Family,
"group": appId.Group,
"app": appId.Application,
"topicId": topicSettings.GetTopicId(),
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cloud/aws/sns/naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (s *GetTopicNameTestSuite) SetupTest() {
Project: "justtrack",
Environment: "test",
Family: "gosoline",
Group: "group",
Application: "producer",
},
ClientName: "default",
Expand All @@ -40,7 +41,7 @@ func (s *GetTopicNameTestSuite) setupConfig(settings map[string]interface{}) {
func (s *GetTopicNameTestSuite) TestDefault() {
name, err := sns.GetTopicName(s.config, s.settings)
s.NoError(err)
s.Equal("justtrack-test-gosoline-producer-event", name)
s.Equal("justtrack-test-gosoline-group-event", name)
}

func (s *GetTopicNameTestSuite) TestDefaultWithPattern() {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cloud/aws/sqs/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s QueueNameSettings) GetQueueId() string {
}

type QueueNamingSettings struct {
Pattern string `cfg:"pattern,nodecode" default:"{project}-{env}-{family}-{app}-{queueId}"`
Pattern string `cfg:"pattern,nodecode" default:"{project}-{env}-{family}-{group}-{queueId}"`
}

func GetQueueName(config cfg.Config, queueSettings QueueNameSettingsAware) (string, error) {
Expand All @@ -57,6 +57,7 @@ func GetQueueName(config cfg.Config, queueSettings QueueNameSettingsAware) (stri
"project": appId.Project,
"env": appId.Environment,
"family": appId.Family,
"group": appId.Group,
"app": appId.Application,
"queueId": queueSettings.GetQueueId(),
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cloud/aws/sqs/naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (s *GetSqsQueueNameTestSuite) SetupTest() {
Project: "justtrack",
Environment: "test",
Family: "gosoline",
Group: "group",
Application: "producer",
},
ClientName: "default",
Expand All @@ -40,15 +41,15 @@ func (s *GetSqsQueueNameTestSuite) setupConfig(settings map[string]interface{})
func (s *GetSqsQueueNameTestSuite) TestDefault() {
name, err := sqs.GetQueueName(s.config, s.settings)
s.NoError(err)
s.Equal("justtrack-test-gosoline-producer-event", name)
s.Equal("justtrack-test-gosoline-group-event", name)
}

func (s *GetSqsQueueNameTestSuite) TestDefaultFifo() {
s.settings.FifoEnabled = true

name, err := sqs.GetQueueName(s.config, s.settings)
s.NoError(err)
s.Equal("justtrack-test-gosoline-producer-event.fifo", name)
s.Equal("justtrack-test-gosoline-group-event.fifo", name)
}

func (s *GetSqsQueueNameTestSuite) TestDefaultWithPattern() {
Expand Down
12 changes: 8 additions & 4 deletions pkg/conc/ddb/leader_election_ddb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ type DdbLeaderElectionItem struct {
LeadingUntil int64 `json:"leadingUntil" ddb:"ttl=enabled"`
}

type TableNamingSettings struct {
Pattern string `cfg:"pattern,nodecode" default:"{project}-{env}-{family}-leader-elections"`
}

type DdbLeaderElectionSettings struct {
TableName string `cfg:"table_name" default:"{app_project}-{env}-{app_family}-leader-elections"`
GroupId string `cfg:"group_id" default:"{app_name}"`
LeaseDuration time.Duration `cfg:"lease_duration" default:"1m"`
Naming TableNamingSettings `cfg:"naming"`
GroupId string `cfg:"group_id" default:"{app_name}"`
LeaseDuration time.Duration `cfg:"lease_duration" default:"1m"`
}

type DdbLeaderElection struct {
Expand All @@ -44,7 +48,7 @@ func NewDdbLeaderElectionWithSettings(ctx context.Context, config cfg.Config, lo
repository, err := ddb.NewRepository(ctx, config, logger, &ddb.Settings{
ModelId: mdl.ModelId{},
TableNamingSettings: ddb.TableNamingSettings{
Pattern: settings.TableName,
Pattern: settings.Naming.Pattern,
},
DisableTracing: true,
Main: ddb.MainSettings{
Expand Down
4 changes: 3 additions & 1 deletion pkg/conc/ddb/leader_election_ddb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func (s *DdbLeaderElectionTestCase) SetupTest() {

var err error
s.election, err = concDdb.NewDdbLeaderElectionWithInterfaces(s.logger, s.clock, s.repository, &concDdb.DdbLeaderElectionSettings{
TableName: "gosoline-leader-election",
Naming: concDdb.TableNamingSettings{
Pattern: "gosoline-leader-election",
},
GroupId: "test",
LeaseDuration: time.Minute,
})
Expand Down
19 changes: 19 additions & 0 deletions pkg/db-repo/notification_publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db_repo_test
import (
"context"
"testing"
"time"

"github.com/justtrackio/gosoline/pkg/cfg"
db_repo "github.com/justtrackio/gosoline/pkg/db-repo"
Expand All @@ -12,6 +13,23 @@ import (
"github.com/stretchr/testify/assert"
)

type modelBased struct {
value string
createdAt, updatedAt *time.Time
}

func (m *modelBased) GetId() *uint {
return mdl.Box(uint(3))
}

func (m *modelBased) SetUpdatedAt(updatedAt *time.Time) {
m.updatedAt = updatedAt
}

func (m *modelBased) SetCreatedAt(createdAt *time.Time) {
m.createdAt = createdAt
}

func Test_Publish_Notifier(t *testing.T) {
input := &modelBased{
value: "my test input",
Expand All @@ -33,6 +51,7 @@ func Test_Publish_Notifier(t *testing.T) {
Name: "myTest",
Application: "testApp",
Family: "testFamily",
Group: "grp",
Environment: "test",
}

Expand Down
Loading

0 comments on commit 1a9987e

Please sign in to comment.