blob: edf6415f9ac9b396401924450715f239ac7f1196 [file] [log] [blame]
David K. Bainbridgef694f5a2016-06-10 16:21:27 -07001package main
2
3import (
David K. Bainbridgef694f5a2016-06-10 16:21:27 -07004 "fmt"
David K. Bainbridgef694f5a2016-06-10 16:21:27 -07005 "net/url"
David K. Bainbridgef694f5a2016-06-10 16:21:27 -07006 "time"
7)
8
9func 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. Bainbridge97ee8052016-06-14 00:52:07 -070016 return NewMemoryStorage()
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070017 default:
18 }
19 return nil, fmt.Errorf("Unknown storage scheme specified, '%s'", u.Scheme)
20}
21
22type Storage interface {
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070023 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. Bainbridge97ee8052016-06-14 00:52:07 -070027 ClearProvisioned(mac string) error
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070028}
29
30type MemoryStorage struct {
David K. Bainbridge97ee8052016-06-14 00:52:07 -070031 Checks map[string]time.Time
32 Times map[string]time.Time
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070033}
34
David K. Bainbridge97ee8052016-06-14 00:52:07 -070035func NewMemoryStorage() (Storage, error) {
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070036
David K. Bainbridge97ee8052016-06-14 00:52:07 -070037 s := MemoryStorage{
38 Checks: make(map[string]time.Time),
39 Times: make(map[string]time.Time),
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070040 }
41 return &s, nil
42}
43
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070044func (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
53func (s *MemoryStorage) MarkMACCheck(mac string, when *time.Time) error {
54 s.Checks[mac] = *when
55 return nil
56}
57
58func (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
67func (s *MemoryStorage) MarkProvisioned(mac string, when *time.Time) error {
68 s.Times[mac] = *when
69 return nil
70}
David K. Bainbridge97ee8052016-06-14 00:52:07 -070071
72func (s *MemoryStorage) ClearProvisioned(mac string) error {
73 delete(s.Times, mac)
74 return nil
75}