Skip to content

Commit

Permalink
refactor: make resource create backward compatible (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbchaos authored Mar 7, 2023
1 parent 964c654 commit 159d1ce
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 111 deletions.
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

0 comments on commit 159d1ce

Please sign in to comment.