Skip to content

Commit 972064b

Browse files
committed
refactor: rename core to resources so that individual resource-s can be later broken out
1 parent 9eee0e5 commit 972064b

File tree

10 files changed

+74
-74
lines changed

10 files changed

+74
-74
lines changed

lib/datadog_backup.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require_relative 'datadog_backup/local_filesystem'
66
require_relative 'datadog_backup/options'
77
require_relative 'datadog_backup/cli'
8-
require_relative 'datadog_backup/core'
8+
require_relative 'datadog_backup/resources'
99
require_relative 'datadog_backup/dashboards'
1010
require_relative 'datadog_backup/monitors'
1111
require_relative 'datadog_backup/synthetics'

lib/datadog_backup/dashboards.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module DatadogBackup
44
# Dashboards specific overrides for backup and restore.
5-
class Dashboards < Core
5+
class Dashboards < Resources
66
def all
77
get_all.fetch('dashboards')
88
end

lib/datadog_backup/local_filesystem.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
module DatadogBackup
99
##
10-
# Meant to be mixed into DatadogBackup::Core
10+
# Meant to be mixed into DatadogBackup::Resources
1111
# Relies on @options[:backup_dir] and @options[:output_format]
1212
module LocalFilesystem
1313
def all_files

lib/datadog_backup/monitors.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module DatadogBackup
44
# Monitor specific overrides for backup and restore.
5-
class Monitors < Core
5+
class Monitors < Resources
66
def all
77
get_all
88
end

lib/datadog_backup/core.rb renamed to lib/datadog_backup/resources.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
module DatadogBackup
99
# The default options for backing up and restores.
1010
# This base class is meant to be extended by specific resources, such as Dashboards, Monitors, and so on.
11-
class Core
11+
class Resources
1212
include ::DatadogBackup::LocalFilesystem
1313
include ::DatadogBackup::Options
1414

lib/datadog_backup/synthetics.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module DatadogBackup
44
# Synthetic specific overrides for backup and restore.
5-
class Synthetics < Core
5+
class Synthetics < Resources
66
def all
77
get_all.fetch('tests')
88
end

