Skip to content

Commit

Permalink
Script to duplicate report structure from one group to another group …
Browse files Browse the repository at this point in the history
…or to role.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1634673

remove garbage

added coping from group to role
  • Loading branch information
yrudman committed Oct 5, 2018
1 parent ead55ec commit f19b4fb
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions tools/copy_reports_structure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/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
raise "MiqGroup '#{group_name}' not found" if group.nil?
group
end

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


def duplicate_for_role(source_group_name, destination_role_name)
puts "Starting to copy report structure from group '#{source_group_name}' to role ' #{destination_role_name}'"
source_group = find_group(source_group_name)
destination_role = MiqUserRole.where(:name => destination_role_name)
raise "MiqUserRole '#{destination_role_name}' not found" if destination_role.nil?
destination_role.miq_groups.each do |destination_group|
begin
puts "Copying report structure from '#{source_group_name}' group to ' #{destination_group.description}'"
destination_group.settings = source_group.settings
destination_group.save
puts "Reports structure was succesfully copied from '#{source_group_name}' to '#{destination_group.description}'"
rescue => 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 (Default for Group): bundle exec ruby #{__FILE__} -d SomeGroup\n" \
"Example (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 :default_group, "Group to reset reports structure to default", :short => "d", :type => :string
opt :default_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 :default_group" if opts[:default_group_given]
msg ||= ":source_group argument can not be used with :default_role" if opts[:default_role_given]
msg ||= "either :group or :role arguments requiered" unless opts[:group_given] || opts[:role_given]
Trollop.die(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

end


def reset_for_group(group)

end

def reset_for_role(role)

end


0 comments on commit f19b4fb

Please sign in to comment.