-
Notifications
You must be signed in to change notification settings - Fork 90
/
manifest.go
88 lines (75 loc) · 2.36 KB
/
manifest.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
package aws
import (
"path/filepath"
"time"
"github.com/sirupsen/logrus"
)
// Manifest is a representation of the file AWS provides with metadata for current usage information.
type Manifest struct {
AssemblyID string `json:"assemblyId"`
Account string `json:"account"`
Columns []Column `json:"columns"`
Charset string `json:"charset"`
Compression string `json:"compression"`
ContentType string `json:"contentType"`
ReportID string `json:"reportId"`
ReportName string `json:"reportName"`
BillingPeriod BillingPeriod `json:"billingPeriod"`
Bucket string `json:"bucket"`
ReportKeys []string `json:"reportKeys"`
AdditionalArtifactKeys []AdditionalArtifact `json:"additionalArtifactKeys"`
}
type BillingPeriod struct {
Start Time `json:"start"`
End Time `json:"end"`
}
// Column is a description of a field from a AWS usage report manifest file.
type Column struct {
Category string `json:"category"`
Name string `json:"name"`
}
type AdditionalArtifact struct {
ArtifactType string `json:"artifactType"`
Name string `json:"name"`
}
// Paths returns the directories containing usage data. The result will be free of duplicates.
func (m Manifest) DataDirectory() string {
paths := m.paths()
pathsLen := len(paths)
var path string
if pathsLen > 0 {
path = paths[0]
}
if pathsLen != 1 {
logrus.Errorf("aws manifest %s does not have exactly 1 data directory containing report data, reportKeys: %v", m.AssemblyID, m.ReportKeys)
}
return path
}
func (m Manifest) paths() []string {
seen := make(map[string]struct{})
var paths []string
for _, key := range m.ReportKeys {
dirPath := filepath.Dir(key)
if _, exists := seen[dirPath]; exists {
continue
}
seen[dirPath] = struct{}{}
paths = append(paths, dirPath)
}
return paths
}
type Time struct {
time.Time
}
const manifestTime = "20060102T000000.000Z"
func (t *Time) UnmarshalJSON(b []byte) error {
// b contains quotes around the timestamp
tt, err := time.Parse(manifestTime, string(b[1:len(b)-1]))
if err == nil {
*t = Time{tt}
}
return err
}
func (t *Time) String() string {
return t.Format(manifestTime)
}