Skip to content

Commit

Permalink
feat: improve fetch resource job
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifosmin Simon committed Nov 11, 2024
1 parent 37f2b8c commit 2554d70
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
6 changes: 3 additions & 3 deletions core/provider/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func normalizeDetails(details map[string]interface{}) (map[string]interface{}, e
return normalized, nil
}

func compareResources(existingResource, newResource domain.Resource) bool {
func compareResources(existingResource, newResource domain.Resource) (bool, string) {
opts := cmp.Options{
cmpopts.IgnoreFields(domain.Resource{}, "ID", "CreatedAt", "UpdatedAt"),
cmpopts.SortSlices(func(x, y map[string]any) bool {
Expand All @@ -67,9 +67,9 @@ func compareResources(existingResource, newResource domain.Resource) bool {
existingResource.Details = normalizedExistingDetails
newResource.Details = normalizedNewDetails
if diff := cmp.Diff(existingResource, newResource, opts); diff != "" {
return true
return true, diff
}
return false
return false, ""
}

type UnimplementedClient struct{}
Expand Down
15 changes: 11 additions & 4 deletions core/provider/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,8 @@ func (s *Service) getResources(ctx context.Context, p *domain.Provider) ([]*doma

existingProviderResources := map[string]bool{}

isResourceUpdated := 0
isResourceUpdated := false
UpdatedResourceCount := 0
for _, newResource := range flattenedProviderResources {
for _, existingResource := range existingGuardianResources {
if existingResource.Type == newResource.Type && existingResource.URN == newResource.URN {
Expand All @@ -627,18 +628,24 @@ func (s *Service) getResources(ctx context.Context, p *domain.Provider) ([]*doma
} else {
newResource.Details = existingDetails
}
if isUpdated := compareResources(*existingResource, *newResource); isUpdated {
isResourceUpdated++
if isUpdated, diff := compareResources(*existingResource, *newResource); isUpdated {
s.logger.Info(ctx, "diff", "resources", diff)
UpdatedResourceCount++
isResourceUpdated = true
s.logger.Info(ctx, "Resources is updated", "resource", newResource.Name)
}
}
existingProviderResources[existingResource.ID] = true
break
}
}
}
if isResourceUpdated == 0 && len(existingGuardianResources) == len(flattenedProviderResources) {
if isResourceUpdated && len(existingGuardianResources) == len(flattenedProviderResources) {
return []*domain.Resource{}, nil
}
s.logger.Info(ctx, "Existing Resource", "Count", len(existingGuardianResources))
s.logger.Info(ctx, "New Resource", "Count", len(flattenedProviderResources))
s.logger.Info(ctx, "Updated Resource", "Count", UpdatedResourceCount)

// mark IsDeleted of guardian resources that no longer exist in provider
updatedResources := []*domain.Resource{}
Expand Down

0 comments on commit 2554d70

Please sign in to comment.