-
Notifications
You must be signed in to change notification settings - Fork 0
/
haci.go
168 lines (129 loc) · 3.83 KB
/
haci.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package ipam
import (
"fmt"
"net"
log "github.com/sirupsen/logrus"
"github.com/Nexinto/go-haci-client/haci"
)
type HaCi struct {
HaCi haci.Client
// Address assignments are tagged with this so we do not touch anything else
Tag string
IpamData
}
func NewHaciIpamWithClient(client haci.Client, network, tag string) (*HaCi, error) {
_, nn, err := net.ParseCIDR(network)
if err != nil {
return nil, fmt.Errorf("could not parse network '%s': %s", network, err.Error())
}
return &HaCi{HaCi: client, Tag: tag, IpamData: IpamData{Network: nn}}, nil
}
func NewHaciIpam(network, url, username, password, root, tag string) (*HaCi, error) {
h, err := haci.NewWebClient(url, username, password, root)
if err != nil {
return nil, fmt.Errorf("could not create HaCi client: %s", err.Error())
}
return NewHaciIpamWithClient(h, network, tag)
}
func (c *HaCi) Reset() error {
return c.HaCi.Reset()
}
func (c *HaCi) Assign(description string) (string, error) {
n, err := c.HaCi.Assign(c.Network.String(), description, 32, []string{c.Tag})
if err != nil {
return "", fmt.Errorf("could not assign new network in %s: %s", c.Network.String(), err.Error())
}
ip, err := n.IP()
if err != nil {
return "", fmt.Errorf("could not parse result %s from haci: %s", n, err.Error())
}
return ip, nil
}
func (c *HaCi) IsAssigned(ip string) (bool, error) {
_, err := c.HaCi.Get(ip + "/32")
return err == nil, nil
}
func (c *HaCi) Unassign(ip string) error {
n, err := c.HaCi.Get(ip + "/32")
if err != nil {
return fmt.Errorf("could not get %s from HaCi (to check before deleting): %s", ip, err.Error())
}
if len(n.Tags) != 1 || n.Tags[0] != c.Tag {
return fmt.Errorf("will not unassign %s in HaCi as it is not managed by us", ip)
}
return c.HaCi.Delete(ip + "/32")
}
func (c *HaCi) Get(ip string) (string, error) {
n, err := c.HaCi.Get(ip + "/32")
if err != nil {
return "", fmt.Errorf("could not get %s from HaCi: %s", ip, err.Error())
}
return n.Description, nil
}
func (c *HaCi) Cleanup(keep []string) error {
networks, err := c.HaCi.List(c.Network.String())
if err != nil {
return fmt.Errorf("error listing supernet %s in HaCi: %s", c.Network.String(), err.Error())
}
k := map[string]bool{}
for _, ip := range keep {
k[ip] = true
}
for _, n := range networks {
ip, err := n.IP()
if err != nil {
return fmt.Errorf("while cleaning up %s in HaCi, a network address could not be parsed (%v): %s", c.Network.String(), n, err.Error())
}
if len(n.Tags) != 1 || n.Tags[0] != c.Tag {
log.Debugf("Ignoring %s as it does not have our tag", n.Network)
continue
}
if !k[ip] {
log.Infof("Deleting %s from HaCi", n.Network)
c.HaCi.Delete(n.Network)
}
}
return nil
}
func (c *HaCi) Search(search string, exact bool) ([]string, error) {
nn, err := c.HaCi.Search(search, exact)
if err != nil {
return []string{}, fmt.Errorf("could not search for '%s' in HaCi: %s", search, err.Error())
}
found := []string{}
for _, n := range nn {
ip, _ := n.IP()
found = append(found, ip)
}
return found, nil
}
func (c *HaCi) List() ([]string, error) {
nn, err := c.HaCi.List(c.Network.String())
if err != nil {
return []string{}, fmt.Errorf("could list supernet '%s' in HaCi: %s", c.Network.String(), err.Error())
}
var found []string
for _, n := range nn {
ip, _ := n.IP()
found = append(found, ip)
}
return found, nil
}
func (c *HaCi) Set(ip string, description string) error {
desc, err := c.Get(ip)
if err == nil { // found
if desc != description {
return fmt.Errorf("address %s is already assigned to %s", ip, desc)
} else {
return nil
}
}
err = c.HaCi.Add(ip+"/32", description, []string{c.Tag})
if err != nil {
return fmt.Errorf("error setting address %s: %s", ip, err.Error())
}
return nil
}
func (c *HaCi) String() string {
return fmt.Sprintf("%s tag %s network %s", c.HaCi.String(), c.Tag, c.Network)
}