partition.go 2.1 KB

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