David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | func 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 | |
| 29 | func 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 | } |