blob: 11eded449209e0f43699eaf1594479ea12d9e3ec [file] [log] [blame]
David K. Bainbridge8bc905c2016-05-31 14:07:10 -07001package main
2
3func Allocate(store Storage, mac string) (string, error) {
4 // Check to see if an IP address is already allocated and if so just
5 // return that value
6 ip, err := store.Get(mac)
7 if err != nil {
8 return "", err
9 }
10
11 if ip != "" {
12 return ip, nil
13 }
14
15 // This MAC does not already have an IP assigned, so pull then next
16 // one off the available queue and return it
17 ip, err = store.Dequeue()
18 if err != nil {
19 return "", err
20 }
21 err = store.Put(mac, ip)
22 if err != nil {
23 store.Enqueue(ip)
24 return "", err
25 }
26 return ip, nil
27}
28
29func Release(store Storage, mac string) error {
30 ip, err := store.Remove(mac)
31 if err != nil {
32 return err
33 }
34
35 if ip != "" {
36 err = store.Enqueue(ip)
37 if err != nil {
38 return err
39 }
40 }
41 return nil
42}