-
Notifications
You must be signed in to change notification settings - Fork 4
/
AwsSnapshotStore.ts
178 lines (159 loc) · 3.96 KB
/
AwsSnapshotStore.ts
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
/**
* @module @ddes/aws-store
*/
import {
AggregateKey,
AggregateSnapshot,
AggregateType,
Timestamp,
utils as coreutils,
} from '@ddes/core'
import {SnapshotStore} from '@ddes/core'
import {S3} from 'aws-sdk'
import {ConfigurationOptions} from 'aws-sdk/lib/config'
import {createBucket, deleteBucket} from './utils'
/**
* Interface for SnapshotStore powered by AWS S3
*/
export default class AwsSnapshotStore extends SnapshotStore {
public manageBucket: boolean = false
public bucketName: string
public keyPrefix: string = ''
public s3ClientConfiguration?: S3.ClientConfiguration
public awsConfig?: ConfigurationOptions
public s3: S3
constructor(config: {
bucketName: string
manageBucket?: boolean
keyPrefix?: string
s3ClientConfiguration?: S3.ClientConfiguration
}) {
super()
this.bucketName = config.bucketName
this.s3ClientConfiguration = config.s3ClientConfiguration
this.s3 = new S3(this.s3ClientConfiguration)
if (config.keyPrefix) {
this.keyPrefix = config.keyPrefix
}
if (typeof config.manageBucket !== 'undefined') {
this.manageBucket = config.manageBucket
}
}
/**
* Create and configure DynamoDB table, S3 bucket and DynamoDB auto-scaling
*/
public async setup() {
if (this.manageBucket) {
await createBucket(this.bucketName, {
s3ClientConfiguration: this.s3ClientConfiguration,
})
}
}
/**
* Remove DynamoDB auto-scaling, S3 Bucket and DynamoDB table
*/
public async teardown() {
if (this.manageBucket) {
await deleteBucket(this.bucketName, {
s3ClientConfiguration: this.s3ClientConfiguration,
})
}
}
/**
* Read an aggregate instance snapshot from AWS S3
*
* @param type e.g. 'Account'
* @param key e.g. '1234'
*/
public async readSnapshot(
type: AggregateType,
key: AggregateKey
): Promise<AggregateSnapshot | null> {
try {
const {Body: snapshotJSON} = await this.s3
.getObject({
Bucket: this.bucketName,
Key: `${this.keyPrefix}${type}_${key}`,
})
.promise()
if (!snapshotJSON) {
return null
}
const {
version,
state,
timestamp: timestampString,
compatibilityChecksum,
} = JSON.parse(snapshotJSON as string)
return {
version,
state,
timestamp: coreutils.toTimestamp(timestampString),
compatibilityChecksum,
}
} catch (error) {
if (error.code !== 'NoSuchKey') {
throw error
}
return null
}
}
/**
* Delete snapshots from AWS S3 bucket
*/
public async deleteSnapshots() {
let listResult
do {
listResult = await this.s3
.listObjectsV2({
Bucket: this.bucketName,
Prefix: this.keyPrefix,
ContinuationToken: listResult
? listResult.NextContinuationToken
: undefined,
})
.promise()
if (!listResult.Contents) {
throw new Error('List request returned on content')
}
for (const s3Object of listResult.Contents) {
await this.s3
.deleteObject({
Bucket: this.bucketName,
Key: s3Object.Key!,
})
.promise()
}
} while (listResult.NextContinuationToken)
}
/**
* Write an aggregate instance snapshot to AWS S3 bucket
*
* @param type e.g. 'Account'
* @param key e.g. '1234'
*/
public async writeSnapshot(
type: string,
key: string,
payload: {
version: number
state: object
timestamp: Timestamp
compatibilityChecksum: string
}
) {
const {version, state, timestamp, compatibilityChecksum} = payload
await this.s3
.putObject({
Bucket: this.bucketName,
Key: `${this.keyPrefix}${type}_${key}`,
Body: JSON.stringify({
version,
state,
compatibilityChecksum,
timestamp,
}),
})
.promise()
}
}