lib/datadog_backup/thread_pool.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
module DatadogBackup
4-
# Used by CLI and Dashboards to size thread pool according to available CPU cores.
4+
# Used by CLI and Dashboards to size thread pool according to available CPU resourcess.
55
module ThreadPool
66
TPOOL = ::Concurrent::ThreadPoolExecutor.new(
77
min_threads: [2, Concurrent.processor_count].max,

spec/datadog_backup/core_spec.rb

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33
require 'spec_helper'
44

5-
describe DatadogBackup::Core do
5+
describe DatadogBackup::Resources do
66
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
77
let(:api_client_double) { Faraday.new { |f| f.adapter :test, stubs } }
88
let(:tempdir) { Dir.mktmpdir }
9-
let(:core) do
10-
core = described_class.new(
9+
let(:resources) do
10+
resources = described_class.new(
1111
action: 'backup',
1212
backup_dir: tempdir,
1313
diff_format: nil,
1414
resources: [],
1515
output_format: :json
1616
)
17-
allow(core).to receive(:api_service).and_return(api_client_double)
18-
return core
17+
allow(resources).to receive(:api_service).and_return(api_client_double)
18+
return resources
1919
end
2020

2121
describe '#diff' do
22-
subject(:diff) { core.diff('diff') }
22+
subject(:diff) { resources.diff('diff') }
2323

2424
before do
25-
allow(core).to receive(:get_by_id).and_return({ 'text' => 'diff1', 'extra' => 'diff1' })
26-
core.write_file('{"text": "diff2", "extra": "diff2"}', "#{tempdir}/core/diff.json")
25+
allow(resources).to receive(:get_by_id).and_return({ 'text' => 'diff1', 'extra' => 'diff1' })
26+
resources.write_file('{"text": "diff2", "extra": "diff2"}', "#{tempdir}/resources/diff.json")
2727
end
2828

2929
it {
@@ -39,55 +39,55 @@
3939
end
4040

4141
describe '#except' do
42-
subject { core.except({ a: :b, b: :c }) }
42+
subject { resources.except({ a: :b, b: :c }) }
4343

4444
it { is_expected.to eq({ a: :b, b: :c }) }
4545
end
4646

4747
describe '#initialize' do
48-
subject(:mycore) { core }
48+
subject(:myresources) { resources }
4949

5050
it 'makes the subdirectories' do
5151
fileutils = class_double(FileUtils).as_stubbed_const
5252
allow(fileutils).to receive(:mkdir_p)
53-
mycore
54-
expect(fileutils).to have_received(:mkdir_p).with("#{tempdir}/core")
53+
myresources
54+
expect(fileutils).to have_received(:mkdir_p).with("#{tempdir}/resources")
5555
end
5656
end
5757

5858
describe '#myclass' do
59-
subject { core.myclass }
59+
subject { resources.myclass }
6060

61-
it { is_expected.to eq 'core' }
61+
it { is_expected.to eq 'resources' }
6262
end
6363

6464
describe '#create' do
65-
subject(:create) { core.create({ 'a' => 'b' }) }
65+
subject(:create) { resources.create({ 'a' => 'b' }) }
6666

6767
example 'it will post /api/v1/dashboard' do
68-
allow(core).to receive(:api_version).and_return('v1')
69-
allow(core).to receive(:api_resource_name).and_return('dashboard')
68+
allow(resources).to receive(:api_version).and_return('v1')
69+
allow(resources).to receive(:api_resource_name).and_return('dashboard')
7070
stubs.post('/api/v1/dashboard', { 'a' => 'b' }) { respond_with200({ 'id' => 'whatever-id-abc' }) }
7171
create
7272
stubs.verify_stubbed_calls
7373
end
7474
end
7575

7676
describe '#update' do
77-
subject(:update) { core.update('abc-123-def', { 'a' => 'b' }) }
77+
subject(:update) { resources.update('abc-123-def', { 'a' => 'b' }) }
7878

7979
example 'it puts /api/v1/dashboard' do
80-
allow(core).to receive(:api_version).and_return('v1')
81-
allow(core).to receive(:api_resource_name).and_return('dashboard')
80+
allow(resources).to receive(:api_version).and_return('v1')
81+
allow(resources).to receive(:api_resource_name).and_return('dashboard')
8282
stubs.put('/api/v1/dashboard/abc-123-def', { 'a' => 'b' }) { respond_with200({ 'id' => 'whatever-id-abc' }) }
8383
update
8484
stubs.verify_stubbed_calls
8585
end
8686

8787
context 'when the id is not found' do
8888
before do
89-
allow(core).to receive(:api_version).and_return('v1')
90-
allow(core).to receive(:api_resource_name).and_return('dashboard')
89+
allow(resources).to receive(:api_version).and_return('v1')
90+
allow(resources).to receive(:api_resource_name).and_return('dashboard')
9191
stubs.put('/api/v1/dashboard/abc-123-def', { 'a' => 'b' }) { [404, {}, { 'id' => 'whatever-id-abc' }] }
9292
end
9393

@@ -99,52 +99,52 @@
9999

100100
describe '#restore' do
101101
before do
102-
allow(core).to receive(:api_version).and_return('api-version-string')
103-
allow(core).to receive(:api_resource_name).and_return('api-resource-name-string')
102+
allow(resources).to receive(:api_version).and_return('api-version-string')
103+
allow(resources).to receive(:api_resource_name).and_return('api-resource-name-string')
104104
stubs.get('/api/api-version-string/api-resource-name-string/abc-123-def') { respond_with200({ 'test' => 'ok' }) }
105105
stubs.get('/api/api-version-string/api-resource-name-string/bad-123-id') do
106106
[404, {}, { 'error' => 'blahblah_not_found' }]
107107
end
108-
allow(core).to receive(:load_from_file_by_id).and_return({ 'load' => 'ok' })
108+
allow(resources).to receive(:load_from_file_by_id).and_return({ 'load' => 'ok' })
109109
end
110110

111111
context 'when id exists' do
112-
subject(:restore) { core.restore('abc-123-def') }
112+
subject(:restore) { resources.restore('abc-123-def') }
113113

114114
example 'it calls out to update' do
115-
allow(core).to receive(:update)
115+
allow(resources).to receive(:update)
116116
restore
117-
expect(core).to have_received(:update).with('abc-123-def', { 'load' => 'ok' })
117+
expect(resources).to have_received(:update).with('abc-123-def', { 'load' => 'ok' })
118118
end
119119
end
120120

121121
context 'when id does not exist on remote' do
122-
subject(:restore_newly) { core.restore('bad-123-id') }
122+
subject(:restore_newly) { resources.restore('bad-123-id') }
123123

124124
let(:fileutils) { class_double(FileUtils).as_stubbed_const }
125125

126126
before do
127-
allow(core).to receive(:load_from_file_by_id).and_return({ 'load' => 'ok' })
127+
allow(resources).to receive(:load_from_file_by_id).and_return({ 'load' => 'ok' })
128128
stubs.put('/api/api-version-string/api-resource-name-string/bad-123-id') do
129129
[404, {}, { 'error' => 'id not found' }]
130130
end
131131
stubs.post('/api/api-version-string/api-resource-name-string', { 'load' => 'ok' }) do
132132
respond_with200({ 'id' => 'my-new-id' })
133133
end
134134
allow(fileutils).to receive(:rm)
135-
allow(core).to receive(:create).with({ 'load' => 'ok' }).and_return({ 'id' => 'my-new-id' })
136-
allow(core).to receive(:get_and_write_file)
137-
allow(core).to receive(:find_file_by_id).with('bad-123-id').and_return('/path/to/bad-123-id.json')
135+
allow(resources).to receive(:create).with({ 'load' => 'ok' }).and_return({ 'id' => 'my-new-id' })
136+
allow(resources).to receive(:get_and_write_file)
137+
allow(resources).to receive(:find_file_by_id).with('bad-123-id').and_return('/path/to/bad-123-id.json')
138138
end
139139

140140
example 'it calls out to create' do
141141
restore_newly
142-
expect(core).to have_received(:create).with({ 'load' => 'ok' })
142+
expect(resources).to have_received(:create).with({ 'load' => 'ok' })
143143
end
144144

145145
example 'it saves the new file' do
146146
restore_newly
147-
expect(core).to have_received(:get_and_write_file).with('my-new-id')
147+
expect(resources).to have_received(:get_and_write_file).with('my-new-id')
148148
end
149149

150150
example 'it deletes the old file' do

0 commit comments

Comments
 (0)