Skip to content

Commit

Permalink
extract loops
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoandredinis committed Dec 10, 2024
1 parent bc12439 commit 39dfbe7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
91 changes: 56 additions & 35 deletions lib/web/integrations_awsoidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"encoding/json"
"fmt"
"log/slog"
"maps"
"net/http"
"net/url"
"slices"
Expand Down Expand Up @@ -287,15 +288,16 @@ func (h *Handler) awsOIDCListDeployedDatabaseService(w http.ResponseWriter, r *h
}, nil
}

func fetchRelevantAWSRegions(ctx context.Context,
authClient interface {
GetResources(ctx context.Context, req *proto.ListResourcesRequest) (*proto.ListResourcesResponse, error)
GetDatabases(context.Context) ([]types.Database, error)
},
discoveryConfigsClient interface {
ListDiscoveryConfigs(ctx context.Context, pageSize int, nextToken string) ([]*discoveryconfig.DiscoveryConfig, string, error)
},
) ([]string, error) {
type databaseGetter interface {
GetResources(ctx context.Context, req *proto.ListResourcesRequest) (*proto.ListResourcesResponse, error)
GetDatabases(context.Context) ([]types.Database, error)
}

type discoveryConfigLister interface {
ListDiscoveryConfigs(ctx context.Context, pageSize int, nextToken string) ([]*discoveryconfig.DiscoveryConfig, string, error)
}

func fetchRelevantAWSRegions(ctx context.Context, authClient databaseGetter, discoveryConfigsClient discoveryConfigLister) ([]string, error) {
regionsSet := make(map[string]struct{})

// Collect Regions from Database resources.
Expand All @@ -321,21 +323,9 @@ func fetchRelevantAWSRegions(ctx context.Context,
if err != nil {
return nil, trace.Wrap(err)
}
for _, resource := range page.Resources {
for _, matcher := range resource.GetResourceMatchers() {
if matcher.Labels == nil {
continue
}
for labelKey, labelValues := range *matcher.Labels {
if labelKey != types.DiscoveryLabelRegion {
continue
}
for _, labelValue := range labelValues {
regionsSet[labelValue] = struct{}{}
}
}
}
}

maps.Copy(regionsSet, extractRegionsFromDatabaseServicesPage(page.Resources))

if page.NextKey == "" {
break
}
Expand All @@ -350,13 +340,7 @@ func fetchRelevantAWSRegions(ctx context.Context,
return nil, trace.Wrap(err)
}

for _, dc := range resp {
for _, awsMatcher := range dc.Spec.AWS {
for _, region := range awsMatcher.Regions {
regionsSet[region] = struct{}{}
}
}
}
maps.Copy(regionsSet, extractRegionsFromDiscoveryConfigPage(resp))

if respNextPageKey == "" {
break
Expand All @@ -375,19 +359,56 @@ func fetchRelevantAWSRegions(ctx context.Context,
return ret, nil
}

func extractRegionsFromDatabaseServicesPage(dbServices []types.DatabaseService) map[string]struct{} {
regionsSet := make(map[string]struct{})
for _, resource := range dbServices {
for _, matcher := range resource.GetResourceMatchers() {
if matcher.Labels == nil {
continue
}
for labelKey, labelValues := range *matcher.Labels {
if labelKey != types.DiscoveryLabelRegion {
continue
}
for _, labelValue := range labelValues {
regionsSet[labelValue] = struct{}{}
}
}
}
}

return regionsSet
}

func extractRegionsFromDiscoveryConfigPage(discoveryConfigs []*discoveryconfig.DiscoveryConfig) map[string]struct{} {
regionsSet := make(map[string]struct{})

for _, dc := range discoveryConfigs {
for _, awsMatcher := range dc.Spec.AWS {
for _, region := range awsMatcher.Regions {
regionsSet[region] = struct{}{}
}
}
}

return regionsSet
}

type deployedDatabaseServiceLister interface {
ListDeployedDatabaseServices(ctx context.Context, in *integrationv1.ListDeployedDatabaseServicesRequest, opts ...grpc.CallOption) (*integrationv1.ListDeployedDatabaseServicesResponse, error)
}

func listDeployedDatabaseServices(ctx context.Context,
logger *slog.Logger,
integrationName string,
regions []string,
awsoidcClient interface {
ListDeployedDatabaseServices(ctx context.Context, in *integrationv1.ListDeployedDatabaseServicesRequest, opts ...grpc.CallOption) (*integrationv1.ListDeployedDatabaseServicesResponse, error)
},
awsOIDCClient deployedDatabaseServiceLister,
) ([]ui.AWSOIDCDeployedDatabaseService, error) {
var services []ui.AWSOIDCDeployedDatabaseService
for _, region := range regions {
var nextToken string
for {
resp, err := awsoidcClient.ListDeployedDatabaseServices(ctx, &integrationv1.ListDeployedDatabaseServicesRequest{
resp, err := awsOIDCClient.ListDeployedDatabaseServices(ctx, &integrationv1.ListDeployedDatabaseServicesRequest{
Integration: integrationName,
Region: region,
NextToken: nextToken,
Expand Down
2 changes: 1 addition & 1 deletion lib/web/ui/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ type AWSOIDCDeployDatabaseServiceResponse struct {
ClusterDashboardURL string `json:"clusterDashboardUrl"`
}

// AWSOIDCDeployedDatabaseService represents an Teleport Database Service that is deployed in Amazon ECS.
// AWSOIDCDeployedDatabaseService represents a Teleport Database Service that is deployed in Amazon ECS.
type AWSOIDCDeployedDatabaseService struct {
// Name is the ECS Service name.
Name string `json:"name,omitempty"`
Expand Down

0 comments on commit 39dfbe7

Please sign in to comment.