Skip to content

Commit

Permalink
Merge pull request #1887 from tisnik/refactoring-ocp-recommendations
Browse files Browse the repository at this point in the history
Refactoring: OCPRecommendationsStorage
  • Loading branch information
tisnik authored Nov 28, 2023
2 parents 351fef5 + 2b7c17a commit 2c705bb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func fillInInfoParams(params map[string]string) {
// createStorage function initializes connection to preconfigured storage,
// usually SQLite, PostgreSQL, or AWS RDS.
func createStorage() (storage.OCPRecommendationsStorage, error) {
storageCfg := conf.GetStorageConfiguration()
storageCfg := conf.GetOCPRecommendationsStorageConfiguration()
redisCfg := conf.GetRedisConfiguration()
// fill-in the missing sub-structure to have the whole Storage
// configuration represented as one data structure
Expand Down Expand Up @@ -144,7 +144,7 @@ func closeStorage(storage storage.OCPRecommendationsStorage) {
// autoMigrate is set performs migration to the latest schema version
// available.
func prepareDBMigrations(dbStorage storage.OCPRecommendationsStorage) int {
if conf.GetStorageConfiguration().Type != types.SQLStorage {
if conf.GetOCPRecommendationsStorageConfiguration().Type != types.SQLStorage {
log.Info().Msg("Skipping migration for non-SQL database type")
return ExitStatusOK
}
Expand Down
30 changes: 15 additions & 15 deletions conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ type ConfigStruct struct {
Processing struct {
OrgAllowlistFile string `mapstructure:"org_allowlist_file" toml:"org_allowlist_file"`
} `mapstructure:"processing"`
Storage storage.Configuration `mapstructure:"storage" toml:"storage"`
Logging logger.LoggingConfiguration `mapstructure:"logging" toml:"logging"`
CloudWatch logger.CloudWatchConfiguration `mapstructure:"cloudwatch" toml:"cloudwatch"`
Redis storage.RedisConfiguration `mapstructure:"redis" toml:"redis"`
Metrics MetricsConfiguration `mapstructure:"metrics" toml:"metrics"`
SentryLoggingConf logger.SentryLoggingConfiguration `mapstructure:"sentry" toml:"sentry"`
KafkaZerologConf logger.KafkaZerologConfiguration `mapstructure:"kafka_zerolog" toml:"kafka_zerolog"`
OCPRecommendationsStorage storage.Configuration `mapstructure:"storage" toml:"storage"`
Logging logger.LoggingConfiguration `mapstructure:"logging" toml:"logging"`
CloudWatch logger.CloudWatchConfiguration `mapstructure:"cloudwatch" toml:"cloudwatch"`
Redis storage.RedisConfiguration `mapstructure:"redis" toml:"redis"`
Metrics MetricsConfiguration `mapstructure:"metrics" toml:"metrics"`
SentryLoggingConf logger.SentryLoggingConfiguration `mapstructure:"sentry" toml:"sentry"`
KafkaZerologConf logger.KafkaZerologConfiguration `mapstructure:"kafka_zerolog" toml:"kafka_zerolog"`
}

// Config has exactly the same structure as *.toml file
Expand Down Expand Up @@ -179,9 +179,9 @@ func getOrganizationAllowlist() mapset.Set {
return allowlist
}

// GetStorageConfiguration returns storage configuration
func GetStorageConfiguration() storage.Configuration {
return Config.Storage
// GetOCPRecommendationsStorageConfiguration returns storage configuration
func GetOCPRecommendationsStorageConfiguration() storage.Configuration {
return Config.OCPRecommendationsStorage
}

// GetRedisConfiguration returns Redis storage configuration
Expand Down Expand Up @@ -317,11 +317,11 @@ func updateConfigFromClowder(c *ConfigStruct) error {

if clowder.LoadedConfig.Database != nil {
// get DB configuration from clowder
c.Storage.PGDBName = clowder.LoadedConfig.Database.Name
c.Storage.PGHost = clowder.LoadedConfig.Database.Hostname
c.Storage.PGPort = clowder.LoadedConfig.Database.Port
c.Storage.PGUsername = clowder.LoadedConfig.Database.Username
c.Storage.PGPassword = clowder.LoadedConfig.Database.Password
c.OCPRecommendationsStorage.PGDBName = clowder.LoadedConfig.Database.Name
c.OCPRecommendationsStorage.PGHost = clowder.LoadedConfig.Database.Hostname
c.OCPRecommendationsStorage.PGPort = clowder.LoadedConfig.Database.Port
c.OCPRecommendationsStorage.PGUsername = clowder.LoadedConfig.Database.Username
c.OCPRecommendationsStorage.PGPassword = clowder.LoadedConfig.Database.Password
} else {
fmt.Println(noStorage)
}
Expand Down
12 changes: 6 additions & 6 deletions conf/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestLoadServerConfiguration(t *testing.T) {
func TestLoadStorageConfiguration(t *testing.T) {
TestLoadConfiguration(t)

storageCfg := conf.GetStorageConfiguration()
storageCfg := conf.GetOCPRecommendationsStorageConfiguration()

assert.Equal(t, "sqlite3", storageCfg.Driver)
assert.Equal(t, ":memory:", storageCfg.SQLiteDataSource)
Expand All @@ -189,7 +189,7 @@ func TestLoadConfigurationOverrideFromEnv(t *testing.T) {

mustLoadConfiguration(configPath)

storageCfg := conf.GetStorageConfiguration()
storageCfg := conf.GetOCPRecommendationsStorageConfiguration()
assert.Equal(t, storage.Configuration{
Driver: "sqlite3",
SQLiteDataSource: ":memory:",
Expand All @@ -207,7 +207,7 @@ func TestLoadConfigurationOverrideFromEnv(t *testing.T) {

mustLoadConfiguration(configPath)

storageCfg = conf.GetStorageConfiguration()
storageCfg = conf.GetOCPRecommendationsStorageConfiguration()
assert.Equal(t, storage.Configuration{
Driver: "postgres",
SQLiteDataSource: ":memory:",
Expand Down Expand Up @@ -353,7 +353,7 @@ func TestLoadConfigurationFromFile(t *testing.T) {
PGDBName: "aggregator",
PGParams: "params",
Type: "sql",
}, conf.GetStorageConfiguration())
}, conf.GetOCPRecommendationsStorageConfiguration())

assert.Equal(t, storage.RedisConfiguration{
RedisEndpoint: "localhost:6379",
Expand Down Expand Up @@ -411,7 +411,7 @@ func TestLoadConfigurationFromEnv(t *testing.T) {
PGDBName: "aggregator",
PGParams: "params",
Type: "sql",
}, conf.GetStorageConfiguration())
}, conf.GetOCPRecommendationsStorageConfiguration())

assert.Equal(t, storage.RedisConfiguration{
RedisEndpoint: "default-redis-endpoint",
Expand Down Expand Up @@ -498,7 +498,7 @@ func TestLoadConfigurationFromEnvVariableClowderEnabled(t *testing.T) {

// retrieve broker config
brokerCfg := conf.GetBrokerConfiguration()
storageCfg := conf.GetStorageConfiguration()
storageCfg := conf.GetOCPRecommendationsStorageConfiguration()

// check
assert.Equal(t, "localhost:29092", brokerCfg.Address, "Broker doesn't match")
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func startServer() error {
// try to retrieve the actual DB migration version
// and add it into the `params` map
log.Info().Msg("Setting DB version for /info endpoint")
if conf.GetStorageConfiguration().Type == types.SQLStorage {
if conf.GetOCPRecommendationsStorageConfiguration().Type == types.SQLStorage {
// migration and DB versioning is now supported for SQL
// databases only
currentVersion, err := migration.GetDBVersion(dbStorage.GetConnection())
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers/mock_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func MustGetPostgresStorage(tb testing.TB, init bool) (storage.OCPRecommendation
helpers.FailOnError(tb, err)

// force postgres and replace db name with test one
storageConf := &conf.Config.Storage
storageConf := &conf.Config.OCPRecommendationsStorage
storageConf.Driver = postgres
storageConf.PGDBName += "_test_db_" + strings.ReplaceAll(uuid.New().String(), "-", "_")
storageConf.PGPassword = dbAdminPassword
Expand Down Expand Up @@ -175,7 +175,7 @@ func MustGetPostgresStorage(tb testing.TB, init bool) (storage.OCPRecommendation
return postgresStorage, func() {
MustCloseStorage(tb, postgresStorage)

_, err := adminConn.Exec("DROP DATABASE " + conf.Config.Storage.PGDBName)
_, err := adminConn.Exec("DROP DATABASE " + conf.Config.OCPRecommendationsStorage.PGDBName)
helpers.FailOnError(tb, err)

helpers.FailOnError(tb, adminConn.Close())
Expand Down

0 comments on commit 2c705bb

Please sign in to comment.