src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

partition.go (2083B)


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