55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
package qaopsorg
|
|
|
|
import "unicode/utf8"
|
|
|
|
// IDs are immutable mapping keys; names are editable labels. Existing keys remain
|
|
// intact when migrating the historical four-region configuration.
|
|
type RegionDefinition struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func DefaultRegions() []RegionDefinition {
|
|
result := make([]RegionDefinition, 0, len(RegionNames))
|
|
for _, name := range RegionNames {
|
|
result = append(result, RegionDefinition{ID: name, Name: name})
|
|
}
|
|
return result
|
|
}
|
|
func (s State) RegionDefinitions() []RegionDefinition {
|
|
if s.Regions == nil {
|
|
return DefaultRegions()
|
|
}
|
|
return s.Regions
|
|
}
|
|
func (s State) RegionLabels() []string {
|
|
labels := []string{}
|
|
for _, region := range s.RegionDefinitions() {
|
|
labels = append(labels, region.Name)
|
|
}
|
|
return labels
|
|
}
|
|
func (s State) RegionLabel(id string) string {
|
|
for _, region := range s.RegionDefinitions() {
|
|
if region.ID == id {
|
|
return region.Name
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
func validRegions(regions []RegionDefinition) bool {
|
|
if len(regions) > 100 {
|
|
return false
|
|
}
|
|
reserved := map[string]bool{"unassigned": true, "cross_region": true, "unmatched": true, "identity_conflict": true, "no_identity": true, "directory_missing": true, "__proto__": true, "constructor": true, "prototype": true}
|
|
ids, names := map[string]bool{}, map[string]bool{}
|
|
for _, region := range regions {
|
|
if !validID(region.ID) || !validID(region.Name) || !utf8.ValidString(region.ID) || !utf8.ValidString(region.Name) || reserved[region.ID] || reserved[region.Name] || ids[region.ID] || names[region.Name] {
|
|
return false
|
|
}
|
|
ids[region.ID] = true
|
|
names[region.Name] = true
|
|
}
|
|
return true
|
|
}
|