partition.go

 1package awsrulesfn
 2
 3import "regexp"
 4
 5// Partition provides the metadata describing an AWS partition.
 6type Partition struct {
 7	ID            string                     `json:"id"`
 8	Regions       map[string]RegionOverrides `json:"regions"`
 9	RegionRegex   string                     `json:"regionRegex"`
10	DefaultConfig PartitionConfig            `json:"outputs"`
11}
12
13// PartitionConfig provides the endpoint metadata for an AWS region or partition.
14type PartitionConfig struct {
15	Name                 string `json:"name"`
16	DnsSuffix            string `json:"dnsSuffix"`
17	DualStackDnsSuffix   string `json:"dualStackDnsSuffix"`
18	SupportsFIPS         bool   `json:"supportsFIPS"`
19	SupportsDualStack    bool   `json:"supportsDualStack"`
20	ImplicitGlobalRegion string `json:"implicitGlobalRegion"`
21}
22
23type RegionOverrides struct {
24	Name               *string `json:"name"`
25	DnsSuffix          *string `json:"dnsSuffix"`
26	DualStackDnsSuffix *string `json:"dualStackDnsSuffix"`
27	SupportsFIPS       *bool   `json:"supportsFIPS"`
28	SupportsDualStack  *bool   `json:"supportsDualStack"`
29}
30
31const defaultPartition = "aws"
32
33func getPartition(partitions []Partition, region string) *PartitionConfig {
34	for _, partition := range partitions {
35		if v, ok := partition.Regions[region]; ok {
36			p := mergeOverrides(partition.DefaultConfig, v)
37			return &p
38		}
39	}
40
41	for _, partition := range partitions {
42		regionRegex := regexp.MustCompile(partition.RegionRegex)
43		if regionRegex.MatchString(region) {
44			v := partition.DefaultConfig
45			return &v
46		}
47	}
48
49	for _, partition := range partitions {
50		if partition.ID == defaultPartition {
51			v := partition.DefaultConfig
52			return &v
53		}
54	}
55
56	return nil
57}
58
59func mergeOverrides(into PartitionConfig, from RegionOverrides) PartitionConfig {
60	if from.Name != nil {
61		into.Name = *from.Name
62	}
63	if from.DnsSuffix != nil {
64		into.DnsSuffix = *from.DnsSuffix
65	}
66	if from.DualStackDnsSuffix != nil {
67		into.DualStackDnsSuffix = *from.DualStackDnsSuffix
68	}
69	if from.SupportsFIPS != nil {
70		into.SupportsFIPS = *from.SupportsFIPS
71	}
72	if from.SupportsDualStack != nil {
73		into.SupportsDualStack = *from.SupportsDualStack
74	}
75	return into
76}