Skip to content

Commit

Permalink
Add 'endpoints.PartitionForRegion'.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Sep 17, 2024
1 parent 092a8bb commit 80d4e38
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
33 changes: 30 additions & 3 deletions endpoints/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package endpoints

import (
"maps"
"regexp"
)

Expand Down Expand Up @@ -36,13 +37,39 @@ func (p Partition) RegionRegex() *regexp.Regexp {
return p.regionRegex
}

// Regions returns a map of Regions for the partition, indexed by their ID.
func (p Partition) Regions() map[string]Region {
partitionAndRegion, ok := partitionsAndRegions[p.id]
if !ok {
return nil
}

return maps.Clone(partitionAndRegion.regions)
}

// DefaultPartitions returns a list of the partitions.
func DefaultPartitions() []Partition {
partitions := make([]Partition, len(partitionsAndRegions))
ps := make([]Partition, len(partitionsAndRegions))

for _, v := range partitionsAndRegions {
partitions = append(partitions, v.partition)
ps = append(ps, v.partition)
}

return ps
}

// PartitionForRegion returns the first partition which includes the specific Region.
func PartitionForRegion(ps []Partition, regionID string) (Partition, bool) {
for _, p := range ps {
partitionAndRegion, ok := partitionsAndRegions[p.id]
if !ok {
continue
}

if _, ok := partitionAndRegion.regions[regionID]; ok || partitionAndRegion.partition.regionRegex.MatchString(regionID) {
return p, true
}
}

return partitions
return Partition{}, false
}
38 changes: 38 additions & 0 deletions endpoints/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,41 @@ func TestDefaultPartitions(t *testing.T) {
t.Fatalf("expected partitions, got none")
}
}

func TestPartitionForRegion(t *testing.T) {
t.Parallel()

testcases := map[string]struct {
expectedFound bool
expectedID string
}{
"us-east-1": {
expectedFound: true,
expectedID: "aws",
},
"us-gov-west-1": {
expectedFound: true,
expectedID: "aws-us-gov",
},
"not-found": {
expectedFound: false,
},
"us-east-17": {
expectedFound: true,
expectedID: "aws",
},
}

ps := endpoints.DefaultPartitions()
for region, testcase := range testcases {
gotID, gotFound := endpoints.PartitionForRegion(ps, region)

if gotFound != testcase.expectedFound {
t.Errorf("expected PartitionFound %t for Region %q, got %t", testcase.expectedFound, region, gotFound)
}
if gotID.ID() != testcase.expectedID {
t.Errorf("expected PartitionID %q for Region %q, got %q", testcase.expectedID, region, gotID.ID())
}
}

}

0 comments on commit 80d4e38

Please sign in to comment.