Skip to content

Commit 2cd76e3

Browse files
committed
Add call to set_s3_lifecycle method in iterate_and_log_notify_errors
1 parent 631f7e0 commit 2cd76e3

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

app/models/conditions_response/backup.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def self.condition_response(condition, log, use_slack_notification: true)
8686

8787
iterate_and_log_notify_errors(backup_files, 'in backup_files loop, uploading_file_to_s3', log) do |backup_file|
8888
upload_file_to_s3(aws_s3, aws_s3_backup_bucket, aws_backup_bucket_full_prefix, backup_file)
89+
# When we first upload our file to s3, the default storage class is STANDARD
90+
# After 1 month, we want to to transition the object to STANDARD IA, then GLACIER after 3 months. This will help us save on costs.
91+
# This however has effects on retreival time for objects which you can see in this performance chart https://aws.amazon.com/s3/storage-classes/#Performance_across_the_S3_Storage_Classes
92+
set_s3_lifecycle_rules(bucket_name: aws_s3_backup_bucket, bucket_full_prefix: aws_backup_bucket_full_prefix, status: 'enabled', storage_rules: [{days: 30, storage_class: 'STANDARD_IA'}, {days: 90, storage_class: 'GLACIER'}])
8993
end
9094

9195
log.record('info', 'Pruning older backups on local storage')
@@ -300,7 +304,7 @@ class << self
300304
end
301305

302306
# s3_lifecycle_rules(bucket_name: 'bucket_name', bucket_full_prefix: 'bucket_full_prefix', status: 'enabled', storage_rules: [{days: 30, storage_class: 'STANDARD_IA'}, {days: 90, storage_class: 'GLACIER'}])
303-
def self.s3_lifecycle_rules(bucket_name:, bucket_full_prefix:, status:, storage_rules:)
307+
def self.set_s3_lifecycle_rules(bucket_name:, bucket_full_prefix:, status:, storage_rules:)
304308
client = Aws::S3::Client.new(region: ENV['SHF_AWS_S3_BACKUP_REGION'],
305309
credentials: Aws::Credentials.new(ENV['SHF_AWS_S3_BACKUP_KEY_ID'], ENV['SHF_AWS_S3_BACKUP_SECRET_ACCESS_KEY']))
306310

spec/models/conditions_response/backup_spec.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,6 @@ def create_faux_backup_file(backups_dir, file_prefix)
692692
let!(:temp_backups_dir) { Dir.mktmpdir('faux-backups-dir') }
693693
let!(:faux_backup_fn) { create_faux_backup_file(temp_backups_dir, 'faux_backup.bak') }
694694

695-
696695
it '.upload_file_to_s3 calls .upload_file for the bucket, full object name, and file to upload' do
697696
expect(mock_bucket_object).to receive(:upload_file).with(faux_backup_fn, anything)
698697
Backup.upload_file_to_s3(mock_s3, bucket_name, bucket_full_prefix, faux_backup_fn)
@@ -1251,7 +1250,9 @@ def create_faux_backup_file(backups_dir, file_prefix)
12511250

12521251

12531252
describe 'iterate_and_log_notify_errors(list, slack_error_details, log)' do
1254-
1253+
let(:status) { 'Enabled' }
1254+
let(:storage_rules) { [{days: 30, storage_class: 'STANDARD_IA'}, {days: 90, storage_class: 'GLACIER'}] }
1255+
12551256
before(:each) do
12561257
allow(SHFNotifySlack).to receive(:failure_notification)
12571258
.with(anything, anything)
@@ -1303,9 +1304,14 @@ def create_faux_backup_file(backups_dir, file_prefix)
13031304
expect(@result_str).to eq 'ac'
13041305
end
13051306

1307+
it 'adds a bucket lifecycle policy to the object' do
1308+
expect(described_class).to receive(:set_s3_lifecycle_rules).with(bucket_name: bucket_name, bucket_full_prefix: bucket_full_prefix, status: status, storage_rules: storage_rules)
1309+
described_class.set_s3_lifecycle_rules(bucket_name: bucket_name, bucket_full_prefix: bucket_full_prefix, status: status, storage_rules: storage_rules)
1310+
end
1311+
13061312
end
13071313

1308-
describe 's3_lifecycle_rules(bucket, bucket_full_prefix, status, *storage_rules_kwargs)' do
1314+
describe 'set_s3_lifecycle_rules(bucket, bucket_full_prefix, status, *storage_rules_kwargs)' do
13091315
let(:invalid_storage_class_list) { ['INVALID_STORAGE_CLASS', 'OTHER_INVALID_STORAGE_CLASS'] }
13101316
let(:another_invalid_storage_class_list) { ['INVALID_STORAGE_CLASS', 'STANDARD_IA', 'GLACIER'] }
13111317
let(:status) { 'Enabled' }
@@ -1323,9 +1329,9 @@ def create_faux_backup_file(backups_dir, file_prefix)
13231329
client
13241330
end
13251331

1326-
it 'calls #s3_lifecycle_rules once' do
1327-
expect(described_class).to receive(:s3_lifecycle_rules).with(bucket_name: bucket_name, bucket_full_prefix: bucket_full_prefix, status: status, storage_rules: storage_rules)
1328-
described_class.s3_lifecycle_rules(bucket_name: bucket_name, bucket_full_prefix: bucket_full_prefix, status: status, storage_rules: storage_rules)
1332+
it 'calls #set_s3_lifecycle_rules once' do
1333+
expect(described_class).to receive(:set_s3_lifecycle_rules).with(bucket_name: bucket_name, bucket_full_prefix: bucket_full_prefix, status: status, storage_rules: storage_rules)
1334+
described_class.set_s3_lifecycle_rules(bucket_name: bucket_name, bucket_full_prefix: bucket_full_prefix, status: status, storage_rules: storage_rules)
13291335
end
13301336

13311337
it "returns 'Invalid storage class' for a list containing only invalid storage classes" do

0 commit comments

Comments
 (0)