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 an importing service dialogs #17241

Merged
merged 1 commit into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions lib/task_helpers/exports/service_dialogs.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions lib/task_helpers/imports/service_dialogs.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/tasks/dialogs.rake
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
23 changes: 22 additions & 1 deletion lib/tasks/evm_export_import.rake
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
47 changes: 47 additions & 0 deletions spec/lib/task_helpers/exports/service_dialogs_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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: []
Original file line number Diff line number Diff line change
@@ -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: []
Loading