Skip to content

Commit

Permalink
rename collectIntegrationStats and use request type
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoandredinis committed Dec 20, 2024
1 parent 9924b6d commit 4b35720
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
42 changes: 25 additions & 17 deletions lib/web/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,53 +221,61 @@ func (h *Handler) integrationStats(w http.ResponseWriter, r *http.Request, p htt
return nil, trace.Wrap(err)
}

summary, err := collectAWSOIDCAutoDiscoverStats(r.Context(), h.logger, ig, clt.DiscoveryConfigClient(), clt, clt.IntegrationAWSOIDCClient())
req := collectIntegrationStatsRequest{
logger: h.logger,
integration: ig,
discoveryConfigLister: clt.DiscoveryConfigClient(),
databaseGetter: clt,
awsOIDCClient: clt.IntegrationAWSOIDCClient(),
}
summary, err := collectIntegrationStats(r.Context(), req)
if err != nil {
return nil, trace.Wrap(err)
}

return summary, nil
}

func collectAWSOIDCAutoDiscoverStats(
ctx context.Context,
logger *slog.Logger,
integration types.Integration,
discoveryConfigLister discoveryConfigLister,
databaseGetter databaseGetter,
awsOIDCClient deployedDatabaseServiceLister,
) (*ui.IntegrationWithSummary, error) {
type collectIntegrationStatsRequest struct {
logger *slog.Logger
integration types.Integration
discoveryConfigLister discoveryConfigLister
databaseGetter databaseGetter
awsOIDCClient deployedDatabaseServiceLister
}

func collectIntegrationStats(ctx context.Context, req collectIntegrationStatsRequest) (*ui.IntegrationWithSummary, error) {
ret := &ui.IntegrationWithSummary{}

uiIg, err := ui.MakeIntegration(integration)
uiIg, err := ui.MakeIntegration(req.integration)
if err != nil {
return nil, err
}
ret.Integration = uiIg

var nextPage string
for {
discoveryConfigs, nextToken, err := discoveryConfigLister.ListDiscoveryConfigs(ctx, 0, nextPage)
discoveryConfigs, nextToken, err := req.discoveryConfigLister.ListDiscoveryConfigs(ctx, 0, nextPage)
if err != nil {
return nil, trace.Wrap(err)
}
for _, dc := range discoveryConfigs {
discoveredResources, ok := dc.Status.IntegrationDiscoveredResources[integration.GetName()]
discoveredResources, ok := dc.Status.IntegrationDiscoveredResources[req.integration.GetName()]
if !ok {
continue
}

if matchers := rulesWithIntegration(dc, types.AWSMatcherEC2, integration.GetName()); matchers != 0 {
if matchers := rulesWithIntegration(dc, types.AWSMatcherEC2, req.integration.GetName()); matchers != 0 {
ret.AWSEC2.RulesCount += matchers
mergeResourceTypeSummary(&ret.AWSEC2, dc.Status.LastSyncTime, discoveredResources.AwsEc2)
}

if matchers := rulesWithIntegration(dc, types.AWSMatcherRDS, integration.GetName()); matchers != 0 {
if matchers := rulesWithIntegration(dc, types.AWSMatcherRDS, req.integration.GetName()); matchers != 0 {
ret.AWSRDS.RulesCount += matchers
mergeResourceTypeSummary(&ret.AWSRDS, dc.Status.LastSyncTime, discoveredResources.AwsRds)
}

if matchers := rulesWithIntegration(dc, types.AWSMatcherEKS, integration.GetName()); matchers != 0 {
if matchers := rulesWithIntegration(dc, types.AWSMatcherEKS, req.integration.GetName()); matchers != 0 {
ret.AWSEKS.RulesCount += matchers
mergeResourceTypeSummary(&ret.AWSEKS, dc.Status.LastSyncTime, discoveredResources.AwsEks)
}
Expand All @@ -279,12 +287,12 @@ func collectAWSOIDCAutoDiscoverStats(
nextPage = nextToken
}

regions, err := fetchRelevantAWSRegions(ctx, databaseGetter, discoveryConfigLister)
regions, err := fetchRelevantAWSRegions(ctx, req.databaseGetter, req.discoveryConfigLister)
if err != nil {
return nil, trace.Wrap(err)
}

services, err := listDeployedDatabaseServices(ctx, logger, integration.GetName(), regions, awsOIDCClient)
services, err := listDeployedDatabaseServices(ctx, req.logger, req.integration.GetName(), regions, req.awsOIDCClient)
if err != nil {
return nil, trace.Wrap(err)
}
Expand Down
18 changes: 16 additions & 2 deletions lib/web/integrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ func TestCollectAWSOIDCAutoDiscoverStats(t *testing.T) {
discoveryConfigs: make([]*discoveryconfig.DiscoveryConfig, 0),
}

gotSummary, err := collectAWSOIDCAutoDiscoverStats(ctx, logger, integration, clt, clt, deployedDatabaseServicesClient)
req := collectIntegrationStatsRequest{
logger: logger,
integration: integration,
discoveryConfigLister: clt,
databaseGetter: clt,
awsOIDCClient: deployedDatabaseServicesClient,
}
gotSummary, err := collectIntegrationStats(ctx, req)
require.NoError(t, err)
expectedSummary := &ui.IntegrationWithSummary{
Integration: &ui.Integration{
Expand Down Expand Up @@ -200,7 +207,14 @@ func TestCollectAWSOIDCAutoDiscoverStats(t *testing.T) {
databases: make([]types.Database, 0),
}

gotSummary, err := collectAWSOIDCAutoDiscoverStats(ctx, logger, integration, clt, clt, deployedDatabaseServicesClient)
req := collectIntegrationStatsRequest{
logger: logger,
integration: integration,
discoveryConfigLister: clt,
databaseGetter: clt,
awsOIDCClient: deployedDatabaseServicesClient,
}
gotSummary, err := collectIntegrationStats(ctx, req)
require.NoError(t, err)
expectedSummary := &ui.IntegrationWithSummary{
Integration: &ui.Integration{
Expand Down

0 comments on commit 4b35720

Please sign in to comment.