-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathvm_region.go
71 lines (59 loc) · 1.59 KB
/
vm_region.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
package lobster
import "errors"
import "sort"
var regionInterfaces map[string]VmInterface = make(map[string]VmInterface)
func vmGetInterface(region string) VmInterface {
vmi, ok := regionInterfaces[region]
if !ok {
panic(errors.New("no interface registered for " + region))
}
return vmi
}
// regions table is used to store transient data about a region
// if a region does not appear in the table, the defaults are assumed:
// * enabled
type Region struct {
Region string
Enabled bool
}
func regionList() []string {
var regions []string
for _, region := range regionListAll() {
if region.Enabled {
regions = append(regions, region.Region)
}
}
sort.Sort(sort.StringSlice(regions))
return regions
}
func regionListAll() []Region {
var regions []Region
seenRegions := make(map[string]bool)
rows := db.Query("SELECT region, enabled FROM regions ORDER BY region")
for rows.Next() {
var region Region
rows.Scan(®ion.Region, ®ion.Enabled)
regions = append(regions, region)
seenRegions[region.Region] = true
}
for region := range regionInterfaces {
if !seenRegions[region] {
regions = append(regions, Region{
Region: region,
Enabled: true,
})
}
}
return regions
}
func regionEnabled(region string) bool {
var count int
db.QueryRow("SELECT COUNT(*) FROM regions WHERE region = ? AND enabled = 0", region).Scan(&count)
return count == 0
}
func enableRegion(region string) {
db.Exec("REPLACE INTO regions (region, enabled) VALUES (?, 1)", region)
}
func disableRegion(region string) {
db.Exec("REPLACE INTO regions (region, enabled) VALUES (?, 0)", region)
}