-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
76 lines (69 loc) · 1.76 KB
/
main.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
72
73
74
75
76
package main
import (
"log"
"net"
"os"
"github.com/maxmind/mmdbwriter"
"github.com/maxmind/mmdbwriter/inserter"
"github.com/maxmind/mmdbwriter/mmdbtype"
)
func main() {
// Load the database we wish to enrich.
writer, err := mmdbwriter.Load("GeoLite2-Country.mmdb", mmdbwriter.Options{})
if err != nil {
log.Fatal(err)
}
// Define and insert the new data.
_, sreNet, err := net.ParseCIDR("56.0.0.0/16")
if err != nil {
log.Fatal(err)
}
sreData := mmdbtype.Map{
"AcmeCorp.DeptName": mmdbtype.String("SRE"),
"AcmeCorp.Environments": mmdbtype.Slice{
mmdbtype.String("development"),
mmdbtype.String("staging"),
mmdbtype.String("production"),
},
}
if err := writer.InsertFunc(sreNet, inserter.TopLevelMergeWith(sreData)); err != nil {
log.Fatal(err)
}
_, devNet, err := net.ParseCIDR("56.1.0.0/16")
if err != nil {
log.Fatal(err)
}
devData := mmdbtype.Map{
"AcmeCorp.DeptName": mmdbtype.String("Development"),
"AcmeCorp.Environments": mmdbtype.Slice{
mmdbtype.String("development"),
mmdbtype.String("staging"),
},
}
if err := writer.InsertFunc(devNet, inserter.TopLevelMergeWith(devData)); err != nil {
log.Fatal(err)
}
_, mgmtNet, err := net.ParseCIDR("56.2.0.0/16")
if err != nil {
log.Fatal(err)
}
mgmtData := mmdbtype.Map{
"AcmeCorp.DeptName": mmdbtype.String("Management"),
"AcmeCorp.Environments": mmdbtype.Slice{
mmdbtype.String("development"),
mmdbtype.String("staging"),
},
}
if err := writer.InsertFunc(mgmtNet, inserter.TopLevelMergeWith(mgmtData)); err != nil {
log.Fatal(err)
}
// Write the newly enriched DB to the filesystem.
fh, err := os.Create("GeoLite2-Country-with-Department-Data.mmdb")
if err != nil {
log.Fatal(err)
}
_, err = writer.WriteTo(fh)
if err != nil {
log.Fatal(err)
}
}