David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 4 | "fmt" |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 5 | "net/url" |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func NewStorage(spec string) (Storage, error) { |
| 10 | u, err := url.Parse(spec) |
| 11 | if err != nil { |
| 12 | return nil, err |
| 13 | } |
| 14 | switch u.Scheme { |
| 15 | case "memory": |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame^] | 16 | return NewMemoryStorage() |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 17 | default: |
| 18 | } |
| 19 | return nil, fmt.Errorf("Unknown storage scheme specified, '%s'", u.Scheme) |
| 20 | } |
| 21 | |
| 22 | type Storage interface { |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 23 | LastMACCheck(mac string) (*time.Time, error) |
| 24 | MarkMACCheck(mac string, when *time.Time) error |
| 25 | LastProvisioned(mac string) (*time.Time, error) |
| 26 | MarkProvisioned(mac string, when *time.Time) error |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame^] | 27 | ClearProvisioned(mac string) error |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | type MemoryStorage struct { |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame^] | 31 | Checks map[string]time.Time |
| 32 | Times map[string]time.Time |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 33 | } |
| 34 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame^] | 35 | func NewMemoryStorage() (Storage, error) { |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 36 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame^] | 37 | s := MemoryStorage{ |
| 38 | Checks: make(map[string]time.Time), |
| 39 | Times: make(map[string]time.Time), |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 40 | } |
| 41 | return &s, nil |
| 42 | } |
| 43 | |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 44 | func (s *MemoryStorage) LastMACCheck(mac string) (*time.Time, error) { |
| 45 | when, ok := s.Checks[mac] |
| 46 | if !ok { |
| 47 | return nil, nil |
| 48 | } |
| 49 | result := when |
| 50 | return &result, nil |
| 51 | } |
| 52 | |
| 53 | func (s *MemoryStorage) MarkMACCheck(mac string, when *time.Time) error { |
| 54 | s.Checks[mac] = *when |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | func (s *MemoryStorage) LastProvisioned(mac string) (*time.Time, error) { |
| 59 | when, ok := s.Times[mac] |
| 60 | if !ok { |
| 61 | return nil, nil |
| 62 | } |
| 63 | result := when |
| 64 | return &result, nil |
| 65 | } |
| 66 | |
| 67 | func (s *MemoryStorage) MarkProvisioned(mac string, when *time.Time) error { |
| 68 | s.Times[mac] = *when |
| 69 | return nil |
| 70 | } |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame^] | 71 | |
| 72 | func (s *MemoryStorage) ClearProvisioned(mac string) error { |
| 73 | delete(s.Times, mac) |
| 74 | return nil |
| 75 | } |