diff --git a/lib/task_helpers/exports/service_dialogs.rb b/lib/task_helpers/exports/service_dialogs.rb new file mode 100644 index 00000000000..40d8c89a451 --- /dev/null +++ b/lib/task_helpers/exports/service_dialogs.rb @@ -0,0 +1,16 @@ +module TaskHelpers + class Exports + class ServiceDialogs + def export(options = {}) + export_dir = options[:directory] + + dialogs = DialogSerializer.new.serialize(Dialog.order(:id).all) + + dialogs.each do |dialog| + fname = Exports.safe_filename(dialog['label'], options[:keep_spaces]) + File.write("#{export_dir}/#{fname}.yaml", [dialog].to_yaml) + end + end + end + end +end diff --git a/lib/task_helpers/imports/service_dialogs.rb b/lib/task_helpers/imports/service_dialogs.rb new file mode 100644 index 00000000000..e9960f51ee0 --- /dev/null +++ b/lib/task_helpers/imports/service_dialogs.rb @@ -0,0 +1,14 @@ +module TaskHelpers + class Imports + class ServiceDialogs + def import(options = {}) + return unless options[:source] + + glob = File.file?(options[:source]) ? options[:source] : "#{options[:source]}/*.yaml" + Dir.glob(glob) do |fname| + DialogImportService.new.import_all_service_dialogs_from_yaml_file(fname) + end + end + end + end +end diff --git a/lib/tasks/dialogs.rake b/lib/tasks/dialogs.rake index e1683ea59cf..515e7d55ba1 100644 --- a/lib/tasks/dialogs.rake +++ b/lib/tasks/dialogs.rake @@ -1,11 +1,13 @@ namespace :dialogs do desc "Imports dialogs from a yml file" task :import, [:filename] => [:environment] do |_, arguments| + warn('dialogs:import is deprecated. Use evm:import:service_dialogs.') TaskHelpers::DialogImportHelper.new.import(arguments[:filename]) end desc "Exports all dialogs to a yml file" task :export, [:filename] => [:environment] do |_, arguments| + warn('dialogs:export is deprecated. Use evm:export:service_dialogs.') timestamp = format_timezone(Time.current, Time.zone, "export_filename") filename = arguments[:filename] || "dialog_export_#{timestamp}.yml" TaskHelpers::DialogExporter.new.export(filename) diff --git a/lib/tasks/evm_export_import.rake b/lib/tasks/evm_export_import.rake index 47f914db73e..c2889aeb013 100644 --- a/lib/tasks/evm_export_import.rake +++ b/lib/tasks/evm_export_import.rake @@ -1,4 +1,9 @@ -# Rake script to export and import Alerts and AlertSets (Alert Profiles) +# Rake script to export and import +# * Alerts and AlertSets (Alert Profiles) +# * Policies and PolicySets (Policy Profiles) +# * Roles +# * Tags +# * Service Dialogs namespace :evm do namespace :export do @@ -49,6 +54,14 @@ namespace :evm do exit # exit so that parameters to the first rake task are not run as rake tasks end + + desc 'Exports all service dialogs to individual YAML files' + task :service_dialogs => :environment do + options = TaskHelpers::Exports.parse_options + TaskHelpers::Exports::ServiceDialogs.new.export(options) + + exit # exit so that parameters to the first rake task are not run as rake tasks + end end namespace :import do @@ -99,5 +112,13 @@ namespace :evm do exit # exit so that parameters to the first rake task are not run as rake tasks end + + desc 'Imports all service dialogs from individual YAML files' + task :service_dialogs => :environment do + options = TaskHelpers::Imports.parse_options + TaskHelpers::Imports::ServiceDialogs.new.import(options) + + exit # exit so that parameters to the first rake task are not run as rake tasks + end end end diff --git a/spec/lib/task_helpers/exports/service_dialogs_spec.rb b/spec/lib/task_helpers/exports/service_dialogs_spec.rb new file mode 100644 index 00000000000..873a5bfeb53 --- /dev/null +++ b/spec/lib/task_helpers/exports/service_dialogs_spec.rb @@ -0,0 +1,47 @@ +describe TaskHelpers::Exports::ServiceDialogs do + let(:buttons) { "the buttons" } + let(:description1) { "the first description" } + let(:description2) { "the second description" } + let(:label1) { "the first label" } + let(:label2) { "TheSecondLabel" } + + let(:expected_data1) do + [{ + "description" => description1, + "buttons" => buttons, + "label" => label1, + "dialog_tabs" => [] + }] + end + + let(:expected_data2) do + [{ + "description" => description2, + "buttons" => buttons, + "label" => label2, + "dialog_tabs" => [], + }] + end + + let(:export_dir) do + Dir.mktmpdir('miq_exp_dir') + end + + before do + FactoryGirl.create(:dialog, :name => label1, :description => description1, :buttons => buttons) + FactoryGirl.create(:dialog, :name => label2, :description => description2, :buttons => buttons) + end + + after do + FileUtils.remove_entry export_dir + end + + it 'exports service dialogs as individual files in a given directory' do + TaskHelpers::Exports::ServiceDialogs.new.export(:directory => export_dir) + file_contents = File.read("#{export_dir}/the_first_label.yaml") + file_contents2 = File.read("#{export_dir}/TheSecondLabel.yaml") + expect(YAML.safe_load(file_contents)).to eq(expected_data1) + expect(YAML.safe_load(file_contents2)).to eq(expected_data2) + expect(Dir[File.join(export_dir, '**', '*')].count { |file| File.file?(file) }).to eq(2) + end +end diff --git a/spec/lib/task_helpers/imports/data/service_dialogs/Simple_Dialog.yaml b/spec/lib/task_helpers/imports/data/service_dialogs/Simple_Dialog.yaml new file mode 100644 index 00000000000..60f48f31f59 --- /dev/null +++ b/spec/lib/task_helpers/imports/data/service_dialogs/Simple_Dialog.yaml @@ -0,0 +1,97 @@ +--- +- description: Simple Dialog to test export and import + buttons: submit,cancel + label: Simple Dialog + dialog_tabs: + - description: + display: + label: New tab + display_method: + display_method_options: + position: 0 + dialog_groups: + - description: + display: + label: New section + display_method: + display_method_options: + position: 0 + dialog_fields: + - name: text_box_1 + description: '' + data_type: string + notes: + notes_display: + display: edit + display_method: + display_method_options: {} + required: false + required_method: + required_method_options: {} + default_value: '' + values: + values_method: + values_method_options: {} + options: + :protected: false + label: Text Box + position: 0 + validator_type: + validator_rule: + reconfigurable: false + dynamic: false + show_refresh_button: false + load_values_on_init: false + read_only: false + auto_refresh: false + trigger_auto_refresh: false + visible: true + type: DialogFieldTextBox + resource_action: + action: + resource_type: DialogField + ae_namespace: + ae_class: + ae_instance: + ae_message: + ae_attributes: {} + dialog_field_responders: [] + - name: check_box_1 + description: '' + data_type: + notes: + notes_display: + display: edit + display_method: + display_method_options: {} + required: true + required_method: + required_method_options: {} + default_value: '' + values: + values_method: + values_method_options: {} + options: + :protected: false + label: Check Box + position: 1 + validator_type: + validator_rule: + reconfigurable: false + dynamic: false + show_refresh_button: false + load_values_on_init: false + read_only: false + auto_refresh: false + trigger_auto_refresh: false + visible: true + type: DialogFieldCheckBox + resource_action: + action: + resource_type: DialogField + ae_namespace: + ae_class: + ae_instance: + ae_message: + ae_attributes: {} + dialog_field_responders: [] diff --git a/spec/lib/task_helpers/imports/data/service_dialogs/Simple_Dialog_modified.yml b/spec/lib/task_helpers/imports/data/service_dialogs/Simple_Dialog_modified.yml new file mode 100644 index 00000000000..ee532a36a00 --- /dev/null +++ b/spec/lib/task_helpers/imports/data/service_dialogs/Simple_Dialog_modified.yml @@ -0,0 +1,97 @@ +--- +- description: Simple Dialog to test export and import + buttons: submit,cancel + label: Simple Dialog + dialog_tabs: + - description: + display: + label: Modified New tab + display_method: + display_method_options: + position: 0 + dialog_groups: + - description: + display: + label: New section + display_method: + display_method_options: + position: 0 + dialog_fields: + - name: text_box_1 + description: '' + data_type: string + notes: + notes_display: + display: edit + display_method: + display_method_options: {} + required: false + required_method: + required_method_options: {} + default_value: '' + values: + values_method: + values_method_options: {} + options: + :protected: false + label: Text Box + position: 0 + validator_type: + validator_rule: + reconfigurable: false + dynamic: false + show_refresh_button: false + load_values_on_init: false + read_only: false + auto_refresh: false + trigger_auto_refresh: false + visible: true + type: DialogFieldTextBox + resource_action: + action: + resource_type: DialogField + ae_namespace: + ae_class: + ae_instance: + ae_message: + ae_attributes: {} + dialog_field_responders: [] + - name: check_box_1 + description: '' + data_type: + notes: + notes_display: + display: edit + display_method: + display_method_options: {} + required: true + required_method: + required_method_options: {} + default_value: '' + values: + values_method: + values_method_options: {} + options: + :protected: false + label: Check Box + position: 1 + validator_type: + validator_rule: + reconfigurable: false + dynamic: false + show_refresh_button: false + load_values_on_init: false + read_only: false + auto_refresh: false + trigger_auto_refresh: false + visible: true + type: DialogFieldCheckBox + resource_action: + action: + resource_type: DialogField + ae_namespace: + ae_class: + ae_instance: + ae_message: + ae_attributes: {} + dialog_field_responders: [] diff --git a/spec/lib/task_helpers/imports/data/service_dialogs/Test_Dialog.yaml b/spec/lib/task_helpers/imports/data/service_dialogs/Test_Dialog.yaml new file mode 100644 index 00000000000..4d761ebfec9 --- /dev/null +++ b/spec/lib/task_helpers/imports/data/service_dialogs/Test_Dialog.yaml @@ -0,0 +1,117 @@ +--- +- description: + buttons: submit,cancel + label: Test Dialog + dialog_tabs: + - description: + display: + label: New tab + display_method: + display_method_options: + position: 0 + dialog_groups: + - description: + display: + label: New section + display_method: + display_method_options: + position: 0 + dialog_fields: + - name: test_dropdown + description: '' + data_type: string + notes: + notes_display: + display: edit + display_method: + display_method_options: {} + required: true + required_method: + required_method_options: {} + default_value: '' + values: + - - + - "" + - - 1 + - One + - - 3 + - Three + - - 2 + - Two + values_method: + values_method_options: {} + options: + :sort_by: description + :sort_order: ascending + :force_multi_value: true + label: Test Dropdown + position: 0 + validator_type: + validator_rule: + reconfigurable: false + dynamic: false + show_refresh_button: false + load_values_on_init: false + read_only: false + auto_refresh: false + trigger_auto_refresh: false + visible: true + type: DialogFieldDropDownList + resource_action: + action: + resource_type: DialogField + ae_namespace: + ae_class: + ae_instance: + ae_message: + ae_attributes: {} + dialog_field_responders: [] + - name: option_0_test_drop_2 + description: '' + data_type: string + notes: + notes_display: + display: edit + display_method: + display_method_options: {} + required: true + required_method: + required_method_options: {} + default_value: '' + values: + - - + - "" + - - '5' + - Five + - - '4' + - Four + - - '6' + - Six + values_method: + values_method_options: {} + options: + :sort_by: description + :sort_order: ascending + :force_multi_value: true + label: Option Dropdown + position: 1 + validator_type: + validator_rule: + reconfigurable: false + dynamic: false + show_refresh_button: false + load_values_on_init: false + read_only: false + auto_refresh: false + trigger_auto_refresh: false + visible: true + type: DialogFieldDropDownList + resource_action: + action: + resource_type: DialogField + ae_namespace: + ae_class: + ae_instance: + ae_message: + ae_attributes: {} + dialog_field_responders: [] diff --git a/spec/lib/task_helpers/imports/service_dialogs_spec.rb b/spec/lib/task_helpers/imports/service_dialogs_spec.rb new file mode 100644 index 00000000000..147d34865ab --- /dev/null +++ b/spec/lib/task_helpers/imports/service_dialogs_spec.rb @@ -0,0 +1,65 @@ +describe TaskHelpers::Imports::ServiceDialogs do + let(:data_dir) { File.join(File.expand_path(__dir__), 'data', 'service_dialogs') } + let(:dialog_file) { 'Simple_Dialog.yaml' } + let(:mod_dialog_file) { 'Simple_Dialog_modified.yml' } + let(:dialog_one_label) { 'Simple Dialog' } + let(:dialog_two_label) { 'Test Dialog' } + let(:dialog_one_desc) { 'Simple Dialog to test export and import' } + let(:dialog_tab_label) { 'Modified New tab' } + + describe "#import" do + let(:options) { {:source => source} } + + describe "when the source is a directory" do + let(:source) { data_dir } + + it 'imports all .yaml files in a specified directory' do + expect do + TaskHelpers::Imports::ServiceDialogs.new.import(options) + end.to_not output.to_stderr + assert_test_service_dialog_one_present + assert_test_service_dialog_two_present + end + end + + describe "when the source is a file" do + let(:source) { "#{data_dir}/#{dialog_file}" } + + it 'imports a specified file' do + expect do + TaskHelpers::Imports::ServiceDialogs.new.import(options) + end.to_not output.to_stderr + assert_test_service_dialog_one_present + end + end + + describe "when the source file modifies an existing file" do + let(:source) { "#{data_dir}/#{mod_dialog_file}" } + + before do + TaskHelpers::Imports::ServiceDialogs.new.import(:source => "#{data_dir}/#{dialog_file}") + end + + it 'modifies an existing service dialog' do + TaskHelpers::Imports::ServiceDialogs.new.import(options) + end + end + end + + def assert_test_service_dialog_one_present + d = Dialog.find_by(:label => dialog_one_label) + expect(d.description).to eq(dialog_one_desc) + expect(d.dialog_tabs.count).to eq(1) + end + + def assert_test_service_dialog_two_present + d = Dialog.find_by(:label => dialog_two_label) + expect(d.description).to be nil + expect(d.dialog_tabs.count).to eq(1) + end + + def assert_test_service_dialog_modified + d = Dialog.find_by(:label => dialog_one_label) + expect(d.dialog_tabs.first.label).to eq(dialog_tab_label) + end +end