| package main |
| |
| import ( |
| "encoding/json" |
| "net/http" |
| "strings" |
| ) |
| |
| type Vendors interface { |
| Switchq(mac string) (bool, error) |
| } |
| |
| type VendorRec struct { |
| Prefix string `json:"prefix"` |
| Vendor string `json:"vendor"` |
| Provision bool `json:"provision"` |
| } |
| |
| type VendorsData struct { |
| Vendors map[string]VendorRec |
| } |
| |
| func NewVendors(spec string) (Vendors, error) { |
| v := VendorsData{} |
| v.Vendors = make(map[string]VendorRec) |
| |
| t := &http.Transport{} |
| t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/"))) |
| c := &http.Client{Transport: t} |
| res, err := c.Get(spec) |
| if err != nil { |
| return nil, err |
| } |
| defer res.Body.Close() |
| |
| data := make([]VendorRec, 0) |
| decoder := json.NewDecoder(res.Body) |
| err = decoder.Decode(&data) |
| if err != nil { |
| return nil, err |
| } |
| for _, rec := range data { |
| v.Vendors[rec.Prefix] = rec |
| } |
| log.Debugf("known vendors %+v", v.Vendors) |
| |
| return &v, nil |
| } |
| |
| func (v *VendorsData) Switchq(mac string) (bool, error) { |
| if len(mac) < 8 { |
| return false, nil |
| } |
| rec, ok := v.Vendors[strings.ToUpper(mac[0:8])] |
| if !ok || !rec.Provision { |
| return false, nil |
| } |
| |
| return true, nil |
| } |