-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcron_store.go
129 lines (112 loc) · 3.05 KB
/
cron_store.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
/*
Copyright 2020 Adevinta
*/
package crontinuous
import (
"bytes"
"encoding/json"
"errors"
"io"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
)
var (
errEntriesFileNotFound = errors.New("EntriesFileNotFound")
)
type ScanCronStore interface {
GetScanEntries() (map[string]ScanEntry, error)
SaveScanEntries(entries map[string]ScanEntry) error
}
type ReportCronStore interface {
GetReportEntries() (map[string]ReportEntry, error)
SaveReportEntries(entries map[string]ReportEntry) error
}
type S3CronStore struct {
bucket string
scanCronKey string
reportCronKey string
s3Client s3iface.S3API
}
func NewS3CronStore(bucket, scanCronKey, reportCronKey string, s3Client s3iface.S3API) *S3CronStore {
return &S3CronStore{
bucket: bucket,
scanCronKey: scanCronKey,
reportCronKey: reportCronKey,
s3Client: s3Client,
}
}
func (s *S3CronStore) GetScanEntries() (map[string]ScanEntry, error) {
entriesData, err := s.getEntriesData(s.scanCronKey)
if err != nil {
// If entries file is not found
// return void entries map.
//
// This allows to auto create the entries file
// automatically in remote store when a new entry
// is added via API.
if err == errEntriesFileNotFound {
return map[string]ScanEntry{}, nil
}
return nil, err
}
var scanEntries map[string]ScanEntry
err = json.Unmarshal(entriesData, &scanEntries)
return scanEntries, err
}
func (s *S3CronStore) SaveScanEntries(entries map[string]ScanEntry) error {
return s.saveEntries(s.scanCronKey, entries)
}
func (s *S3CronStore) GetReportEntries() (map[string]ReportEntry, error) {
entriesData, err := s.getEntriesData(s.reportCronKey)
if err != nil {
// If entries file is not found
// return void entries map.
//
// This allows to auto create the entries file
// automatically in remote store when a new entry
// is added via API.
if err == errEntriesFileNotFound {
return map[string]ReportEntry{}, nil
}
return nil, err
}
var reportEntries map[string]ReportEntry
err = json.Unmarshal(entriesData, &reportEntries)
return reportEntries, err
}
func (s *S3CronStore) SaveReportEntries(entries map[string]ReportEntry) error {
return s.saveEntries(s.reportCronKey, entries)
}
func (s *S3CronStore) getEntriesData(key string) ([]byte, error) {
output, err := s.s3Client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(key),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeNoSuchKey:
return nil, errEntriesFileNotFound
default:
return nil, err
}
}
return nil, err
}
return io.ReadAll(output.Body)
}
func (s *S3CronStore) saveEntries(key string, entries interface{}) error {
content, err := json.Marshal(entries)
if err != nil {
return err
}
params := &s3.PutObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(key),
Body: bytes.NewReader(content),
}
_, err = s.s3Client.PutObject(params)
return err
}