partition.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. }
  18. type RegionOverrides struct {
  19. Name *string `json:"name"`
  20. DnsSuffix *string `json:"dnsSuffix"`
  21. DualStackDnsSuffix *string `json:"dualStackDnsSuffix"`
  22. SupportsFIPS *bool `json:"supportsFIPS"`
  23. SupportsDualStack *bool `json:"supportsDualStack"`
  24. }
  25. const defaultPartition = "aws"
  26. func getPartition(partitions []Partition, region string) *PartitionConfig {
  27. for _, partition := range partitions {
  28. if v, ok := partition.Regions[region]; ok {
  29. p := mergeOverrides(partition.DefaultConfig, v)
  30. return &p
  31. }
  32. }
  33. for _, partition := range partitions {
  34. regionRegex := regexp.MustCompile(partition.RegionRegex)
  35. if regionRegex.MatchString(region) {
  36. v := partition.DefaultConfig
  37. return &v
  38. }
  39. }
  40. for _, partition := range partitions {
  41. if partition.ID == defaultPartition {
  42. v := partition.DefaultConfig
  43. return &v
  44. }
  45. }
  46. return nil
  47. }
  48. func mergeOverrides(into PartitionConfig, from RegionOverrides) PartitionConfig {
  49. if from.Name != nil {
  50. into.Name = *from.Name
  51. }
  52. if from.DnsSuffix != nil {
  53. into.DnsSuffix = *from.DnsSuffix
  54. }
  55. if from.DualStackDnsSuffix != nil {
  56. into.DualStackDnsSuffix = *from.DualStackDnsSuffix
  57. }
  58. if from.SupportsFIPS != nil {
  59. into.SupportsFIPS = *from.SupportsFIPS
  60. }
  61. if from.SupportsDualStack != nil {
  62. into.SupportsDualStack = *from.SupportsDualStack
  63. }
  64. return into
  65. }