Skip to content
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
merged 3 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/task_helpers/exports/provision_dialogs.rb
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|
Copy link
Contributor

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.

Copy link
Contributor Author

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 an ActiveRecord::Relation [] so the only option is using collect

     NoMethodError:
       undefined method `collect!' for #<ActiveRecord::Relation []>
       Did you mean?  collect

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor de-duping. You can move the order(:all) down to the caller.

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
20 changes: 20 additions & 0 deletions lib/task_helpers/imports/provision_dialogs.rb
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
17 changes: 17 additions & 0 deletions lib/tasks/evm_export_import.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# * Roles
# * Tags
# * Service Dialogs
# * Provision Dialogs

namespace :evm do
namespace :export do
Expand Down Expand Up @@ -76,6 +77,14 @@ namespace :evm do

exit # exit so that parameters to the first rake task are not run as rake tasks
end

desc 'Exports all provision dialogs to individual YAML files'
task :provision_dialogs => :environment do
options = TaskHelpers::Exports.parse_options
TaskHelpers::Exports::ProvisionDialogs.new.export(options)

exit # exit so that parameters to the first rake task are not run as rake tasks
end
end

namespace :import do
Expand Down Expand Up @@ -140,5 +149,13 @@ namespace :evm do

exit # exit so that parameters to the first rake task are not run as rake tasks
end

desc 'Imports all provision dialogs from individual YAML files'
task :provision_dialogs => :environment do
options = TaskHelpers::Imports.parse_options
TaskHelpers::Imports::ProvisionDialogs.new.import(options)

exit # exit so that parameters to the first rake task are not run as rake tasks
end
end
end
118 changes: 118 additions & 0 deletions spec/lib/task_helpers/exports/provision_dialogs_spec.rb
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
Loading