diff --git a/create.go b/create.go index 3b6de72..f97a575 100644 --- a/create.go +++ b/create.go @@ -38,19 +38,16 @@ func create(w http.ResponseWriter, r *http.Request) error { fmt.Fprintln(w, result) return errors.New(result) } + if d.Uuid == "" { u := uuid.New() d.Uuid = u.String() } - return nil + if len(d.Networks) == 0 { - mac, err := getNewMac() - if err != nil { - return err - } var newNet *pb.Network newNet = new(pb.Network) - newNet.Mac = mac + newNet.Mac = getNewMac() d.Networks = append(d.Networks, newNet) // d.AddDefaultNetwork(mac) } diff --git a/validate.go b/validate.go index a4caa52..4105c6f 100644 --- a/validate.go +++ b/validate.go @@ -15,6 +15,7 @@ package main import ( "errors" + "fmt" "path/filepath" "github.com/google/uuid" @@ -133,7 +134,7 @@ func ValidateDiskFilenames(cluster *pb.Cluster) []*pb.Event { return alle } -func getNewMac() (string, error) { +func getNewMac() string { // mac address map to check for duplicates var macs map[string]string macs = make(map[string]string) @@ -145,21 +146,25 @@ func getNewMac() (string, error) { // UUID already exists log.Info("duplicate MAC", n.Mac, macs[n.Mac]) log.Info("duplicate MAC", n.Mac, d.Hostname) - return "", errors.New("duplicate MAC: " + d.Hostname + " and " + macs[n.Mac]) + return "" } macs[n.Mac] = d.Hostname } } - var mac string = "22:22:22:22:22:09" + var i int = 9 + var mac string for { + mac = fmt.Sprintf("22:22:22:22:22:%02d", i) if _, ok := macs[mac]; ok { log.Info("MAC already defined", mac, macs[mac]) + i += 1 continue } - return mac, nil + log.Info("using new MAC:", mac) + return mac } - return mac, nil + return "" } // runs on startup. dies if there are duplicates