-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18284 from branic/reports_export_import
Add support for exporting and importing reports
- Loading branch information
Showing
11 changed files
with
753 additions
and
0 deletions.
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,16 @@ | ||
module TaskHelpers | ||
class Exports | ||
class Reports | ||
def export(options = {}) | ||
export_dir = options[:directory] | ||
|
||
custom_reports = options[:all] ? MiqReport.all : MiqReport.where(:rpt_type => "Custom") | ||
|
||
custom_reports.each do |report| | ||
filename = Exports.safe_filename(report.name, options[:keep_spaces]) | ||
File.write("#{export_dir}/#{filename}.yaml", report.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
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,26 @@ | ||
module TaskHelpers | ||
class Imports | ||
class Reports | ||
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 Reports from: #{filename}") | ||
|
||
report_options = { :userid => 'admin', | ||
:overwrite => options[:overwrite], | ||
:save => true } | ||
|
||
begin | ||
report_fd = File.open(filename, 'r') | ||
MiqReport.import(report_fd, report_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
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,67 @@ | ||
describe TaskHelpers::Exports::Reports do | ||
let(:export_dir) do | ||
Dir.mktmpdir('miq_exp_dir') | ||
end | ||
|
||
before do | ||
FactoryBot.create(:miq_report, | ||
:name => "Test Report", | ||
:rpt_type => "Custom", | ||
:tz => "Eastern Time (US & Canada)", | ||
:col_order => %w(name boot_time disks_aligned), | ||
:cols => %w(name boot_time disks_aligned), | ||
:db_options => { :rpt_type => "ChargebackContainerProject" }, | ||
"include" => { "columns" => %w(col1 col2) }) | ||
FactoryBot.create(:miq_report, | ||
:name => "Test Report 2", | ||
:rpt_type => "Custom", | ||
:tz => "Eastern Time (US & Canada)", | ||
:col_order => %w(name boot_time disks_aligned), | ||
:cols => %w(name boot_time disks_aligned), | ||
:db_options => { :rpt_type => "ChargebackContainerProject" }, | ||
"include" => { "columns" => %w(col1 col2) }) | ||
FactoryBot.create(:miq_report, | ||
:name => "Default Test Report", | ||
:rpt_type => "Default", | ||
:tz => "Eastern Time (US & Canada)", | ||
:col_order => %w(name boot_time disks_aligned), | ||
:cols => %w(name boot_time disks_aligned), | ||
:db_options => { :rpt_type => "ChargebackContainerProject" }, | ||
"include" => { "columns" => %w(col1 col2) }) | ||
end | ||
|
||
after do | ||
FileUtils.remove_entry export_dir | ||
end | ||
|
||
describe "when --all is not specified" do | ||
let(:report_filename1) { "#{export_dir}/Test_Report.yaml" } | ||
let(:report_filename2) { "#{export_dir}/Test_Report_2.yaml" } | ||
|
||
it "exports custom reports to individual files in a given directory" do | ||
TaskHelpers::Exports::Reports.new.export(:directory => export_dir) | ||
expect(Dir[File.join(export_dir, '**', '*')].count { |file| File.file?(file) }).to eq(2) | ||
report1 = YAML.load_file(report_filename1) | ||
expect(report1.first["MiqReport"]["menu_name"]).to eq("Test Report") | ||
report2 = YAML.load_file(report_filename2) | ||
expect(report2.first["MiqReport"]["menu_name"]).to eq("Test Report 2") | ||
end | ||
end | ||
|
||
describe "when --all is specified" do | ||
let(:report_filename1) { "#{export_dir}/Test_Report.yaml" } | ||
let(:report_filename2) { "#{export_dir}/Test_Report_2.yaml" } | ||
let(:report_filename3) { "#{export_dir}/Default_Test_Report.yaml" } | ||
|
||
it "exports all reports to individual files in a given directory" do | ||
TaskHelpers::Exports::Reports.new.export(:directory => export_dir, :all => true) | ||
expect(Dir[File.join(export_dir, '**', '*')].count { |file| File.file?(file) }).to eq(3) | ||
report1 = YAML.load_file(report_filename1) | ||
expect(report1.first["MiqReport"]["menu_name"]).to eq("Test Report") | ||
report2 = YAML.load_file(report_filename2) | ||
expect(report2.first["MiqReport"]["menu_name"]).to eq("Test Report 2") | ||
report3 = YAML.load_file(report_filename3) | ||
expect(report3.first["MiqReport"]["menu_name"]).to eq("Default Test Report") | ||
end | ||
end | ||
end |
86 changes: 86 additions & 0 deletions
86
spec/lib/task_helpers/imports/data/reports/Test_Report.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,86 @@ | ||
--- | ||
- MiqReport: | ||
title: Test Report for Exporting | ||
rpt_group: Custom | ||
rpt_type: Custom | ||
priority: | ||
db: Vm | ||
cols: | ||
- name | ||
- ipaddresses | ||
- os_image_name | ||
- num_cpu | ||
- cpu_cores_per_socket | ||
- cpu_total_cores | ||
- mem_cpu | ||
- evm_owner_name | ||
- evm_owner_email | ||
include: {} | ||
col_order: | ||
- name | ||
- ipaddresses | ||
- os_image_name | ||
- num_cpu | ||
- cpu_cores_per_socket | ||
- cpu_total_cores | ||
- mem_cpu | ||
- evm_owner_name | ||
- evm_owner_email | ||
headers: | ||
- Name | ||
- IP Addresses | ||
- OS Name | ||
- Number of CPUs | ||
- Cpu Cores Per Socket | ||
- Number of CPU Cores | ||
- Memory | ||
- Evm Owner Name | ||
- Evm Owner Email | ||
conditions: !ruby/object:MiqExpression | ||
exp: | ||
CONTAINS: | ||
tag: Vm.managed-department | ||
value: engineering | ||
context_type: | ||
order: Ascending | ||
sortby: | ||
- evm_owner_email | ||
- name | ||
group: y | ||
graph: | ||
dims: | ||
filename: | ||
file_mtime: | ||
categories: [] | ||
timeline: | ||
template_type: report | ||
where_clause: | ||
db_options: {} | ||
generate_cols: | ||
generate_rows: | ||
col_formats: | ||
- | ||
- | ||
- | ||
- | ||
- | ||
- | ||
- | ||
- | ||
- | ||
tz: | ||
time_profile_id: | ||
display_filter: | ||
col_options: | ||
evm_owner_email: | ||
:break_label: 'Evm Owner Email: ' | ||
:break_format: :model_name | ||
rpt_options: | ||
:pdf: | ||
:page_size: US Letter - 8.5in x 11.0in | ||
:queue_timeout: | ||
:summary: | ||
:hide_detail_rows: false | ||
miq_group_id: 2 | ||
user_id: 1 | ||
menu_name: Test Report |
Oops, something went wrong.