Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make resource create backward compatible #754

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/resource/service/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (m *ResourceMgr) CreateResource(ctx context.Context, res *resource.Resource
me.Append(res.MarkExistInStore())
} else {
me.Append(res.MarkFailure())
me.Append(err)
}
me.Append(err)
} else {
me.Append(res.MarkSuccess())
}
Expand Down
2 changes: 1 addition & 1 deletion core/resource/service/resource_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestResourceManager(t *testing.T) {
manager.RegisterDatastore(store, storeService)

err = manager.CreateResource(ctx, createRequest)
assert.Error(t, err)
assert.NoError(t, err)
})
t.Run("creates the resource on the datastore", func(t *testing.T) {
spec := map[string]any{"description": "test spec"}
Expand Down
26 changes: 10 additions & 16 deletions core/resource/service/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,18 @@ type ResourceManager interface {
GetURN(res *resource.Resource) (string, error)
}

type TenantDetailsGetter interface {
GetDetails(ctx context.Context, tnnt tenant.Tenant) (*tenant.WithDetails, error)
}

type ResourceService struct {
repo ResourceRepository
mgr ResourceManager
tnntDetailsGetter TenantDetailsGetter
repo ResourceRepository
mgr ResourceManager

logger log.Logger
}

func NewResourceService(logger log.Logger, repo ResourceRepository, mgr ResourceManager, tnntDetailsGetter TenantDetailsGetter) *ResourceService {
func NewResourceService(logger log.Logger, repo ResourceRepository, mgr ResourceManager) *ResourceService {
return &ResourceService{
repo: repo,
mgr: mgr,
tnntDetailsGetter: tnntDetailsGetter,
logger: logger,
repo: repo,
mgr: mgr,
logger: logger,
}
}

Expand All @@ -70,17 +64,17 @@ func (rs ResourceService) Create(ctx context.Context, incoming *resource.Resourc
return err
}

if _, err := rs.tnntDetailsGetter.GetDetails(ctx, incoming.Tenant()); err != nil {
rs.logger.Error("error getting tenant for resource [%s] details: %s", incoming.FullName(), err)
return err
}
incoming.MarkToCreate()

if err := rs.repo.Create(ctx, incoming); err != nil {
rs.logger.Error("error creating resource [%s] to db: %s", incoming.FullName(), err)
return err
}
} else {
if existing.Status() == resource.StatusSuccess || existing.Status() == resource.StatusExistInStore {
return nil // Note: return in case resource already exists
}

if !resource.StatusForToCreate(existing.Status()) {
msg := fmt.Sprintf("cannot create resource [%s] since it already exists with status [%s]", incoming.FullName(), existing.Status())
rs.logger.Error(msg)
Expand Down
Loading