Skip to content

Commit

Permalink
#81 implement repos for dogu config
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-dammeier committed Oct 15, 2024
1 parent ef2f5ec commit b0901c0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
25 changes: 16 additions & 9 deletions pkg/adapter/config/kubernetes/doguConfigRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package kubernetes

import (
"context"
"fmt"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domainservice"
"github.com/cloudogu/k8s-registry-lib/config"
"github.com/cloudogu/k8s-registry-lib/repository"
)
Expand All @@ -13,26 +13,33 @@ type DoguConfigRepository struct {
}

func (e DoguConfigRepository) GetAll(ctx context.Context, doguNames []common.SimpleDoguName) (map[common.SimpleDoguName]config.DoguConfig, error) {
//TODO implement me
panic("implement me")
var configByDogus map[common.SimpleDoguName]config.DoguConfig
for _, doguName := range doguNames {
loaded, err := e.Get(ctx, doguName)
if err != nil {
return nil, fmt.Errorf("could not load config for all given dogus: %w", err)
}
configByDogus[doguName] = loaded
}
return configByDogus, nil
}

func NewDoguConfigRepository(repo repository.DoguConfigRepository) *DoguConfigRepository {
return &DoguConfigRepository{repo: repo}
}

func (e DoguConfigRepository) Get(ctx context.Context, doguName common.SimpleDoguName) (config.DoguConfig, error) {
// TODO: There seems to be no way to know, if we have a NotFoundError or a connection error.
return e.repo.Get(ctx, doguName)
loadedConfig, err := e.repo.Get(ctx, doguName)
if err != nil {
return loadedConfig, fmt.Errorf("could not load dogu config: %w", mapToBlueprintError(err))
}
return loadedConfig, nil
}

func (e DoguConfigRepository) Update(ctx context.Context, config config.DoguConfig) (config.DoguConfig, error) {
updatedConfig, err := e.repo.Update(ctx, config)
// TODO: we cannot see here, if there is a real conflict or there was a connection error.
// With a conflict, we can immediately restart the business process
// With an connection error we need a longer backoff (internalError)
if err != nil {
return config, domainservice.NewInternalError(err, "failed to update config for %s", config.DoguName)
return updatedConfig, fmt.Errorf("could not update dogu config: %w", mapToBlueprintError(err))
}
return updatedConfig, nil
}
2 changes: 1 addition & 1 deletion pkg/adapter/config/kubernetes/errorhandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func mapToBlueprintError(err error) error {
} else if liberrors.IsConnectionError(err) {
return domainservice.NewInternalError(err, "could not load/update config due to connection problems")
} else if liberrors.IsAlreadyExistsError(err) {
return domainservice.NewInternalError(err, "could not create config as it already exists")
return domainservice.NewConflictError(err, "could not create config as it already exists")
} else {
// GenericError and fallback if even that would not match the error
return domainservice.NewInternalError(err, "could not load/update config due to an unknown problem")
Expand Down
36 changes: 36 additions & 0 deletions pkg/adapter/config/kubernetes/errorhandling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package kubernetes

import (
"fmt"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domainservice"
liberrors "github.com/cloudogu/k8s-registry-lib/errors"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_mapToBlueprintError(t *testing.T) {
tests := []struct {
name string
givenError error
errTypeAssert func(err error) bool
}{
{"no error", nil, nil},
{"connectionError", liberrors.NewConnectionError(assert.AnError), domainservice.IsInternalError},
{"notFoundError", liberrors.NewNotFoundError(assert.AnError), domainservice.IsNotFoundError},
{"conflictError", liberrors.NewConflictError(assert.AnError), domainservice.IsConflictError},
{"alreadyExistsError", liberrors.NewAlreadyExistsError(assert.AnError), domainservice.IsConflictError},
{"genericError", liberrors.NewGenericError(assert.AnError), domainservice.IsInternalError},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testString := fmt.Sprintf("mapToBlueprintError(%v)", tt.givenError)
resultError := mapToBlueprintError(tt.givenError)
if tt.givenError != nil {
assert.ErrorContains(t, resultError, tt.givenError.Error(), testString)
}
if tt.errTypeAssert != nil {
assert.True(t, tt.errTypeAssert(resultError), testString)
}
})
}
}
25 changes: 16 additions & 9 deletions pkg/adapter/config/kubernetes/sensitiveDoguConfigRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package kubernetes

import (
"context"
"fmt"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domainservice"
"github.com/cloudogu/k8s-registry-lib/config"
"github.com/cloudogu/k8s-registry-lib/repository"
)
Expand All @@ -13,26 +13,33 @@ type SensitiveDoguConfigRepository struct {
}

func (e SensitiveDoguConfigRepository) GetAll(ctx context.Context, doguNames []common.SimpleDoguName) (map[common.SimpleDoguName]config.DoguConfig, error) {
//TODO implement me
panic("implement me")
var configByDogus map[common.SimpleDoguName]config.DoguConfig
for _, doguName := range doguNames {
loaded, err := e.Get(ctx, doguName)
if err != nil {
return nil, fmt.Errorf("could not load sensitive config for all given dogus: %w", err)
}
configByDogus[doguName] = loaded
}
return configByDogus, nil
}

func NewSensitiveDoguConfigRepository(repo repository.DoguConfigRepository) *SensitiveDoguConfigRepository {
return &SensitiveDoguConfigRepository{repo: repo}
}

func (e SensitiveDoguConfigRepository) Get(ctx context.Context, doguName common.SimpleDoguName) (config.DoguConfig, error) {
// TODO: There seems to be no way to know, if we have a NotFoundError or a connection error.
return e.repo.Get(ctx, doguName)
loadedConfig, err := e.repo.Get(ctx, doguName)
if err != nil {
return loadedConfig, fmt.Errorf("could not load sensitive dogu config: %w", mapToBlueprintError(err))
}
return loadedConfig, nil
}

func (e SensitiveDoguConfigRepository) Update(ctx context.Context, config config.DoguConfig) (config.DoguConfig, error) {
updatedConfig, err := e.repo.Update(ctx, config)
// TODO: we cannot see here, if there is a real conflict or there was a connection error.
// With a conflict, we can immediately restart the business process
// With an connection error we need a longer backoff (internalError)
if err != nil {
return config, domainservice.NewInternalError(err, "failed to update sensitive config for %s", config.DoguName)
return updatedConfig, fmt.Errorf("could not update sensitive dogu config: %w", mapToBlueprintError(err))
}
return updatedConfig, nil
}

0 comments on commit b0901c0

Please sign in to comment.