Skip to content

Commit

Permalink
feat: remove delete resource on deploy api (#222) (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
okysetiawan authored May 14, 2024
1 parent 995884a commit b9dfe58
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 95 deletions.
37 changes: 0 additions & 37 deletions core/resource/service/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,12 @@ func (rs ResourceService) Deploy(ctx context.Context, tnnt tenant.Tenant, store

var toCreate []*resource.Resource
var toUpdate []*resource.Resource
var toDelete []*resource.Resource
for _, r := range toUpdateOnStore {
switch r.Status() {
case resource.StatusToCreate:
toCreate = append(toCreate, r)
case resource.StatusToUpdate:
toUpdate = append(toUpdate, r)
case resource.StatusToDelete:
toDelete = append(toDelete, r)
}
}

Expand All @@ -342,11 +339,6 @@ func (rs ResourceService) Deploy(ctx context.Context, tnnt tenant.Tenant, store
rs.raiseUpdateEvent(r, existingMappedByFullName[r.FullName()].GetUpdateImpact(r))
}

for _, r := range toDelete {
rs.raiseDeleteEvent(r)
logWriter.Write(writer.LogLevelWarning, fmt.Sprintf("[deleted] %s", r.FullName()))
}

if err = rs.handleRefreshDownstream(ctx, toUpdate, existingMappedByFullName, logWriter); err != nil {
multiError.Append(err)
}
Expand All @@ -357,10 +349,8 @@ func (rs ResourceService) Deploy(ctx context.Context, tnnt tenant.Tenant, store
func (rs ResourceService) getResourcesToBatchUpdate(ctx context.Context, incomings []*resource.Resource, existingMappedByFullName map[string]*resource.Resource) ([]*resource.Resource, error) { // nolint:gocritic
var toUpdateOnStore []*resource.Resource
me := errors.NewMultiError("error in resources to batch update")
incomingByFullName := make(map[string]*resource.Resource)

for _, incoming := range incomings {
incomingByFullName[incoming.FullName()] = incoming
if incoming.Status() != resource.StatusValidationSuccess {
continue
}
Expand Down Expand Up @@ -395,20 +385,6 @@ func (rs ResourceService) getResourcesToBatchUpdate(ctx context.Context, incomin
me.Append(err)
}

for _, existing := range existingMappedByFullName {
_, found := incomingByFullName[existing.FullName()]
if found || existing.IsDeleted() {
continue
}

_ = existing.MarkToDelete()
err := rs.repo.Update(ctx, existing)
if err == nil {
toUpdateOnStore = append(toUpdateOnStore, existing)
}
me.Append(err)
}

return toUpdateOnStore, me.ToErr()
}

Expand Down Expand Up @@ -438,19 +414,6 @@ func (rs ResourceService) raiseUpdateEvent(res *resource.Resource, impact resour
rs.eventHandler.HandleEvent(ev)
}

func (rs ResourceService) raiseDeleteEvent(res *resource.Resource) { // nolint:gocritic
if res.Status() != resource.StatusDeleted {
return
}

ev, err := event.NewResourceDeleteEvent(res)
if err != nil {
rs.logger.Error("error creating event for resource delete: %s", err)
return
}
rs.eventHandler.HandleEvent(ev)
}

func (rs ResourceService) handleRefreshDownstream( // nolint:gocritic
ctx context.Context,
incomings []*resource.Resource,
Expand Down
58 changes: 0 additions & 58 deletions core/resource/service/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,64 +835,6 @@ func TestResourceService(t *testing.T) {
assert.NoError(t, actualError)
})

t.Run("returns nil if no error is encountered with a deleted resource", func(t *testing.T) {
existingToCreate := resourceWithStatus("project.dataset.view1", viewSpec, resource.StatusCreateFailure)
existingToSkip := resourceWithStatus("project.dataset.view2", viewSpec, resource.StatusSuccess)
existingToUpdate := resourceWithStatus("project.dataset.view3", viewSpec, resource.StatusUpdateFailure)
existingToDelete := resourceWithStatus("project.dataset.view4", viewSpec, resource.StatusSuccess)
incomingToDelete := resourceWithStatus("project.dataset.view4", viewSpec, resource.StatusToDelete)

updatedViewSpec := map[string]any{
"view_query": "select 1;",
}
incomingToUpdate, err := resource.NewResource("project.dataset.view3", "view", resource.Bigquery, tnnt, meta, updatedViewSpec)
assert.NoError(t, err)
incomingToCreateExisting, resErr := resource.NewResource("project.dataset.view1", "view", resource.Bigquery, tnnt, meta, viewSpec)
assert.NoError(t, resErr)
incomingToSkip, resErr := resource.NewResource("project.dataset.view2", "view", resource.Bigquery, tnnt, meta, viewSpec)
assert.NoError(t, resErr)
incomingToCreate, resErr := resource.NewResource("project.dataset.view5", "view", resource.Bigquery, tnnt, meta, viewSpec)
assert.NoError(t, resErr)

repo := newResourceRepository(t)
repo.On("ReadAll", ctx, tnnt, resource.Bigquery, onlyActive).Return([]*resource.Resource{existingToCreate, existingToSkip, existingToUpdate, existingToDelete}, nil)
repo.On("Create", ctx, incomingToCreate).Return(nil)
repo.On("Update", ctx, incomingToUpdate).Return(nil)
repo.On("Update", ctx, incomingToCreateExisting).Return(nil)
repo.On("Update", ctx, incomingToDelete).Return(nil)

mgr := newResourceManager(t)
mgr.On("Validate", mock.Anything).Return(nil)
mgr.On("GetURN", mock.Anything).Return("bigquery://project:dataset.view1", nil)
mgr.On("BatchUpdate", ctx, resource.Bigquery, []*resource.Resource{incomingToCreate, incomingToUpdate, incomingToCreateExisting, incomingToDelete}).
Run(func(args mock.Arguments) {
res := args.Get(2).([]*resource.Resource)
for _, r := range res {
if r.Status() == resource.StatusToDelete {
r.MarkDeleted()
continue
}
r.MarkSuccess()
}
}).
Return(nil)

eventHandler := newEventHandler(t)
argMatcher := mock.MatchedBy(func(ev moderator.Event) bool {
return ev != nil
})
eventHandler.On("HandleEvent", argMatcher).Return().Times(4)

refresher := new(mockDownstreamRefresher)
refresher.On("RefreshResourceDownstream", ctx, mock.Anything, logWriter).Return(nil)

rscService := service.NewResourceService(logger, repo, refresher, mgr, eventHandler, nil)

incomings := []*resource.Resource{incomingToCreate, incomingToSkip, incomingToUpdate, incomingToCreateExisting}
actualError := rscService.Deploy(ctx, tnnt, resource.Bigquery, incomings, logWriter)
assert.NoError(t, actualError)
})

t.Run("returns nil if no error is encountered with a recreation resource", func(t *testing.T) {
existingToCreate := resourceWithStatus("project.dataset.view1", viewSpec, resource.StatusCreateFailure)
existingToSkip := resourceWithStatus("project.dataset.view2", viewSpec, resource.StatusSuccess)
Expand Down

0 comments on commit b9dfe58

Please sign in to comment.