-
Notifications
You must be signed in to change notification settings - Fork 898
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 provision dialogs #17739
Merged
gmcculloug
merged 3 commits into
ManageIQ:master
from
branic:provision_dialogs_export_import
Aug 1, 2018
Merged
Changes from 1 commit
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module TaskHelpers | ||
class Exports | ||
class ProvisionDialogs | ||
def export(options = {}) | ||
export_dir = options[:directory] | ||
|
||
dialogs = options[:all] ? MiqDialog.order(:id).all : MiqDialog.order(:id).where(:default => [false, nil]) | ||
|
||
dialogs = dialogs.collect do |dialog| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor de-duping. You can move the dialogs = options[:all] ? MiqDialog.all : MiqDialog.where(:default => [false, nil])
dialogs = dialogs.order(:id).collect do |dialog| |
||
Exports.exclude_attributes(dialog.to_model_hash, %i(file_mtime created_at updated_at id class)) | ||
end | ||
|
||
dialogs.each do |dialog| | ||
$log.info("Exporting Provision Dialog: #{dialog[:name]} (#{dialog[:description]})") | ||
|
||
fname = Exports.safe_filename("#{dialog[:dialog_type]}-#{dialog[:name]}", options[:keep_spaces]) | ||
File.write("#{export_dir}/#{fname}.yaml", dialog.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,20 @@ | ||
module TaskHelpers | ||
class Imports | ||
class ProvisionDialogs | ||
def import(options = {}) | ||
return unless options[:source] | ||
|
||
glob = File.file?(options[:source]) ? options[:source] : "#{options[:source]}/*.yaml" | ||
Dir.glob(glob) do |fname| | ||
$log.info("Importing Provision Dialog from: #{fname}") | ||
|
||
dialog = YAML.load_file(fname) | ||
|
||
miq_dialog = MiqDialog.find_by(:name => dialog[:name], :dialog_type => dialog[:dialog_type]) | ||
|
||
miq_dialog.nil? ? MiqDialog.create(dialog) : miq_dialog.update(dialog) | ||
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
118 changes: 118 additions & 0 deletions
118
spec/lib/task_helpers/exports/provision_dialogs_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,118 @@ | ||
describe TaskHelpers::Exports::ProvisionDialogs do | ||
let(:dialog_name1) { "default_dialog" } | ||
let(:dialog_name2) { "custom_dialog" } | ||
let(:dialog_desc1) { "Default Provisioning Dialog" } | ||
let(:dialog_desc2) { "Custom Provisioning Dialog" } | ||
let(:dialog_type1) { "MiqProvisionWorkflow" } | ||
let(:dialog_type2) { "MiqProvisionWorkflow" } | ||
let(:dialog_type3) { "VmMigrateWorkflow" } | ||
|
||
let(:content) do | ||
{ | ||
:dialogs => { | ||
:hardware => { | ||
:description => "Hardware", | ||
:fields => { | ||
:disk_format => { | ||
:description => "Disk Format", | ||
:required => false, | ||
:display => :edit, | ||
:default => "unchanged", | ||
:data_type => :string, | ||
:values => { | ||
:thick => "Thick", | ||
:thin => "Thin" | ||
} | ||
}, | ||
:cpu_limit => { | ||
:description => "CPU (MHz)", | ||
:required => false, | ||
:notes => "(-1 = Unlimited)", | ||
:display => :edit, | ||
:data_type => :integer, | ||
:notes_display => :show | ||
} | ||
} | ||
} | ||
} | ||
} | ||
end | ||
|
||
let(:content2) do | ||
{ | ||
:dialogs => { | ||
:buttons => %i(submit cancel) | ||
} | ||
} | ||
end | ||
|
||
let(:export_dir) do | ||
Dir.mktmpdir('miq_exp_dir') | ||
end | ||
|
||
before do | ||
FactoryGirl.create(:miq_dialog, | ||
:dialog_type => dialog_type1, | ||
:name => dialog_name1, | ||
:description => dialog_desc1, | ||
:content => content, | ||
:default => true) | ||
|
||
FactoryGirl.create(:miq_dialog, | ||
:dialog_type => dialog_type2, | ||
:name => dialog_name2, | ||
:description => dialog_desc2, | ||
:content => content, | ||
:default => false) | ||
end | ||
|
||
after do | ||
FileUtils.remove_entry export_dir | ||
end | ||
|
||
describe "when --all is not specified" do | ||
let(:dialog_filename1) { "#{export_dir}/#{dialog_type1}-custom_dialog.yaml" } | ||
let(:dialog_filename2) { "#{export_dir}/#{dialog_type3}-custom_dialog.yaml" } | ||
|
||
it 'exports user provision dialogs to a given directory' do | ||
TaskHelpers::Exports::ProvisionDialogs.new.export(:directory => export_dir) | ||
expect(Dir[File.join(export_dir, '**', '*')].count { |file| File.file?(file) }).to eq(1) | ||
dialog = YAML.load_file(dialog_filename1) | ||
expect(dialog[:content]).to eq(content) | ||
expect(dialog[:description]).to eq(dialog_desc2) | ||
end | ||
|
||
it 'exports dialogs with the same name to different files' do | ||
FactoryGirl.create(:miq_dialog, | ||
:dialog_type => dialog_type3, | ||
:name => dialog_name2, | ||
:description => dialog_desc2, | ||
:content => content2, | ||
:default => false) | ||
TaskHelpers::Exports::ProvisionDialogs.new.export(:directory => export_dir) | ||
expect(Dir[File.join(export_dir, '**', '*')].count { |file| File.file?(file) }).to eq(2) | ||
dialog = YAML.load_file(dialog_filename1) | ||
expect(dialog[:content]).to eq(content) | ||
expect(dialog[:description]).to eq(dialog_desc2) | ||
dialog2 = YAML.load_file(dialog_filename2) | ||
expect(dialog2[:content]).to eq(content2) | ||
expect(dialog2[:description]).to eq(dialog_desc2) | ||
end | ||
end | ||
|
||
describe "when --all is specified" do | ||
let(:dialog_filename1) { "#{export_dir}/#{dialog_type1}-default_dialog.yaml" } | ||
let(:dialog_filename2) { "#{export_dir}/#{dialog_type1}-custom_dialog.yaml" } | ||
|
||
it 'exports all provision dialogs to a given directory' do | ||
TaskHelpers::Exports::ProvisionDialogs.new.export(:directory => export_dir, :all => true) | ||
expect(Dir[File.join(export_dir, '**', '*')].count { |file| File.file?(file) }).to eq(2) | ||
dialog1 = YAML.load_file(dialog_filename1) | ||
dialog2 = YAML.load_file(dialog_filename2) | ||
expect(dialog1[:content]).to eq(content) | ||
expect(dialog1[:description]).to eq(dialog_desc1) | ||
expect(dialog2[:content]).to eq(content) | ||
expect(dialog2[:description]).to eq(dialog_desc2) | ||
end | ||
end | ||
end |
Oops, something went wrong.
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.
Could use collect! and not need the variable.
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.
collect!
isn't defined for anActiveRecord::Relation []
so the only option is usingcollect