-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_host.go
61 lines (51 loc) · 1.64 KB
/
group_host.go
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
package config
import (
"errors"
"github.com/squarefactory/cloud-burster/logger"
"github.com/squarefactory/cloud-burster/utils/cidr"
"github.com/squarefactory/cloud-burster/utils/generators"
"github.com/squarefactory/cloud-burster/validate"
"go.uber.org/zap"
)
type GroupHost struct {
// NamePattern overrides the host name
NamePattern string `yaml:"namePattern" validate:"required"`
// IPCidr overrides the IP. Based on NamePattern, each host will have an IP allocated.
IPCidr string `yaml:"ipCIDR" validate:"required,cidr"`
// IPOffset offsets the selection of IP.
IPOffset int `yaml:"ipOffset" validate:"omitempty"`
// HostTemplate defines helps to define a Host
HostTemplate Host `yaml:"template"`
}
func (g *GroupHost) GenerateHosts() ([]Host, error) {
var out []Host
// Generates names based on Name Pattern
names := generators.ExpandBrackets(g.NamePattern)
// Generates IPs
ipAddresses := cidr.Hosts(g.IPCidr)
if len(names) > len(ipAddresses) {
logger.I.Error(
"not enough IP addresses in CIDR",
zap.String("namePattern", g.NamePattern),
zap.Int("len(namePattern)", len(names)),
zap.String("ipCIDR", g.IPCidr),
zap.Int("len(ipAddresses)", len(ipAddresses)),
)
return []Host{}, errors.New("not enough IP addresses in CIDR")
}
// Map the names into host
for idx, name := range names {
host := Host{
Name: name,
DiskSize: g.HostTemplate.DiskSize,
FlavorName: g.HostTemplate.FlavorName,
ImageName: g.HostTemplate.ImageName,
IP: ipAddresses[idx+g.IPOffset],
}
out = append(out, host)
}
return out, nil
}
func (g *GroupHost) Validate() error {
return validate.I.Struct(g)
}