Skip to content

Commit

Permalink
Added export/import of SmartState Analysis Profiles
Browse files Browse the repository at this point in the history
Fixes BZ #1344589

Based on code from https://bitbucket.org/tsinfinity/cfme-rhconsulting-scripts/src/master/rhconsulting_scanitems.rake

Will be extracted into seperate files next for import/export
  • Loading branch information
Julian Cheal committed May 16, 2018
1 parent e915a3f commit 0c54268
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/task_helpers/exports/scan_profiles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module TaskHelpers
class Exports
class ScanProfiles
def export(options)
ScanItemSet.all.each do |p|
# Skip read only entries
if p.read_only
next
end
# Also skip any entries that are using file defaults...
uses_files = false
p.members.each do |m|
if m.filename
uses_files = true
end
end
if uses_files
next
end

puts("Exporting Scan Profile: #{p.name} (#{p.description})")

pname = TaskHelpers::Exports.safe_filename(p.name, options[:keep_spaces])
fname = "ScanProfile_#{pname}.yaml"

# Clean-up data
profile = ScanItem.get_profile(p.name).first.dup
%w(id created_on updated_on).each { |k| profile.delete(k) }
profile['definition'].each do |dd|
%w(id created_on updated_on description).each { |k| dd.delete(k) }
end

s = profile.to_yaml
s.gsub!(/\n\s*guid:\s+\S+\n/,"\n")
File.write("#{options[:directory]}/#{fname}", s)
end
end
end
end
end
49 changes: 49 additions & 0 deletions lib/task_helpers/imports/scan_profiles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module TaskHelpers
class Imports
class ScanProfiles
def import(options)
Dir.glob("#{options[:source]}/ScanProfile_*yaml") do |filename|
puts("Importing Scan Profile: #{File.basename(filename, '.yaml').gsub(/^ScanProfile_/, '')} ....")

hash = YAML.load_file(filename)
items = hash["definition"]
hash.delete("definition")
profile = ScanItemSet.find_by(:name => hash["name"]);
if profile.nil?
if hash["guid"].nil?
hash["guid"] = SecureRandom.uuid
end
profile = ScanItemSet.new(hash)
else
profile.attributes = hash
end
profile.save!

# Delete existing members
profile.members.each do |one|
if one.filename
# OK, this was defined through yaml file... just skip it
next
#profile.delete(one)
else
one.destroy
end
end
items.each do |i|
if i['filename']
# OK, this rules refers to a file, just use it...
next
else
if i['guid'].nil?
i['guid'] = SecureRandom.uuid
end
#puts i.inspect
si = ScanItem.create(i)
end
profile.add_member(si)
end
end
end
end
end
end
12 changes: 12 additions & 0 deletions lib/tasks/evm_export_import.rake
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ namespace :evm do
exit # exit so that parameters to the first rake task are not run as rake tasks
end

desc 'Exports all scan profiles to individual YAML files'
task :scan_profiles => [:environment] do
options = TaskHelpers::Exports.parse_options
TaskHelpers::Exports::ScanProfiles.new.export(options)
end

desc 'Exports all tags to individual YAML files'
task :tags => :environment do
options = TaskHelpers::Exports.parse_options
Expand Down Expand Up @@ -105,6 +111,12 @@ namespace :evm do
exit # exit so that parameters to the first rake task are not run as rake tasks
end

desc 'Imports all scan profiles from individual YAML files'
task :scan_profiles => [:environment] do
options = TaskHelpers::Imports.parse_options
TaskHelpers::Imports::ScanProfiles.new.import(options)
end

desc 'Imports all tags to individual YAML files'
task :tags => :environment do
options = TaskHelpers::Imports.parse_options
Expand Down

0 comments on commit 0c54268

Please sign in to comment.