summaryrefslogtreecommitdiff
path: root/validate.go
blob: 2faaf382e6152edc04182c7a48dbb6da991db642 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main

/*
	validate / sanity check / consistancy check the data

	here is some code to do smart things like:

	* check mac addresses are unique
	* check uuid's are unique
	* double check filenames are unique
	* return a unique mac address
	* return a unique uuid

*/

import (
	"os"

	"github.com/google/uuid"

	"go.wit.com/log"
)

// will make sure the mac address is unique
func checkUniqueMac(mac string) bool {
	for _, d := range me.cluster.Droplets {
		for _, n := range d.Networks {
			if n.Mac == mac {
				log.Info("duplicate MAC", n.Mac, "in droplet", d.Hostname)
				return false
			}
		}
	}
	return true
}

func checkDroplets(dump bool) bool {
	// uuid map to check for duplicates
	var umap map[string]string
	umap = make(map[string]string)

	// mac address map to check for duplicates
	var macs map[string]string
	macs = make(map[string]string)

	for _, d := range me.cluster.Droplets {
		// Generate a new UUID
		if d.Uuid == "" {
			u := uuid.New()
			d.Uuid = u.String()
		}

		// seconds, ok := timeZone[tz]; ok {
		if _, ok := umap[d.Uuid]; ok {
			// UUID already exists
			log.Info("duplicate UUID", d.Uuid, umap[d.Uuid])
			log.Info("duplicate UUID", d.Uuid, d.Hostname)
			os.Exit(-1)
		}
		umap[d.Uuid] = d.Hostname

		for _, n := range d.Networks {
			// log.Println("network:", n.Mac, d.Uuid, d.Hostname)
			if _, ok := macs[n.Mac]; ok {
				// UUID already exists
				log.Info("duplicate MAC", n.Mac, macs[n.Mac], umap[macs[n.Mac]])
				log.Info("duplicate MAC", n.Mac, d.Hostname)
				os.Exit(-1)
			}
			macs[n.Mac] = d.Uuid
		}
	}
	log.Println("validated okay: no duplicate MAC addr")
	log.Println("validated okay: no duplicate UUID")

	if dump {
		for u, hostname := range umap {
			log.Println("uuid:", u, "hostname:", hostname)
		}

		for mac, uuid := range macs {
			log.Println("mac:", mac, "uuid", uuid, "hostname:", umap[uuid])
		}
	}

	return false
}