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 reports #18284

Merged
merged 1 commit into from
Dec 20, 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/reports.rb
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
1 change: 1 addition & 0 deletions lib/task_helpers/imports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def self.parse_options
require 'trollop'
options = Trollop.options(EvmRakeHelper.extract_command_options) do
opt :source, 'Directory or file to import from', :type => :string, :required => true
opt :overwrite, 'Overwrite existing object', :type => :boolean, :default => true
end

error = validate_source(options[:source])
Expand Down
26 changes: 26 additions & 0 deletions lib/task_helpers/imports/reports.rb
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
17 changes: 17 additions & 0 deletions lib/tasks/evm_export_import.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# * Custom Buttons
# * SmartState Analysis Scan Profiles
# * Customization Templates
# * Reports

namespace :evm do
namespace :export do
Expand Down Expand Up @@ -96,6 +97,14 @@ namespace :evm do

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

desc 'Exports all reports to individual YAML files'
task :reports => :environment do
options = TaskHelpers::Exports.parse_options
TaskHelpers::Exports::Reports.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 @@ -184,5 +193,13 @@ namespace :evm do

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

desc 'Imports all reports from individual YAML files'
task :reports => :environment do
options = TaskHelpers::Imports.parse_options
TaskHelpers::Imports::Reports.new.import(options)

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