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