/
report_export.go
311 lines (273 loc) · 11.3 KB
/
report_export.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package grpcserver
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"strings"
"time"
pRequest "github.com/chef/automate/api/interservice/cfgmgmt/request"
"github.com/chef/automate/api/interservice/cfgmgmt/response"
"github.com/chef/automate/api/interservice/cfgmgmt/service"
"github.com/chef/automate/components/config-mgmt-service/backend"
"github.com/chef/automate/components/config-mgmt-service/params"
"github.com/chef/automate/lib/io/chunks"
"github.com/chef/automate/lib/stringutils"
"github.com/gocarina/gocsv"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type exportReportHandler func([]backend.Run) error
// The fields that will be displayed in a CSV and JSON download
type displayReport struct {
RunID string `csv:"Run ID" json:"run_id"`
StartTime time.Time `csv:"Start Time" json:"start_time"`
EndTime time.Time `csv:"End Time" json:"end_time"`
UpdatedResourceCount int `csv:"Updated Resource Count" json:"updated_resource_count"`
Resources []backend.Resource `csv:"-" json:"resources"`
NodeName string `csv:"Node Name" json:"name"`
ID string `csv:"Node ID" json:"node_id"`
Platform string `csv:"Platform" json:"platform"`
Organization string `csv:"Organization" json:"organization"`
Environment string `csv:"Environment" json:"environment"`
Fqdn string `csv:"Fqdn" json:"fqdn"`
ClientVersion string `csv:"Client Version" json:"client_version"`
RunList []string `csv:"-" json:"run_list"`
Source string `csv:"Source" json:"source"`
Status string `csv:"Status" json:"status"`
TotalResourceCount int `csv:"Total Resource Count" json:"total_resource_count"`
Tags []string `csv:"-" json:"tags"`
ResourceNames []string `csv:"-" json:"resource_names"`
Recipes []string `csv:"-" json:"recipes"`
Cookbooks []string `csv:"-" json:"cookbooks"`
UptimeSeconds int64 `csv:"Uptime Seconds" json:"uptime_seconds"`
Roles []string `csv:"-" json:"roles"`
PolicyName string `csv:"Policy Name" json:"policy_name"`
PolicyGroup string `csv:"Policy Group" json:"policy_group"`
PolicyRevision string `csv:"Policy Revision" json:"policy_revision"`
SourceFqdn string `csv:"Source FQDN" json:"source_fqdn"`
IpAddress string `csv:"IP Address" json:"ip_address"`
Deprecations []backend.Deprecation `csv:"-" json:"deprecations"`
ExpandedRunList backend.ExpandedRunList `csv:"-" json:"expanded_run_list"`
Hostname string `csv:"Hostname" json:"hostname"`
IP6address string `csv:"IP6 Address" json:"ip6_address"`
Macaddress string `csv:"MAC Address" json:"mac_address"`
MemoryTotal string `csv:"Memory Total" json:"memory_total"`
VirtualizationRole string `csv:"Virtualization Role" json:"virtualization_role"`
VirtualizationSystem string `csv:"Virtualization System" json:"virtualization_system"`
KernelRelease string `csv:"Kernel Release" json:"kernel_release"`
KernelVersion string `csv:"Kernel Version" json:"kernel_version"`
Domain string `csv:"Domain" json:"domain"`
Timezone string `csv:"Timezone" json:"timezone"`
DmiSystemManufacturer string `csv:"DMI System Manufacturer" json:"dmi_system_manufacturer"`
DmiSystemSerialNumber string `csv:"DMI System Serial Number" json:"dmi_system_serial_number"`
}
// ReportExport streams a json or csv export
func (s *CfgMgmtServer) ReportExport(request *pRequest.ReportExport, stream service.CfgMgmtService_ReportExportServer) error {
exporter, err := getReportExportHandler(request.OutputType, stream)
if err != nil {
return err
}
streamCtx := stream.Context()
deadline, ok := streamCtx.Deadline()
if !ok {
deadline = time.Now().Add(5 * time.Minute)
}
ctx, cancel := context.WithDeadline(streamCtx, deadline)
defer cancel()
return s.exportReports(ctx, request, exporter)
}
func getReportExportHandler(outputType string, stream service.CfgMgmtService_ReportExportServer) (exportReportHandler, error) {
switch outputType {
case "", "json":
return jsonReportExport(stream), nil
case "csv":
return csvReportExport(stream), nil
default:
return nil, status.Error(codes.Unauthenticated, fmt.Sprintf(outputType+" export is not supported"))
}
}
func (s *CfgMgmtServer) exportReports(ctx context.Context, request *pRequest.ReportExport,
sendResult exportReportHandler) error {
// Date Range
start, err := ToTime(request.Start)
if err != nil {
return status.Errorf(codes.InvalidArgument, err.Error())
}
end, err := ToTime(request.End)
if err != nil {
return status.Errorf(codes.InvalidArgument, err.Error())
}
projectFilters, err := filterByProjects(ctx, map[string][]string{})
if err != nil {
return err
}
// Check if the projects match the associated node async
nodeExistsChan := s.nodeExistsAsync(request.NodeId, projectFilters)
pageSize := 100
var cursorEndTime time.Time
cursorID := ""
sortAsc := false
runFilters, err := stringutils.FormatFiltersWithKeyConverter(request.Filter,
params.ConvertParamToNodeRunBackend)
if err != nil {
return status.Errorf(codes.InvalidArgument, err.Error())
}
runs, err := s.client.GetRunsPageByCursor(ctx, request.NodeId, start, end,
runFilters, cursorEndTime, cursorID, pageSize, sortAsc)
if err != nil {
return status.Errorf(codes.Internal, err.Error())
}
// Wait for check if the associated node's projects match
nodeExists := <-nodeExistsChan
if nodeExists.err != nil {
return nodeExists.err
}
// Either the user does not have permissions or the node does not exist
if !nodeExists.exists {
return nil
}
for len(runs) > 0 {
err = sendResult(runs)
if err != nil {
return status.Errorf(codes.Internal, "Failed to stream nodes Error: %s", err.Error())
}
lastRun := runs[len(runs)-1]
cursorID = lastRun.RunID
cursorEndTime = lastRun.EndTime
runs, err = s.client.GetRunsPageByCursor(ctx, request.NodeId, start, end,
runFilters, cursorEndTime, cursorID, pageSize, sortAsc)
if err != nil {
return status.Errorf(codes.Internal, err.Error())
}
}
return nil
}
func jsonReportExport(stream service.CfgMgmtService_ReportExportServer) exportReportHandler {
return func(runs []backend.Run) error {
displayNodeCollection := reportCollectionToDisplayReportCollection(runs)
raw, err := json.Marshal(displayNodeCollection)
if err != nil {
return fmt.Errorf("Failed to marshal JSON export data: %+v", err)
}
reader := bytes.NewReader(raw)
buf := make([]byte, streamBufferSize)
writer := chunks.NewWriter(streamBufferSize, func(p []byte) error {
return stream.Send(&response.ReportExportData{Content: p})
})
_, err = io.CopyBuffer(writer, reader, buf)
if err != nil {
return fmt.Errorf("Failed to export JSON: %+v", err)
}
return nil
}
}
func csvReportExport(stream service.CfgMgmtService_ReportExportServer) exportReportHandler {
initialRun := true
return func(runs []backend.Run) error {
res, err := runsToCSV(runs)
if err != nil {
return err
}
if initialRun {
initialRun = false
} else {
splits := strings.SplitAfterN(res, "\n", 2)
if len(splits) == 2 {
res = splits[1]
}
}
reader := bytes.NewReader([]byte(res))
buf := make([]byte, streamBufferSize)
writer := chunks.NewWriter(streamBufferSize, func(p []byte) error {
return stream.Send(&response.ReportExportData{Content: p})
})
_, err = io.CopyBuffer(writer, reader, buf)
if err != nil {
return fmt.Errorf("Failed to export CSV: %+v", err)
}
return nil
}
}
// runsToCSV converts a run to its CSV representation as a string
// which includes a header line.
func runsToCSV(runs []backend.Run) (string, error) {
displayReportCollection := reportCollectionToDisplayReportCollection(runs)
// export everything
content, err := gocsv.MarshalString(&displayReportCollection)
if err != nil {
return "", fmt.Errorf("Failed to marshal CSV report: %+v", err)
}
return content, nil
}
func reportCollectionToDisplayReportCollection(runs []backend.Run) []displayReport {
var displayReportCollection = make([]displayReport, len(runs))
for index, run := range runs {
displayReportCollection[index] = runToDisplayReport(run)
}
return displayReportCollection
}
func runToDisplayReport(run backend.Run) displayReport {
deprecations := make([]backend.Deprecation, len(run.Deprecations))
for index, dep := range run.Deprecations {
deprecations[index] = backend.Deprecation(dep)
}
resources := make([]backend.Resource, len(run.Resources))
for index, resource := range run.Resources {
resources[index] = backend.Resource(resource)
}
var ip, ip6 string
ipAddress := net.ParseIP(toString(run.Ipaddress))
if ipAddress != nil {
ip = ipAddress.String()
}
ip6Address := net.ParseIP(toString(run.NodeInfo.Ip6address))
if ip6Address != nil {
ip6 = ipAddress.String()
}
return displayReport{
NodeName: run.NodeName,
ID: run.EntityUuid,
Platform: run.Platform,
Environment: run.Environment,
Organization: run.OrganizationName,
Fqdn: run.Fqdn,
IpAddress: ip,
ClientVersion: run.ChefVersion,
RunList: run.RunList,
Source: run.Source,
Status: run.Status,
TotalResourceCount: run.TotalResourceCount,
Tags: run.ChefTags,
ResourceNames: run.ResourceNames,
Recipes: run.Recipes,
Cookbooks: run.Cookbooks,
UptimeSeconds: run.UptimeSeconds,
Roles: run.Roles,
PolicyName: run.PolicyName,
PolicyGroup: run.PolicyGroup,
PolicyRevision: run.PolicyRevision,
SourceFqdn: run.SourceFqdn,
ExpandedRunList: backend.ExpandedRunList(run.ExpandedRunList),
Deprecations: deprecations,
RunID: run.RunID,
StartTime: run.StartTime,
EndTime: run.EndTime,
UpdatedResourceCount: run.UpdatedResourceCount,
Resources: resources,
Hostname: run.NodeInfo.Hostname,
IP6address: ip6,
Macaddress: run.NodeInfo.Macaddress,
MemoryTotal: run.NodeInfo.MemoryTotal,
VirtualizationRole: run.NodeInfo.VirtualizationRole,
VirtualizationSystem: run.NodeInfo.VirtualizationSystem,
KernelRelease: run.NodeInfo.KernelRelease,
KernelVersion: run.NodeInfo.KernelVersion,
Domain: run.NodeInfo.Domain,
Timezone: run.NodeInfo.Timezone,
DmiSystemManufacturer: run.NodeInfo.DmiSystemManufacturer,
DmiSystemSerialNumber: run.NodeInfo.DmiSystemSerialNumber,
}
}