-
Notifications
You must be signed in to change notification settings - Fork 897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for exporting and importing generic object definitions #18688
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fc78b82
Export GenericObjectDefinitions
romanblanco 621c18c
Import GenericObjectDefinitions
romanblanco 215e39b
Specs for the import/export of the Generic Object Definitions.
pkomanek 77e1879
Review response: Import/Export methods extracted into a mixin
romanblanco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module TaskHelpers | ||
class Exports | ||
class GenericObjectDefinitions | ||
def export(options = {}) | ||
export_dir = options[:directory] | ||
GenericObjectDefinition.all.each do |god| | ||
filename = Exports.safe_filename(god.name, options[:keep_spaces]) | ||
File.write("#{export_dir}/#{filename}.yaml", god.export_to_array.to_yaml) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module TaskHelpers | ||
class Imports | ||
class GenericObjectDefinitions | ||
def import(options = {}) | ||
return unless options[:source] | ||
|
||
glob = File.file?(options[:source]) ? options[:source] : "#{options[:source]}/*.yaml" | ||
Dir.glob(glob) do |filename| | ||
$log.info("Importing Generic Object Definitions from: #{filename}") | ||
|
||
god_options = {:overwrite => options[:overwrite]} | ||
|
||
begin | ||
god_fd = File.open(filename, 'r') | ||
GenericObjectDefinition.import(god_fd, god_options) | ||
rescue ActiveModel::UnknownAttributeError, RuntimeError => err | ||
$log.error("Error importing #{filename} : #{err.message}") | ||
warn("Error importing #{filename} : #{err.message}") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
spec/lib/task_helpers/exports/generic_object_definitions_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
describe TaskHelpers::Exports::GenericObjectDefinitions do | ||
let(:export_dir) do | ||
Dir.mktmpdir('miq_exp_dir') | ||
end | ||
|
||
after do | ||
FileUtils.remove_entry export_dir | ||
end | ||
|
||
context 'when there is something to export' do | ||
let(:god_filename1) { "#{export_dir}/#{@god1.name}.yaml" } | ||
let(:god_filename2) { "#{export_dir}/#{@god2.name}.yaml" } | ||
|
||
before do | ||
@god1 = FactoryBot.create(:generic_object_definition, :with_methods_attributes_associations, :description => 'god_description') | ||
@god2 = FactoryBot.create(:generic_object_definition) | ||
end | ||
|
||
it 'export all definitions' do | ||
TaskHelpers::Exports::GenericObjectDefinitions.new.export(:directory => export_dir) | ||
expect(Dir[File.join(export_dir, '**', '*')].count { |file| File.file?(file) }).to eq(2) | ||
god1_yaml = YAML.load_file(god_filename1) | ||
expect(god1_yaml.first["GenericObjectDefinition"]["name"]).to eq(@god1.name) | ||
expect(god1_yaml.first["GenericObjectDefinition"]["description"]).to eq(@god1.description) | ||
expect(god1_yaml.first["GenericObjectDefinition"]["properties"]).to eq(@god1.properties) | ||
|
||
god2_yaml = YAML.load_file(god_filename2) | ||
expect(god2_yaml.first["GenericObjectDefinition"]["name"]).to eq(@god2.name) | ||
expect(god2_yaml.first["GenericObjectDefinition"]["description"]).to eq(nil) | ||
expect(god2_yaml.first["GenericObjectDefinition"]["properties"]).to eq( | ||
:attributes => {}, :associations => {}, :methods => [] | ||
) | ||
end | ||
end | ||
|
||
context 'when there is nothing to export' do | ||
it 'export no definitions' do | ||
TaskHelpers::Exports::GenericObjectDefinitions.new.export(:directory => export_dir) | ||
expect(Dir[File.join(export_dir, '**', '*')].count { |file| File.file?(file) }).to eq(0) | ||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
spec/lib/task_helpers/imports/data/generic_object_definitions/apep.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
- GenericObjectDefinition: | ||
name: Apep | ||
description: Ancient Egyptian deity who embodied chaos | ||
properties: | ||
:attributes: | ||
weapon: :string | ||
is_tired: :boolean | ||
created: :datetime | ||
retirement: :datetime | ||
:associations: | ||
cloud_tenant: CloudTenant | ||
:methods: | ||
- kick | ||
- laugh_at | ||
- punch | ||
- parseltongue |
18 changes: 18 additions & 0 deletions
18
spec/lib/task_helpers/imports/data/generic_object_definitions/apep_update.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
- GenericObjectDefinition: | ||
name: Apep | ||
description: Updated description | ||
properties: | ||
:attributes: | ||
weapon: :string | ||
is_tired: :boolean | ||
created: :datetime | ||
retirement: :datetime | ||
:associations: | ||
cloud_tenant: CloudTenant | ||
:methods: | ||
- kick | ||
- laugh_at | ||
- punch | ||
- parseltongue | ||
- updated_method |
17 changes: 17 additions & 0 deletions
17
spec/lib/task_helpers/imports/data/generic_object_definitions/apophis.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
- GenericObjectDefinition: | ||
name: Apophis | ||
description: Ancient Egyptian deity who embodied chaos | ||
properties: | ||
:attributes: | ||
weapon: :string | ||
is_tired: :boolean | ||
created: :datetime | ||
retirement: :datetime | ||
:associations: | ||
cloud_tenant: CloudTenant | ||
:methods: | ||
- kick | ||
- laugh_at | ||
- punch | ||
- parseltongue |
18 changes: 18 additions & 0 deletions
18
spec/lib/task_helpers/imports/data/generic_object_definitions/god_attr_error.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
- GenericObjectDefinition: | ||
name: Apep | ||
invalid_attribute: Apophis | ||
description: Ancient Egyptian deity who embodied chaos | ||
properties: | ||
:attributes: | ||
weapon: :string | ||
is_tired: :boolean | ||
created: :datetime | ||
retirement: :datetime | ||
:associations: | ||
cloud_tenant: CloudTenant | ||
:methods: | ||
- kick | ||
- laugh_at | ||
- punch | ||
- parseltongue |
5 changes: 5 additions & 0 deletions
5
spec/lib/task_helpers/imports/data/generic_object_definitions/god_runtime_error.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
- GenericObjectDefinition: | ||
name: | ||
description: Ancient Egyptian deity who embodied chaos | ||
properties: |
130 changes: 130 additions & 0 deletions
130
spec/lib/task_helpers/imports/generic_object_definitions_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
describe TaskHelpers::Imports::GenericObjectDefinitions do | ||
describe "#import" do | ||
let(:data_dir) { File.join(File.expand_path(__dir__), 'data', 'generic_object_definitions') } | ||
let(:options) { { :source => source, :overwrite => overwrite } } | ||
let(:god_name1) { "Apep" } | ||
let(:god_name2) { "Apophis" } | ||
let(:god_file1) { "apep.yaml" } | ||
let(:god_file2) { "apophis.yaml" } | ||
let(:god_desc1) { "Ancient Egyptian deity who embodied chaos" } | ||
let(:god_desc1_updated) { "Updated description" } | ||
let(:runt_err_file) { "god_runtime_error.yml" } | ||
let(:attr_err_file) { "god_attr_error.yml" } | ||
let(:god_prop1) do | ||
{ | ||
:attributes => { | ||
'weapon' => :string, | ||
'is_tired' => :boolean, | ||
'created' => :datetime, | ||
'retirement' => :datetime | ||
}, | ||
:associations => { 'cloud_tenant' => 'CloudTenant' }, | ||
:methods => ['kick', 'laugh_at', 'punch', 'parseltongue'] | ||
} | ||
end | ||
|
||
describe "when the source is a directory" do | ||
let(:source) { data_dir } | ||
let(:overwrite) { true } | ||
|
||
it 'imports all .yaml files in a specified directory' do | ||
expect do | ||
TaskHelpers::Imports::GenericObjectDefinitions.new.import(options) | ||
end.to_not output.to_stderr | ||
expect(GenericObjectDefinition.all.count).to eq(2) | ||
assert_test_god_one_present | ||
assert_test_god_two_present | ||
end | ||
end | ||
|
||
describe "when the source is a file" do | ||
let(:source) { "#{data_dir}/#{god_file1}" } | ||
let(:overwrite) { true } | ||
|
||
it 'imports a specified file' do | ||
expect do | ||
TaskHelpers::Imports::GenericObjectDefinitions.new.import(options) | ||
end.to_not output.to_stderr | ||
expect(GenericObjectDefinition.all.count).to eq(1) | ||
assert_test_god_one_present | ||
end | ||
end | ||
|
||
describe "when the source file modifies an existing generic object definition" do | ||
let(:update_file) { "apep_update.yml" } | ||
let(:source) { "#{data_dir}/#{update_file}" } | ||
|
||
before do | ||
TaskHelpers::Imports::GenericObjectDefinitions.new.import(:source => "#{data_dir}/#{god_file1}") | ||
end | ||
|
||
context 'overwrite is true' do | ||
let(:overwrite) { true } | ||
|
||
it 'overwrites an existing generic object definition' do | ||
expect do | ||
TaskHelpers::Imports::GenericObjectDefinitions.new.import(options) | ||
end.to_not output.to_stderr | ||
assert_test_god_one_modified | ||
end | ||
end | ||
|
||
context 'overwrite is false' do | ||
let(:overwrite) { false } | ||
|
||
it 'does not overwrite an existing generic object definition' do | ||
expect do | ||
TaskHelpers::Imports::GenericObjectDefinitions.new.import(options) | ||
end.to_not output.to_stderr | ||
assert_test_god_one_present | ||
end | ||
end | ||
end | ||
|
||
describe "when the source file has invalid settings" do | ||
let(:overwrite) { true } | ||
|
||
context "when the object type is invalid" do | ||
let(:source) { "#{data_dir}/#{runt_err_file}" } | ||
|
||
it 'generates an error' do | ||
expect do | ||
TaskHelpers::Imports::GenericObjectDefinitions.new.import(options) | ||
end.to output(/Incorrect format/).to_stderr | ||
end | ||
end | ||
|
||
context "when an attribute is invalid" do | ||
let(:source) { "#{data_dir}/#{attr_err_file}" } | ||
|
||
it 'generates an error' do | ||
expect do | ||
TaskHelpers::Imports::GenericObjectDefinitions.new.import(options) | ||
end.to output(/unknown attribute 'invalid_attribute'/).to_stderr | ||
end | ||
end | ||
end | ||
end | ||
|
||
def assert_test_god_one_present | ||
god = GenericObjectDefinition.find_by(:name => god_name1) | ||
expect(god.name).to eq(god_name1) | ||
expect(god.description).to eq(god_desc1) | ||
expect(god.properties).to eq(god_prop1) | ||
end | ||
|
||
def assert_test_god_two_present | ||
god = GenericObjectDefinition.find_by(:name => god_name2) | ||
expect(god.name).to eq(god_name2) | ||
expect(god.description).to eq(god_desc1) | ||
expect(god.properties).to eq(god_prop1) | ||
end | ||
|
||
def assert_test_god_one_modified | ||
god = GenericObjectDefinition.find_by(:name => god_name1) | ||
expect(god.name).to eq(god_name1) | ||
expect(god.description).to eq(god_desc1_updated) | ||
god_prop1[:methods] << 'updated_method' | ||
expect(god.properties).to eq(god_prop1) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOOOL. I love this.