-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathLifecycleUtils.ts
More file actions
310 lines (290 loc) · 12 KB
/
LifecycleUtils.ts
File metadata and controls
310 lines (290 loc) · 12 KB
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
import assert from 'assert';
import LifecycleDateTime from './LifecycleDateTime';
import { supportedLifecycleRules } from '../../constants';
export default class LifecycleUtils {
_supportedRules: string[];
_datetime: LifecycleDateTime;
constructor(supportedRules?: string[], datetime?: LifecycleDateTime) {
if (supportedRules) {
assert(Array.isArray(supportedRules));
}
if (datetime) {
assert(datetime instanceof LifecycleDateTime);
}
this._supportedRules = supportedRules || supportedLifecycleRules;
this._datetime = datetime || new LifecycleDateTime();
}
/**
* Compare two transition rules and return the one that is most recent.
* @param params - The function parameters
* @param params.transition1 - A transition from the current rule
* @param params.transition2 - A transition from the previous rule
* @param params.lastModified - The object's last modified
* date
* @return The most applicable transition rule
*/
compareTransitions(params: {
lastModified: string;
transition1: any;
transition2?: any;
}): number | undefined;
compareTransitions(params: {
lastModified: string;
transition1?: any;
transition2: any;
}): number | undefined;
compareTransitions(params: {
lastModified: string;
transition1: any;
transition2: any;
}): number | undefined;
compareTransitions(params: {
lastModified: string;
transition1?: any;
transition2?: any;
}) {
const { transition1, transition2, lastModified } = params;
if (transition1 === undefined) {
return transition2;
}
if (transition2 === undefined) {
return transition1;
}
const trans1 = this._datetime.getTransitionTimestamp(transition1!, lastModified)!;
const trans2 = this._datetime.getTransitionTimestamp(transition2!, lastModified)!;
return trans1 > trans2 ? transition1 : transition2;
}
// TODO Fix This
/**
* Find the most relevant trantition rule for the given transitions array
* and any previously stored transition from another rule.
* @param params - The function parameters
* @param params.transitions - Array of lifecycle rule transitions
* @param params.lastModified - The object's last modified
* date
* @return The most applicable transition rule
*/
getApplicableTransition(params: {
store: any;
currentDate: Date;
transitions: any[];
lastModified: string;
}) {
const { transitions, store, lastModified, currentDate } = params;
const transition = transitions.reduce((result, transition) => {
const isApplicable = // Is the transition time in the past?
this._datetime.getTimestamp(currentDate) >=
this._datetime.getTransitionTimestamp(transition, lastModified)!;
if (!isApplicable) {
return result;
}
return this.compareTransitions({
transition1: transition,
transition2: result,
lastModified,
});
}, undefined);
return this.compareTransitions({
transition1: transition,
transition2: store.Transition,
lastModified,
});
}
/**
* Find the most relevant trantition rule for the given transitions array
* and any previously stored transition from another rule.
* @param params.noncurrentTransitions - Array of lifecycle rule noncurrent
* transitions
* @param params.lastModified - The object's last modified
* @param params.currentDate - current date
* @param params.store - object containing applicable rules
* date
* @return The most applicable transition rule
*/
getApplicableNoncurrentVersionTransition(params: {
store: any;
currentDate: Date;
noncurrentTransitions: any[];
lastModified: string;
}) {
const { noncurrentTransitions, store, lastModified, currentDate } = params;
const ncvt = noncurrentTransitions.reduce((result, ncvt) => {
const isApplicable = // Is the transition time in the past?
this._datetime.getTimestamp(currentDate) >=
this._datetime.getTransitionTimestamp(ncvt, lastModified)!;
if (!isApplicable) {
return result;
}
return this.compareTransitions({
transition1: ncvt,
transition2: result,
lastModified,
});
}, undefined);
return this.compareTransitions({
transition1: ncvt,
transition2: store.NoncurrentVersionTransition,
lastModified,
});
}
// TODO
/**
* Filter out all rules based on `Status` and `Filter` (Prefix and Tags)
* @param bucketLCRules - array of bucket lifecycle rules
* @param item - represents a single object, version, or upload
* @param objTags - all tags for given `item`
* @return list of all filtered rules that apply to `item`
*/
filterRules(bucketLCRules: any[], item: any, objTags: any) {
/*
Bucket Tags must be included in the list of object tags.
So if a bucket tag with "key1/value1" exists, and an object with
"key1/value1, key2/value2" exists, this bucket lifecycle rules
apply on this object.
Vice versa, bucket rule is "key1/value1, key2/value2" and object
rule is "key1/value1", this buckets rule does not apply to this
object.
*/
function deepCompare(rTags: any, oTags: any) {
// check to make sure object tags length matches or is greater
if (rTags.length > oTags.length) {
return false;
}
// all key/value tags of bucket rules must be within object tags
for (let i = 0; i < rTags.length; i++) {
const oTag = oTags.find((pair: any) => pair.Key === rTags[i].Key);
if (!oTag || rTags[i].Value !== oTag.Value) {
return false;
}
}
return true;
}
return bucketLCRules.filter(rule => {
if (rule.Status === 'Disabled') {
return false;
}
// check all locations where prefix could possibly be
// console.log(rule.Prefix);
// console.log(rule.Filter);
// console.log(rule.Filter.And);
const prefix = rule.Prefix
|| (rule.Filter && (rule.Filter.And
? rule.Filter.And.Prefix
: rule.Filter.Prefix));
if (prefix && !item.Key.startsWith(prefix)) {
return false;
}
if (!rule.Filter) {
return true;
}
const tags = rule.Filter.And
? rule.Filter.And.Tags
: (rule.Filter.Tag && [rule.Filter.Tag]);
if (tags && !deepCompare(tags, objTags.TagSet || [])) {
return false;
}
return true;
});
}
// TODO
/**
* For all filtered rules, get rules that apply the earliest
* @param rules - list of filtered rules that apply to a specific
* object, version, or upload
* @param metadata - metadata about the object to transition
* @return all applicable rules with earliest dates of action
* i.e. { Expiration: { Date: <DateObject>, Days: 10 },
* NoncurrentVersionExpiration: { NoncurrentDays: 5 } }
*/
getApplicableRules(rules: any[], metadata: any) {
// Declare the current date before the reducing function so that all
// rule comparisons use the same date.
const currentDate = new Date();
/* eslint-disable no-param-reassign */
const applicableRules = rules.reduce((store, rule) => {
// filter and find earliest dates
if (rule.Expiration && this._supportedRules.includes('expiration')) {
if (!store.Expiration) {
store.Expiration = {};
}
if (rule.Expiration.Days) {
if (!store.Expiration.Days || rule.Expiration.Days
< store.Expiration.Days) {
store.Expiration.ID = rule.ID;
store.Expiration.Days = rule.Expiration.Days;
}
}
if (rule.Expiration.Date) {
if (!store.Expiration.Date || rule.Expiration.Date
< store.Expiration.Date) {
store.Expiration.ID = rule.ID;
store.Expiration.Date = rule.Expiration.Date;
}
}
const eodm = rule.Expiration.ExpiredObjectDeleteMarker;
if (eodm !== undefined) {
// preference for later rules in list of rules
store.Expiration.ID = rule.ID;
store.Expiration.ExpiredObjectDeleteMarker = eodm;
}
}
if (rule.NoncurrentVersionExpiration
&& this._supportedRules.includes('noncurrentVersionExpiration')) {
// Names are long, so obscuring a bit
const ncve = 'NoncurrentVersionExpiration';
const ncd = 'NoncurrentDays';
if (!store[ncve]) {
store[ncve] = {};
}
if (!store[ncve][ncd] || rule[ncve][ncd] < store[ncve][ncd]) {
store[ncve].ID = rule.ID;
store[ncve][ncd] = rule[ncve][ncd];
}
}
if (rule.AbortIncompleteMultipartUpload
&& this._supportedRules.includes('abortIncompleteMultipartUpload')) {
// Names are long, so obscuring a bit
const aimu = 'AbortIncompleteMultipartUpload';
const dai = 'DaysAfterInitiation';
if (!store[aimu]) {
store[aimu] = {};
}
if (!store[aimu][dai] || rule[aimu][dai] < store[aimu][dai]) {
store[aimu].ID = rule.ID;
store[aimu][dai] = rule[aimu][dai];
}
}
const hasTransitions = Array.isArray(rule.Transitions) && rule.Transitions.length > 0;
if (hasTransitions && this._supportedRules.includes('transitions')) {
store.Transition = this.getApplicableTransition({
transitions: rule.Transitions,
lastModified: metadata.LastModified,
store,
currentDate,
});
}
const ncvt = 'NoncurrentVersionTransitions';
const hasNoncurrentTransitions = Array.isArray(rule[ncvt]) && rule[ncvt].length > 0;
if (hasNoncurrentTransitions && this._supportedRules.includes('noncurrentVersionTransitions')) {
store.NoncurrentVersionTransition = this.getApplicableNoncurrentVersionTransition({
noncurrentTransitions: rule.NoncurrentVersionTransitions,
lastModified: metadata.LastModified,
store,
currentDate,
});
}
return store;
}, {});
// Do not transition to a location where the object is already stored.
if (applicableRules.Transition
&& applicableRules.Transition.StorageClass === metadata.StorageClass) {
applicableRules.Transition = undefined;
}
if (applicableRules.NoncurrentVersionTransition
&& applicableRules.NoncurrentVersionTransition.StorageClass === metadata.StorageClass) {
applicableRules.NoncurrentVersionTransition = undefined;
}
return applicableRules;
/* eslint-enable no-param-reassign */
}
}