forked from metalsoft-io/metalcloud-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_reports.go
196 lines (157 loc) · 4.48 KB
/
cmd_reports.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"flag"
"fmt"
metalcloud "github.com/metalsoft-io/metal-cloud-sdk-go/v2"
"github.com/metalsoft-io/tableformatter"
)
var reportsCmds = []Command{
{
Description: "Statistics and other reports.",
Subject: "report",
AltSubject: "report",
Predicate: "devices",
AltPredicate: "devices",
FlagSet: flag.NewFlagSet("list active devices in all datacenters", flag.ExitOnError),
InitFunc: func(c *Command) {
c.Arguments = map[string]interface{}{
"format": c.FlagSet.String("format", _nilDefaultStr, "The output format. Supported values are 'json','csv','yaml'. The default format is human readable."),
}
},
ExecuteFunc: devicesListCmd,
Endpoint: DeveloperEndpoint,
},
}
func getActiveServers(datacenter string, client metalcloud.MetalCloudClient) (*[]metalcloud.ServerSearchResult, error) {
servers, err := client.ServersSearch("datacenter_name:" + datacenter)
if err != nil {
return nil, err
}
filteredServers := []metalcloud.ServerSearchResult{}
for _, s := range *servers {
switch s.ServerStatus {
case "available", "used", "cleaning", "cleaning_required", "available_reserved":
filteredServers = append(filteredServers, s)
}
}
return &filteredServers, nil
}
func getAllActiveSwitches(datacenter string, client metalcloud.MetalCloudClient) (*[]metalcloud.SwitchDevice, error) {
switches := []metalcloud.SwitchDevice{}
switchList, err := client.SwitchDevices(datacenter, "")
if err != nil {
return nil, err
}
for _, s := range *switchList {
switches = append(switches, s)
}
return &switches, nil
}
func getAllActiveStoragePools(datacenter string, client metalcloud.MetalCloudClient) (*[]metalcloud.StoragePoolSearchResult, error) {
return client.StoragePoolSearch("datacenter_name:" + datacenter)
}
type devicesList struct {
servers *[]metalcloud.ServerSearchResult
switches *[]metalcloud.SwitchDevice
storages *[]metalcloud.StoragePoolSearchResult
}
func getAllActiveDevices(datacenter string, client metalcloud.MetalCloudClient) (*devicesList, error) {
servers, err := getActiveServers(datacenter, client)
if err != nil {
return nil, err
}
switches, err := getAllActiveSwitches(datacenter, client)
if err != nil {
return nil, err
}
storages, err := getAllActiveStoragePools(datacenter, client)
if err != nil {
return nil, err
}
return &devicesList{
servers: servers,
switches: switches,
storages: storages,
}, nil
}
func devicesListCmd(c *Command, client metalcloud.MetalCloudClient) (string, error) {
DCs, err := client.Datacenters(true)
if err != nil {
return "", err
}
stats := map[string]*devicesList{}
for _, dc := range *DCs {
if dc.DatacenterIsMaster {
continue
}
deviceList, err := getAllActiveDevices(dc.DatacenterName, client)
if err != nil {
return "", err
}
stats[dc.DatacenterName] = deviceList
}
schema := []tableformatter.SchemaField{
{
FieldName: "DC_IDX",
FieldType: tableformatter.TypeString,
FieldSize: 3,
},
{
FieldName: "DATACENTER",
FieldType: tableformatter.TypeString,
FieldSize: 6,
},
{
FieldName: "SERVERS",
FieldType: tableformatter.TypeString,
FieldSize: 6,
},
{
FieldName: "SWITCHES",
FieldType: tableformatter.TypeString,
FieldSize: 6,
},
{
FieldName: "STORAGES",
FieldType: tableformatter.TypeString,
FieldSize: 6,
},
}
data := [][]interface{}{}
totalServers := 0
totalSwitches := 0
totalStorages := 0
dc_idx := 0
for datacenterName, dcStats := range stats {
serverCount := len(*dcStats.servers)
switchesCount := len(*dcStats.switches)
storagePoolsCount := len(*dcStats.storages)
row := []interface{}{
fmt.Sprintf("%d", dc_idx),
datacenterName,
fmt.Sprintf("%d", serverCount),
fmt.Sprintf("%d", switchesCount),
fmt.Sprintf("%d", storagePoolsCount),
}
data = append(data, row)
dc_idx++
totalServers += serverCount
totalSwitches += switchesCount
totalStorages += storagePoolsCount
}
totalsRow := []interface{}{
"",
bold("TOTAL"),
bold(fmt.Sprintf("%d", totalServers)),
bold(fmt.Sprintf("%d", totalSwitches)),
bold(fmt.Sprintf("%d", totalStorages)),
}
data = append(data, totalsRow)
table := tableformatter.Table{
Data: data,
Schema: schema,
}
totalDevices := totalServers + totalSwitches + totalStorages
title := fmt.Sprint("Count of active or in-use equipment per datacenter")
return table.RenderTable(fmt.Sprintf("Records (%d active devices across all datacenters)", totalDevices), title, getStringParam(c.Arguments["format"]))
}