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

feat(provider): fetch resources on provider update #16

Merged
merged 3 commits into from
Apr 6, 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
23 changes: 21 additions & 2 deletions core/provider/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (s *Service) Create(ctx context.Context, p *domain.Provider) error {
}

go func() {
s.logger.Info("fetching resources", "provider_urn", p.URN)
s.logger.Info("provider create fetching resources", "provider_urn", p.URN)
ctx := audit.WithActor(context.Background(), domain.SystemActorName)
resources, err := s.getResources(ctx, p)
if err != nil {
Expand Down Expand Up @@ -199,7 +199,9 @@ func (s *Service) Update(ctx context.Context, p *domain.Provider) error {
return err
}

if !isDryRun(ctx) {
dryRun := isDryRun(ctx)

if !dryRun {
if err := s.repository.Update(ctx, p); err != nil {
return err
}
Expand All @@ -209,6 +211,23 @@ func (s *Service) Update(ctx context.Context, p *domain.Provider) error {
}
}

go func() {
s.logger.Info("provider update fetching resources", "provider_urn", p.URN)
ctx := audit.WithActor(context.Background(), domain.SystemActorName)
resources, err := s.getResources(ctx, p)
if err != nil {
s.logger.Error("failed to fetch resources", "error", err)
}

if !dryRun {
if err := s.resourceService.BulkUpsert(ctx, resources); err != nil {
s.logger.Error("failed to insert resources to db", "error", err)
} else {
s.logger.Info("resources added", "provider_urn", p.URN, "count", len(resources))
}
}
}()

return nil
}

Expand Down
15 changes: 15 additions & 0 deletions core/provider/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ func (s *ServiceTestSuite) TestUpdate() {
s.mockProviderRepository.EXPECT().Update(mock.AnythingOfType("*context.emptyCtx"), tc.expectedNewProvider).Return(nil)
s.mockAuditLogger.On("Log", mock.Anything, provider.AuditKeyUpdate, mock.Anything).Return(nil).Once()

expectedResources := []*domain.Resource{}
s.mockResourceService.On("Find", mock.Anything, domain.ListResourcesFilter{
ProviderType: tc.updatePayload.Type,
ProviderURN: tc.updatePayload.URN,
}).Return([]*domain.Resource{}, nil).Once()
s.mockProvider.On("GetResources", tc.updatePayload.Config).Return(expectedResources, nil).Once()
s.mockResourceService.On("BulkUpsert", mock.Anything, expectedResources).Return(nil).Once()

actualError := s.service.Update(context.Background(), tc.updatePayload)

s.Nil(actualError)
Expand Down Expand Up @@ -310,6 +318,13 @@ func (s *ServiceTestSuite) TestUpdate() {

ctx := provider.WithDryRun(context.Background())

expectedResources := []*domain.Resource{}
s.mockResourceService.On("Find", mock.Anything, domain.ListResourcesFilter{
ProviderType: p.Type,
ProviderURN: p.URN,
}).Return([]*domain.Resource{}, nil).Once()
s.mockProvider.On("GetResources", p.Config).Return(expectedResources, nil).Once()

actualError := s.service.Update(ctx, p)

s.Nil(actualError)
Expand Down