-
Notifications
You must be signed in to change notification settings - Fork 0
/
filer.go
99 lines (92 loc) · 2.66 KB
/
filer.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
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
88
89
90
91
92
93
94
95
96
97
98
99
package netbox
import (
"fmt"
"strings"
"github.com/netbox-community/go-netbox/v3/netbox/client/dcim"
"github.com/netbox-community/go-netbox/v3/netbox/models"
)
type Filer struct {
Name string `json:"name" yaml:"name"`
Host string `json:"host" yaml:"host"`
AvailabilityZone string `json:"availability_zone" yaml:"availability_zone"`
IP string `json:"ip,omitempty" yaml:"ip,omitempty"`
}
type Filers []Filer
func (nb Client) GetFilers(region, query string) (filers Filers, err error) {
switch query {
case "md", "manila":
filers, err = getFilersByTag(nb, region, "manila")
case "bb", "cinder":
filers, err = getFilersByTag(nb, region, "cinder")
case "bm", "baremetal":
filers, err = getFilersByTag(nb, region, "baremetal")
case "apod", "cp", "control-plane", "control_plane":
filers, err = getFilersByTag(nb, region, "apod")
default:
err = fmt.Errorf("%s is not valide filer type", query)
}
if err != nil {
return nil, err
}
return filers, nil
}
func getFilersByTag(nb Client, region, tag string) (Filers, error) {
var (
roleFiler = "filer"
manufacturer = "netapp"
statusActive = "active"
interfaces = "False"
)
devices, err := nb.FetchDevices(dcim.DcimDevicesListParams{
Role: &roleFiler,
Status: &statusActive,
Manufacturer: &manufacturer,
Interfaces: &interfaces,
Tag: &tag,
Region: ®ion,
})
if err != nil {
return nil, err
}
return makeFilers(nb, region, devices), nil
}
func makeFilers(nb Client, region string, devices []*models.DeviceWithConfigContext) Filers {
// IP address is not maintained in netbox for the filer cluster, therefore
// filer name is used to determin the host name.
//
// TODO: Use the ip address of the first node as the host ip. To do that,
// one should read the IP of the installed device on the first node
// bay.
filers := make(Filers, 0)
for _, d := range devices {
// Ignore filer cluster with no nodes
if deviceBays, err := nb.GetDeviceBaysByDeviceID(d.ID); err == nil {
hasChildDevice := false
ip := ""
for _, node := range deviceBays {
if node.InstalledDevice != nil {
hasChildDevice = true
if ip == "" {
d, err := nb.GetDeviceByID(node.InstalledDevice.ID)
if err != nil {
continue
}
if d.PrimaryIp4 != nil {
s := strings.Split(*d.PrimaryIp4.Address, "/")
ip = s[0]
}
}
}
}
if hasChildDevice {
filers = append(filers, Filer{
Name: *d.Name,
Host: *d.Name + ".cc." + region + ".cloud.sap",
AvailabilityZone: strings.ToLower(*d.Site.Name),
IP: ip,
})
}
}
}
return filers
}