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

Script to copy reports access from group to role #18066

Merged
Merged
Changes from 1 commit
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
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"
yrudman marked this conversation as resolved.
Show resolved Hide resolved
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
yrudman marked this conversation as resolved.
Show resolved Hide resolved
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