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 rake script to export/import policies and policy profiles #15256

Merged
merged 5 commits into from
Dec 19, 2017
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
1 change: 1 addition & 0 deletions lib/task_helpers/exports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def self.parse_options
options = Trollop.options(EvmRakeHelper.extract_command_options) do
opt :keep_spaces, 'Keep spaces in filenames', :type => :boolean, :short => 's', :default => false
opt :directory, 'Directory to place exported files in', :type => :string, :required => true
opt :all, 'Export read-only objects', :type => :boolean, :default => false
end

error = validate_directory(options[:directory])
Expand Down
20 changes: 20 additions & 0 deletions lib/task_helpers/exports/policies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module TaskHelpers
class Exports
class Policies
def export(options = {})
export_dir = options[:directory]

policies = if options[:all]
MiqPolicy.order(:id).all
else
MiqPolicy.order(:id).where(:read_only => [false, nil])
end

policies.each do |p|
fname = Exports.safe_filename(p.description, options[:keep_spaces])
File.write("#{export_dir}/#{fname}.yaml", p.export_to_yaml)
end
end
end
end
end
20 changes: 20 additions & 0 deletions lib/task_helpers/exports/policy_sets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module TaskHelpers
class Exports
class PolicySets
def export(options = {})
export_dir = options[:directory]

policy_sets = if options[:all]
MiqPolicySet.order(:id).all
else
MiqPolicySet.order(:id).where(:read_only => [false, nil])
end

policy_sets.each do |p|
fname = Exports.safe_filename(p.description, options[:keep_spaces])
File.write("#{export_dir}/#{fname}.yaml", p.export_to_yaml)
end
end
end
end
end
29 changes: 29 additions & 0 deletions lib/task_helpers/imports/policies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module TaskHelpers
class Imports
class Policies
def import(options = {})
return unless options[:source]

glob = File.file?(options[:source]) ? options[:source] : "#{options[:source]}/*.yaml"
Dir.glob(glob) do |fname|
begin
policies = YAML.load_file(fname)
import_policies(policies)
rescue => e
$stderr.puts "Error importing #{fname} : #{e.message}"
end
end
end

private

def import_policies(policies)
MiqPolicy.transaction do
policies.each do |policy|
MiqPolicy.import_from_hash(policy['MiqPolicy'], :save => true)
end
end
end
end
end
end
29 changes: 29 additions & 0 deletions lib/task_helpers/imports/policy_sets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module TaskHelpers
class Imports
class PolicySets
def import(options = {})
return unless options[:source]

glob = File.file?(options[:source]) ? options[:source] : "#{options[:source]}/*.yaml"
Dir.glob(glob) do |fname|
begin
policysets = YAML.load_file(fname)
import_policysets(policysets)
rescue => e
$stderr.puts "Error importing #{fname} : #{e.message}"
end
end
end

private

def import_policysets(policysets)
MiqPolicySet.transaction do
policysets.each do |policyset|
MiqPolicySet.import_from_hash(policyset['MiqPolicySet'], :save => true)
end
end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/tasks/evm_export_import.rake
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ namespace :evm do

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

desc 'Exports all policies to individual YAML files'
task :policies => :environment do
options = TaskHelpers::Exports.parse_options
TaskHelpers::Exports::Policies.new.export(options)

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

desc 'Exports all policy profiles to individual YAML files'
task :policyprofiles => :environment do
options = TaskHelpers::Exports.parse_options
TaskHelpers::Exports::PolicySets.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 All @@ -35,5 +51,21 @@ namespace :evm do

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

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

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

desc 'Imports all policy profiles from individual YAML files'
task :policyprofiles => :environment do
options = TaskHelpers::Imports.parse_options
TaskHelpers::Imports::PolicySets.new.import(options)

exit # exit so that parameters to the first rake task are not run as rake tasks
end
end
end
4 changes: 4 additions & 0 deletions spec/factories/miq_policy_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
factory :miq_policy_set do
description "Test Policy Set"
end

factory :miq_policy_set_read_only, :parent => :miq_policy_set do
read_only true
end
end
94 changes: 94 additions & 0 deletions spec/lib/task_helpers/exports/policies_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
describe TaskHelpers::Exports::Policies do
let(:guid) { "a61314d5-67bd-435f-9c82-b82226e0a7fe" }
let(:guid2) { "ac7e2972-f2b2-4ebe-b29d-97eefaac7615" }

let(:profile_export_attrs) do
[
{
"MiqPolicy" => {
"name" => "a61314d5-67bd-435f-9c82-b82226e0a7fe",
"description" => "Test Compliance Policy",
"expression" => nil,
"towhat" => "Vm",
"guid" => "a61314d5-67bd-435f-9c82-b82226e0a7fe",
"created_by" => nil,
"updated_by" => nil,
"notes" => nil,
"active" => true,
"mode" => "compliance",
"read_only" => nil,
"MiqPolicyContent" => [],
"Condition" => []
}
}
]
end

let(:policy_create_attrs) do
{
:description => "Test Compliance Policy",
:name => guid,
:guid => guid,
:mode => "compliance",
}
end

