Skip to content

Commit

Permalink
Script to copy/reset available reports (for group and role):
Browse files Browse the repository at this point in the history
  - duplicate report structure from one group to another group
  - duplicate report structure from group to role, which means to each group having that role
  - remove cutomized report sructure from group
  - remove customized report structure from role, which means from each group having that role
EXAMPLE: bundle exec ruby ./tools/copy_reports_structure -s EvmGroup  -r SomeRole
         bundle exec ruby ./tools/copy_reports_structure -p SomeRole
Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1634673
  • Loading branch information
yrudman committed Oct 9, 2018
1 parent 8a9222c commit dc2495f
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions tools/copy_reports_structure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env ruby
require File.expand_path('../config/environment', __dir__)
require 'bundler/setup'
require 'trollop'

def find_group(group_name)
group = MiqGroup.where(:description => group_name).first
abort("MiqGroup '#{group_name}' not found") if group.nil?
group
end

def find_role(role_name)
role = MiqUserRole.where(:name => role_name).first
abort("MiqUserRole '#{role_name}' not found") if role.nil?
role
end

def duplicate_for_group(source_group_name, destination_group_name)
destination_group = find_group(destination_group_name)
destination_group.settings = find_group(source_group_name).settings
destination_group.save!
puts "Reports structure was succesfully cloned from '#{source_group_name}' to '#{destination_group_name}'"
rescue StandardError => e
$stderr.puts e.message
end

def duplicate_for_role(source_group_name, destination_role_name)
puts "Copying report structure from group '#{source_group_name}' to role ' #{destination_role_name}' ..."
source_group = find_group(source_group_name)
find_role(destination_role_name).miq_groups.each do |destination_group|
begin
destination_group.settings = source_group.settings
destination_group.save!
puts " Reports structure was succesfully copied from '#{source_group_name}' to '#{destination_group.description}'"
rescue StandardError => e
$stderr.puts e.message
end
end
end

def reset_for_group(group_name)
group = find_group(group_name)
group.settings = nil
group.save!
puts "Succsefully removed custom report structure for group '#{group_name}'"
rescue StandardError => e
$stderr.puts e.message
end

def reset_for_role(role_name)
puts "Removing custom report structure for role '#{role_name}'..."
find_role(role_name).miq_groups.each do |group|
begin
group.settings = nil
group.save!
puts "Succsefully removed custom report structure for group '#{group.description}'"
rescue StandardError => e
$stderr.puts e.message
end
end
end

opts = Trollop.options(ARGV) do
banner "Utility to: \n" \
" - make report structure configured for a group available to another group\n" \
" - make report structure configured for a group available to role\n" \
" - reset report access to default for group or role\n" \
"Example (Duplicate for Group): bundle exec ruby #{__FILE__} -s EvmGroup -g SomeGroup\n" \
"Example (Duplicate for Role): bundle exec ruby #{__FILE__} -s EvmGroup -r SomeRole\n" \
"Example (Reset to Default for Group): bundle exec ruby #{__FILE__} -d SomeGroup\n" \
"Example (Reset to Default for Role): bundle exec ruby #{__FILE__} -p SomeRole\n"
opt :source_group, "Source group to take report structure from", :short => "s", :type => :string
opt :group, "Destination group", :short => "g", :type => :string
opt :role, "Destination role", :short => "r", :type => :string
opt :delete_group, "Group to reset reports structure to default", :short => "d", :type => :string
opt :delete_role, "Role to reset reports structure to default", :short => "p", :type => :string
end

if opts[:source_group_given]
msg = ":source_group argument can not be used with :delete_group" if opts[:delete_group_given]
msg ||= ":source_group argument can not be used with :delete_role" if opts[:delete_role_given]
msg ||= "either :group or :role arguments requiered" unless opts[:group_given] || opts[:role_given]
abort(msg) unless msg.nil?
duplicate_for_group(opts[:source_group], opts[:group]) if opts[:group_given]
duplicate_for_role(opts[:source_group], opts[:role]) if opts[:role_given]
else
unless opts[:delete_group_given] || opts[:delete_role_given]
abort("use either :delete_group or :delete_role parameter for resetting report structure to default")
end
reset_for_group(opts[:delete_group]) if opts[:delete_group_given]
reset_for_role(opts[:delete_role]) if opts[:delete_role_given]
end

0 comments on commit dc2495f

Please sign in to comment.