mirror of
https://github.com/outbackdingo/matchbox.git
synced 2026-01-28 10:19:36 +00:00
* Use YAML group config to match machine labels to groups of zero or more machines sharing a Specification * Discontinue using machine.json resources representations which are repetitive, don't support groups, and don't allow label matching * Previous "default" special case now handled by a Group with an empty matcher
38 lines
858 B
Go
38 lines
858 B
Go
package api
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// labelsFromRequest returns Labels from request query parameters.
|
|
func labelsFromRequest(req *http.Request) Labels {
|
|
values := req.URL.Query()
|
|
labels := map[string]string{}
|
|
for key := range values {
|
|
switch strings.ToLower(key) {
|
|
case "mac":
|
|
// set mac if and only if it parses
|
|
if hw, err := parseMAC(values.Get(key)); err == nil {
|
|
labels[key] = hw.String()
|
|
}
|
|
default:
|
|
// matchers don't use multi-value keys, drop later values
|
|
labels[key] = values.Get(key)
|
|
}
|
|
}
|
|
return LabelSet(labels)
|
|
}
|
|
|
|
// parseMAC wraps net.ParseMAC with logging.
|
|
func parseMAC(s string) (net.HardwareAddr, error) {
|
|
macAddr, err := net.ParseMAC(s)
|
|
if err != nil {
|
|
// invalid MAC arguments may be common
|
|
log.Debugf("error parsing MAC address: %s", err)
|
|
return nil, err
|
|
}
|
|
return macAddr, err
|
|
}
|