update switchq to calling provisioner, fixed a few bugs found while testing at on.labs
Change-Id: I2367669aa54f680b98ff0cbbc8d41a49fb7e7a79
diff --git a/switchq/storage.go b/switchq/storage.go
index d4ec36d..edf6415 100644
--- a/switchq/storage.go
+++ b/switchq/storage.go
@@ -1,12 +1,8 @@
package main
import (
- "encoding/json"
"fmt"
- "log"
"net/url"
- "os"
- "strings"
"time"
)
@@ -17,73 +13,34 @@
}
switch u.Scheme {
case "memory":
- return NewMemoryStorage(u)
+ return NewMemoryStorage()
default:
}
return nil, fmt.Errorf("Unknown storage scheme specified, '%s'", u.Scheme)
}
type Storage interface {
- Switchq(mac string) (bool, error)
LastMACCheck(mac string) (*time.Time, error)
MarkMACCheck(mac string, when *time.Time) error
LastProvisioned(mac string) (*time.Time, error)
MarkProvisioned(mac string, when *time.Time) error
-}
-
-type VendorRec struct {
- Prefix string `json:"prefix"`
- Vendor string `json:"vendor"`
- Provision bool `json:"provision"`
+ ClearProvisioned(mac string) error
}
type MemoryStorage struct {
- Vendors map[string]VendorRec
- Checks map[string]time.Time
- Times map[string]time.Time
+ Checks map[string]time.Time
+ Times map[string]time.Time
}
-func NewMemoryStorage(u *url.URL) (Storage, error) {
+func NewMemoryStorage() (Storage, error) {
- s := MemoryStorage{}
- s.Vendors = make(map[string]VendorRec)
-
- if u.Path != "" {
- file, err := os.Open(u.Path)
- if err != nil {
- return nil, err
- }
- defer file.Close()
-
- data := make([]VendorRec, 0)
- decoder := json.NewDecoder(file)
- err = decoder.Decode(&data)
- if err != nil {
- return nil, err
- }
- for _, rec := range data {
- s.Vendors[rec.Prefix] = rec
- }
- log.Printf("[debug] %v", s.Vendors)
-
- } else {
- log.Printf("[warn] no vendors have been set, no switches will be provisioned")
+ s := MemoryStorage{
+ Checks: make(map[string]time.Time),
+ Times: make(map[string]time.Time),
}
return &s, nil
}
-func (s *MemoryStorage) Switchq(mac string) (bool, error) {
- if len(mac) < 8 {
- return false, nil
- }
- rec, ok := s.Vendors[strings.ToUpper(mac[0:8])]
- if !ok || !rec.Provision {
- return false, nil
- }
-
- return true, nil
-}
-
func (s *MemoryStorage) LastMACCheck(mac string) (*time.Time, error) {
when, ok := s.Checks[mac]
if !ok {
@@ -111,3 +68,8 @@
s.Times[mac] = *when
return nil
}
+
+func (s *MemoryStorage) ClearProvisioned(mac string) error {
+ delete(s.Times, mac)
+ return nil
+}