/
servers.go
92 lines (73 loc) · 2.18 KB
/
servers.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
package main
import (
"os"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
)
// RefreshServerMetadata refreshes the map of MAC addresses to server metadata.
func (app *Application) RefreshServerMetadata(acquireStateLock bool) error {
if acquireStateLock {
app.stateLock.Lock()
defer app.stateLock.Unlock()
}
serversByMACAddress := make(map[string]compute.Server)
page := compute.DefaultPaging()
page.PageSize = 50
for {
servers, err := app.Client.ListServersInNetworkDomain(app.NetworkDomain.ID, page)
if err != nil {
return err
}
if servers.IsEmpty() {
break
}
for _, server := range servers.Items {
// Ignore servers that are being deployed or destroyed.
if server.Network.PrimaryAdapter.PrivateIPv4Address == nil {
continue
}
primaryMACAddress := *server.Network.PrimaryAdapter.MACAddress
serversByMACAddress[primaryMACAddress] = server
for _, additionalNetworkAdapter := range server.Network.AdditionalNetworkAdapters {
// Ignore network adapters that are being deployed or destroyed.
if additionalNetworkAdapter.PrivateIPv4Address == nil {
continue
}
additionalMACAddress := *additionalNetworkAdapter.MACAddress
serversByMACAddress[additionalMACAddress] = server
}
}
page.Next()
}
app.ServersByMACAddress = serversByMACAddress
return nil
}
// FindServerByMACAddress finds the server (if any) posessing a network adapter with the specified MAC address.
func (app *Application) FindServerByMACAddress(macAddress string) *compute.Server {
app.stateLock.Lock()
defer app.stateLock.Unlock()
server, ok := app.ServersByMACAddress[macAddress]
if ok {
return &server
}
return nil
}
// Create a test server for calls from localhost.
func createTestServer() *compute.Server {
localhost4 := os.Getenv("MCP_TEST_HOST_IPV4")
if localhost4 == "" {
localhost4 = "127.0.0.1"
}
localhost6 := os.Getenv("MCP_TEST_HOST_IPV6")
if localhost6 == "" {
localhost6 = "::1"
}
return &compute.Server{
Name: os.Getenv("HOST"),
Network: compute.VirtualMachineNetwork{
PrimaryAdapter: compute.VirtualMachineNetworkAdapter{
PrivateIPv4Address: &localhost4,
PrivateIPv6Address: &localhost6,
},
},
}
}