Skip to content

Commit

Permalink
#81 use registry-lib key and value types
Browse files Browse the repository at this point in the history
Introduce new types so that we can
refactor the operator step by step.
  • Loading branch information
alexander-dammeier committed Oct 7, 2024
1 parent 033f9b7 commit 077c111
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 37 deletions.
6 changes: 3 additions & 3 deletions pkg/adapter/config/etcd/doguConfigRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewDoguConfigRepository(etcdStore etcdStore) *DoguConfigRepository {
}

func (e DoguConfigRepository) Get(_ context.Context, key common.DoguConfigKey) (*ecosystem.DoguConfigEntry, error) {
entry, err := e.etcdStore.DoguConfig(string(key.DoguName)).Get(key.Key)
entry, err := e.etcdStore.DoguConfig(string(key.DoguName)).Get(string(key.Key))
if registry.IsKeyNotFoundError(err) {
return nil, domainservice.NewNotFoundError(err, "could not find %s in etcd", key)
} else if err != nil {
Expand All @@ -33,7 +33,7 @@ func (e DoguConfigRepository) Get(_ context.Context, key common.DoguConfigKey) (
func (e DoguConfigRepository) Save(_ context.Context, entry *ecosystem.DoguConfigEntry) error {
strDoguName := string(entry.Key.DoguName)
strValue := string(entry.Value)
err := setEtcdKey(entry.Key.Key, strValue, e.etcdStore.DoguConfig(strDoguName))
err := setEtcdKey(string(entry.Key.Key), strValue, e.etcdStore.DoguConfig(strDoguName))
if err != nil {
return domainservice.NewInternalError(err, "failed to set %s with value %q in etcd", entry.Key, strValue)
}
Expand All @@ -43,7 +43,7 @@ func (e DoguConfigRepository) Save(_ context.Context, entry *ecosystem.DoguConfi

func (e DoguConfigRepository) Delete(_ context.Context, key common.DoguConfigKey) error {
strDoguName := string(key.DoguName)
err := deleteEtcdKey(key.Key, e.etcdStore.DoguConfig(strDoguName))
err := deleteEtcdKey(string(key.Key), e.etcdStore.DoguConfig(strDoguName))
if err != nil && !registry.IsKeyNotFoundError(err) {
return domainservice.NewInternalError(err, "failed to delete %s from etcd", key)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/adapter/config/etcd/doguConfigRepository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestEtcdDoguConfigRepository_Delete(t *testing.T) {

key := common.DoguConfigKey{Key: "key", DoguName: testSimpleDoguNameRedmine}
etcdMock.EXPECT().DoguConfig(string(testSimpleDoguNameRedmine)).Return(configurationContextMock)
configurationContextMock.EXPECT().Delete(key.Key).Return(nil)
configurationContextMock.EXPECT().Delete(string(key.Key)).Return(nil)

// when
err := sut.Delete(testCtx, key)
Expand All @@ -42,7 +42,7 @@ func TestEtcdDoguConfigRepository_Delete(t *testing.T) {

key := common.DoguConfigKey{Key: "key", DoguName: testSimpleDoguNameRedmine}
etcdMock.EXPECT().DoguConfig(string(testSimpleDoguNameRedmine)).Return(configurationContextMock)
configurationContextMock.EXPECT().Delete(key.Key).Return(etcdNotFoundError)
configurationContextMock.EXPECT().Delete(string(key.Key)).Return(etcdNotFoundError)

// when
err := sut.Delete(testCtx, key)
Expand All @@ -59,7 +59,7 @@ func TestEtcdDoguConfigRepository_Delete(t *testing.T) {

key := common.DoguConfigKey{Key: "key", DoguName: testSimpleDoguNameRedmine}
etcdMock.EXPECT().DoguConfig(string(testSimpleDoguNameRedmine)).Return(configurationContextMock)
configurationContextMock.EXPECT().Delete(key.Key).Return(assert.AnError)
configurationContextMock.EXPECT().Delete(string(key.Key)).Return(assert.AnError)

// when
err := sut.Delete(testCtx, key)
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestEtcdDoguConfigRepository_Save(t *testing.T) {
PersistenceContext: nil,
}
etcdMock.EXPECT().DoguConfig(string(testSimpleDoguNameRedmine)).Return(configurationContextMock)
configurationContextMock.EXPECT().Set(entry.Key.Key, string(entry.Value)).Return(nil)
configurationContextMock.EXPECT().Set(string(entry.Key.Key), string(entry.Value)).Return(nil)

// when
err := sut.Save(testCtx, entry)
Expand All @@ -249,7 +249,7 @@ func TestEtcdDoguConfigRepository_Save(t *testing.T) {
PersistenceContext: nil,
}
etcdMock.EXPECT().DoguConfig(string(testSimpleDoguNameRedmine)).Return(configurationContextMock)
configurationContextMock.EXPECT().Set(entry.Key.Key, string(entry.Value)).Return(assert.AnError)
configurationContextMock.EXPECT().Set(string(entry.Key.Key), string(entry.Value)).Return(assert.AnError)

// when
err := sut.Save(testCtx, entry)
Expand Down
6 changes: 3 additions & 3 deletions pkg/adapter/config/etcd/sensitiveDoguConfigRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewSensitiveDoguConfigRepository(etcdStore etcdStore) *SensitiveDoguConfigR
}

func (e SensitiveDoguConfigRepository) Get(_ context.Context, key common.SensitiveDoguConfigKey) (*ecosystem.SensitiveDoguConfigEntry, error) {
entry, err := e.etcdStore.DoguConfig(string(key.DoguName)).Get(key.Key)
entry, err := e.etcdStore.DoguConfig(string(key.DoguName)).Get(string(key.Key))
if registry.IsKeyNotFoundError(err) {
return nil, domainservice.NewNotFoundError(err, "could not find sensitive %s in etcd", key)
} else if err != nil {
Expand All @@ -34,7 +34,7 @@ func (e SensitiveDoguConfigRepository) Get(_ context.Context, key common.Sensiti
func (e SensitiveDoguConfigRepository) Save(_ context.Context, entry *ecosystem.SensitiveDoguConfigEntry) error {
strDoguName := string(entry.Key.DoguName)
strValue := string(entry.Value)
err := setEtcdKey(entry.Key.Key, strValue, e.etcdStore.DoguConfig(strDoguName))
err := setEtcdKey(string(entry.Key.Key), strValue, e.etcdStore.DoguConfig(strDoguName))
if err != nil {
return domainservice.NewInternalError(err, "failed to set encrypted %s with value %q in etcd", entry.Key, strValue)
}
Expand All @@ -44,7 +44,7 @@ func (e SensitiveDoguConfigRepository) Save(_ context.Context, entry *ecosystem.

func (e SensitiveDoguConfigRepository) Delete(_ context.Context, key common.SensitiveDoguConfigKey) error {
strDoguName := string(key.DoguName)
err := deleteEtcdKey(key.Key, e.etcdStore.DoguConfig(strDoguName))
err := deleteEtcdKey(string(key.Key), e.etcdStore.DoguConfig(strDoguName))
if err != nil && !registry.IsKeyNotFoundError(err) {
return domainservice.NewInternalError(err, "failed to delete encrypted %s from etcd", key)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/adapter/config/etcd/sensitiveDoguConfigRepository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestEtcdSensitiveDoguConfigRepository_Delete(t *testing.T) {
sut := SensitiveDoguConfigRepository{etcdStore: etcdMock}
key := common.SensitiveDoguConfigKey{DoguConfigKey: common.DoguConfigKey{Key: "key", DoguName: testSimpleDoguNameRedmine}}
etcdMock.EXPECT().DoguConfig(string(testSimpleDoguNameRedmine)).Return(configurationContextMock)
configurationContextMock.EXPECT().Delete(key.Key).Return(nil)
configurationContextMock.EXPECT().Delete(string(key.Key)).Return(nil)

// when
err := sut.Delete(testCtx, key)
Expand All @@ -32,7 +32,7 @@ func TestEtcdSensitiveDoguConfigRepository_Delete(t *testing.T) {
sut := SensitiveDoguConfigRepository{etcdStore: etcdMock}
key := common.SensitiveDoguConfigKey{DoguConfigKey: common.DoguConfigKey{Key: "key", DoguName: testSimpleDoguNameRedmine}}
etcdMock.EXPECT().DoguConfig(string(testSimpleDoguNameRedmine)).Return(configurationContextMock)
configurationContextMock.EXPECT().Delete(key.Key).Return(etcdNotFoundError)
configurationContextMock.EXPECT().Delete(string(key.Key)).Return(etcdNotFoundError)

// when
err := sut.Delete(testCtx, key)
Expand All @@ -48,7 +48,7 @@ func TestEtcdSensitiveDoguConfigRepository_Delete(t *testing.T) {
sut := SensitiveDoguConfigRepository{etcdStore: etcdMock}
key := common.SensitiveDoguConfigKey{DoguConfigKey: common.DoguConfigKey{Key: "key", DoguName: testSimpleDoguNameRedmine}}
etcdMock.EXPECT().DoguConfig(string(testSimpleDoguNameRedmine)).Return(configurationContextMock)
configurationContextMock.EXPECT().Delete(key.Key).Return(assert.AnError)
configurationContextMock.EXPECT().Delete(string(key.Key)).Return(assert.AnError)

// when
err := sut.Delete(testCtx, key)
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestEtcdSensitiveDoguConfigRepository_Save(t *testing.T) {
Value: "value",
}
etcdMock.EXPECT().DoguConfig(string(testSimpleDoguNameRedmine)).Return(configurationContextMock)
configurationContextMock.EXPECT().Set(entry.Key.Key, string(entry.Value)).Return(nil)
configurationContextMock.EXPECT().Set(string(entry.Key.Key), string(entry.Value)).Return(nil)

// when
err := sut.Save(testCtx, entry)
Expand All @@ -237,7 +237,7 @@ func TestEtcdSensitiveDoguConfigRepository_Save(t *testing.T) {
PersistenceContext: nil,
}
etcdMock.EXPECT().DoguConfig(string(testSimpleDoguNameRedmine)).Return(configurationContextMock)
configurationContextMock.EXPECT().Set(entry.Key.Key, string(entry.Value)).Return(assert.AnError)
configurationContextMock.EXPECT().Set(string(entry.Key.Key), string(entry.Value)).Return(assert.AnError)

// when
err := sut.Save(testCtx, entry)
Expand Down
13 changes: 7 additions & 6 deletions pkg/adapter/kubernetes/blueprintcr/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common"
"github.com/cloudogu/k8s-registry-lib/config"
)

type Config struct {
Expand Down Expand Up @@ -82,7 +83,7 @@ func convertToDoguConfigDTO(config domain.DoguConfig) DoguConfig {
if len(config.Present) != 0 {
present = make(map[string]string, len(config.Present))
for key, value := range config.Present {
present[key.Key] = string(value)
present[string(key.Key)] = string(value)
}
}

Expand All @@ -92,7 +93,7 @@ func convertToDoguConfigDTO(config domain.DoguConfig) DoguConfig {
if len(config.Absent) != 0 {
absent = make([]string, len(config.Absent))
for i, key := range config.Absent {
absent[i] = key.Key
absent[i] = string(key.Key)
}
}

Expand Down Expand Up @@ -132,7 +133,7 @@ func convertToDoguConfigDomain(doguName string, config DoguConfig) domain.DoguCo
func convertToDoguConfigKeyDomain(doguName, key string) common.DoguConfigKey {
return common.DoguConfigKey{
DoguName: common.SimpleDoguName(doguName),
Key: key,
Key: config.Key(key),
}
}

Expand All @@ -143,7 +144,7 @@ func convertToSensitiveDoguConfigDTO(config domain.SensitiveDoguConfig) Sensitiv
if len(config.Present) != 0 {
present = make(map[string]string, len(config.Present))
for key, value := range config.Present {
present[key.Key] = string(value)
present[string(key.Key)] = string(value)
}
}

Expand All @@ -153,7 +154,7 @@ func convertToSensitiveDoguConfigDTO(config domain.SensitiveDoguConfig) Sensitiv
if len(config.Absent) != 0 {
absent = make([]string, len(config.Absent))
for i, key := range config.Absent {
absent[i] = key.Key
absent[i] = string(key.Key)
}
}

Expand Down Expand Up @@ -193,7 +194,7 @@ func convertToSensitiveDoguConfigDomain(doguName string, config SensitiveDoguCon
func convertToSensitiveDoguConfigKeyDomain(doguName, key string) common.SensitiveDoguConfigKey {
return common.SensitiveDoguConfigKey{DoguConfigKey: common.DoguConfigKey{
DoguName: common.SimpleDoguName(doguName),
Key: key,
Key: config.Key(key),
},
}
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/adapter/kubernetes/blueprintcr/v1/doguConfigDiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common"
"github.com/cloudogu/k8s-registry-lib/config"
)

type CombinedDoguConfigDiff struct {
Expand Down Expand Up @@ -56,7 +57,7 @@ func convertToDoguConfigEntryDiffDomain(doguName string, dto DoguConfigEntryDiff
return domain.DoguConfigEntryDiff{
Key: common.DoguConfigKey{
DoguName: common.SimpleDoguName(doguName),
Key: dto.Key,
Key: config.Key(dto.Key),
},
Actual: domain.DoguConfigValueState{
Value: dto.Actual.Value,
Expand All @@ -75,7 +76,7 @@ func convertToSensitiveDoguConfigEntryDiffDomain(doguName string, dto SensitiveD
Key: common.SensitiveDoguConfigKey{
DoguConfigKey: common.DoguConfigKey{
DoguName: common.SimpleDoguName(doguName),
Key: dto.Key,
Key: config.Key(dto.Key),
},
},
Actual: domain.DoguConfigValueState{
Expand Down Expand Up @@ -116,7 +117,7 @@ func convertToCombinedDoguConfigDiffDTO(domainModel domain.CombinedDoguConfigDif

func convertToDoguConfigEntryDiffDTO(domainModel domain.DoguConfigEntryDiff) DoguConfigEntryDiff {
return DoguConfigEntryDiff{
Key: domainModel.Key.Key,
Key: string(domainModel.Key.Key),
Actual: DoguConfigValueState{
Value: domainModel.Actual.Value,
Exists: domainModel.Actual.Exists,
Expand All @@ -131,7 +132,7 @@ func convertToDoguConfigEntryDiffDTO(domainModel domain.DoguConfigEntryDiff) Dog

func convertToSensitiveDoguConfigEntryDiffDTO(domainModel domain.SensitiveDoguConfigEntryDiff) SensitiveDoguConfigEntryDiff {
return SensitiveDoguConfigEntryDiff{
Key: domainModel.Key.Key,
Key: string(domainModel.Key.Key),
Actual: DoguConfigValueState{
Value: domainModel.Actual.Value,
Exists: domainModel.Actual.Exists,
Expand Down
9 changes: 5 additions & 4 deletions pkg/application/ecosystemConfigUseCase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain/ecosystem"
"github.com/cloudogu/k8s-registry-lib/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -532,7 +533,7 @@ func TestNewEcosystemConfigUseCase(t *testing.T) {
func getSetDoguConfigEntryDiff(key, value string, doguName common.SimpleDoguName) domain.DoguConfigEntryDiff {
return domain.DoguConfigEntryDiff{
Key: common.DoguConfigKey{
Key: key,
Key: config.Key(key),
DoguName: doguName,
},
Expected: domain.DoguConfigValueState{
Expand All @@ -545,7 +546,7 @@ func getSetDoguConfigEntryDiff(key, value string, doguName common.SimpleDoguName
func getRemoveDoguConfigEntryDiff(key string, doguName common.SimpleDoguName) domain.DoguConfigEntryDiff {
return domain.DoguConfigEntryDiff{
Key: common.DoguConfigKey{
Key: key,
Key: config.Key(key),
DoguName: doguName,
},
NeededAction: domain.ConfigActionRemove,
Expand All @@ -556,7 +557,7 @@ func getSensitiveDoguConfigEntryDiffForAction(key, value string, doguName common
return domain.SensitiveDoguConfigEntryDiff{
Key: common.SensitiveDoguConfigKey{
DoguConfigKey: common.DoguConfigKey{
Key: key,
Key: config.Key(key),
DoguName: doguName,
},
},
Expand All @@ -571,7 +572,7 @@ func getRemoveSensitiveDoguConfigEntryDiff(key string, doguName common.SimpleDog
return domain.SensitiveDoguConfigEntryDiff{
Key: common.SensitiveDoguConfigKey{
DoguConfigKey: common.DoguConfigKey{
Key: key,
Key: config.Key(key),
DoguName: doguName,
},
},
Expand Down
11 changes: 6 additions & 5 deletions pkg/domain/common/configNames.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package common
import (
"errors"
"fmt"
"github.com/cloudogu/k8s-registry-lib/config"
)

type RegistryConfigKey interface {
GlobalConfigKey | DoguConfigKey | SensitiveDoguConfigKey
}

type GlobalConfigKey string
type GlobalConfigKey = config.Key

type DoguConfigKey struct {
DoguName SimpleDoguName
Key string
Key config.Key
}

func (k DoguConfigKey) Validate() error {
Expand All @@ -37,10 +38,10 @@ type SensitiveDoguConfigKey struct {
}

// GlobalConfigValue is a single global config value
type GlobalConfigValue string
type GlobalConfigValue = config.Value

// DoguConfigValue is a single dogu config value, which is no sensitive configuration
type DoguConfigValue string
type DoguConfigValue = config.Value

// SensitiveDoguConfigValue is a single unencrypted sensitive dogu config value
type SensitiveDoguConfigValue string
type SensitiveDoguConfigValue = config.Value
4 changes: 2 additions & 2 deletions pkg/domain/stateDiffConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func TestCombinedDoguConfigDiff_CensorValues(t *testing.T) {
require.Len(t, result.DoguConfigDiff, 1)

assert.Equal(t, "ldap", string(result.DoguConfigDiff[0].Key.DoguName))
assert.Equal(t, "logging/root", result.DoguConfigDiff[0].Key.Key)
assert.Equal(t, "logging/root", string(result.DoguConfigDiff[0].Key.Key))
assert.Equal(t, "ERROR", result.DoguConfigDiff[0].Actual.Value)
assert.Equal(t, false, result.DoguConfigDiff[0].Actual.Exists)
assert.Equal(t, "DEBUG", result.DoguConfigDiff[0].Expected.Value)
Expand Down Expand Up @@ -486,7 +486,7 @@ func TestCombinedDoguConfigDiff_CensorValues(t *testing.T) {
require.Len(t, result.SensitiveDoguConfigDiff, 1)

assert.Equal(t, "ldap", string(result.SensitiveDoguConfigDiff[0].Key.DoguName))
assert.Equal(t, "logging/root", result.SensitiveDoguConfigDiff[0].Key.Key)
assert.Equal(t, "logging/root", string(result.SensitiveDoguConfigDiff[0].Key.Key))
assert.Equal(t, censorValue, result.SensitiveDoguConfigDiff[0].Actual.Value)
assert.Equal(t, false, result.SensitiveDoguConfigDiff[0].Actual.Exists)
assert.Equal(t, censorValue, result.SensitiveDoguConfigDiff[0].Expected.Value)
Expand Down

0 comments on commit 077c111

Please sign in to comment.