let(:profile2_export_attrs) do
[
{
"MiqPolicy" => {
"name" => "ac7e2972-f2b2-4ebe-b29d-97eefaac7615",
"description" => "Test Compliance Policy 2",
"expression" => nil,
"towhat" => "Host",
"guid" => "ac7e2972-f2b2-4ebe-b29d-97eefaac7615",
"created_by" => nil,
"updated_by" => nil,
"notes" => nil,
"active" => true,
"mode" => "control",
"read_only" => true,
"MiqPolicyContent" => [],
"Condition" => []
}
}
]
end

let(:policy2_create_attrs) do
{
:description => "Test Compliance Policy 2",
:name => guid2,
:guid => guid2,
:towhat => "Host"
}
end

let(:export_dir) do
Dir.mktmpdir('miq_exp_dir')
end

before do
FactoryGirl.create(:miq_policy, policy_create_attrs)
FactoryGirl.create(:miq_policy_read_only, policy2_create_attrs)
end

after do
FileUtils.remove_entry export_dir
end

it 'exports user policies to a given directory' do
TaskHelpers::Exports::Policies.new.export(:directory => export_dir)
file_contents = File.read("#{export_dir}/Test_Compliance_Policy.yaml")
expect(YAML.safe_load(file_contents)).to eq(profile_export_attrs)
expect(Dir[File.join(export_dir, '**', '*')].count { |file| File.file?(file) }).to eq(1)
end

it 'exports all policies to a given directory' do
TaskHelpers::Exports::Policies.new.export(:directory => export_dir, :all => true)
file_contents = File.read("#{export_dir}/Test_Compliance_Policy.yaml")
file_contents2 = File.read("#{export_dir}/Test_Compliance_Policy_2.yaml")
expect(YAML.safe_load(file_contents)).to eq(profile_export_attrs)
expect(YAML.safe_load(file_contents2)).to eq(profile2_export_attrs)
end
end
87 changes: 87 additions & 0 deletions spec/lib/task_helpers/exports/policy_sets_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
describe TaskHelpers::Exports::PolicySets do
let(:guid) { "a3734dcc-e25d-4164-ba95-1114568d491a" }
let(:guid2) { "328593c3-a0a2-4a31-8d58-1a3eeef0ce95" }

let(:policy_set_export_attrs) do
[
{
"MiqPolicySet" => {
"name" => "a3734dcc-e25d-4164-ba95-1114568d491a",
"description" => "Policy Set Export Test",
"set_type" => "MiqPolicySet",
"guid" => "a3734dcc-e25d-4164-ba95-1114568d491a",
"read_only" => nil,
"set_data" => nil,
"mode" => nil,
"owner_type" => nil,
"owner_id" => nil,
"userid" => nil,
"group_id" => nil,
"MiqPolicy" => []
}
}
]
end

let(:policy_set_create_attrs) do
{
:description => "Policy Set Export Test",
:guid => guid,
:name => guid
}
end

let(:policy2_set_export_attrs) do
[
{
"MiqPolicySet" => {
"name" => "328593c3-a0a2-4a31-8d58-1a3eeef0ce95",
"description" => "Policy Set Export Test 2",
"set_type" => "MiqPolicySet",
"guid" => "328593c3-a0a2-4a31-8d58-1a3eeef0ce95",
"read_only" => true,
"set_data" => nil,
"mode" => nil,
"owner_type" => nil,
"owner_id" => nil,
"userid" => nil,
"group_id" => nil,
"MiqPolicy" => []
}
}
]
end

let(:policy2_set_create_attrs) do
{
:description => "Policy Set Export Test 2",
:guid => guid2,
:name => guid2
}
end

let(:export_dir) do
Dir.mktmpdir('miq_exp_dir')
end

before do
FactoryGirl.create(:miq_policy_set, policy_set_create_attrs)
FactoryGirl.create(:miq_policy_set_read_only, policy2_set_create_attrs)
end

after do
FileUtils.remove_entry export_dir
end

it 'exports user policy sets to a given directory' do
TaskHelpers::Exports::PolicySets.new.export(:directory => export_dir)
file_contents = File.read("#{export_dir}/Policy_Set_Export_Test.yaml")
expect(YAML.safe_load(file_contents)).to eq(policy_set_export_attrs)
end

it 'exports all policy sets to a given directory' do
TaskHelpers::Exports::PolicySets.new.export(:directory => export_dir, :all => true)
file_contents = File.read("#{export_dir}/Policy_Set_Export_Test_2.yaml")
expect(YAML.safe_load(file_contents)).to eq(policy2_set_export_attrs)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
- MiqPolicy:
name: 7562ca69-a00d-4017-be8f-d31d39a07deb
description:
expression: !ruby/object:MiqExpression
exp:
"=":
field: Vm-cpu_hot_add_enabled
value: 'false'
context_type:
towhat: Vm
guid: 7562ca69-a00d-4017-be8f-d31d39a07deb
created_by: admin
updated_by: admin
notes:
active: true
mode: compliance
read_only:
MiqPolicyContent:
- qualifier: failure
failure_sequence: 1
failure_synchronous: true
MiqEventDefinition:
name: vm_compliance_check
description: VM Compliance Check
guid: dc3c6494-1a5e-11e7-9bb4-02426c6b2651
event_type: Default
definition:
default:
enabled:
MiqAction:
name: compliance_failed
description: Mark as Non-Compliant
guid: db553dc6-1a5e-11e7-9bb4-02426c6b2651
action_type: default
options: {}
Condition: []
Loading