Skip to content

Commit

Permalink
Add compliance information to a server
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlleDaniel committed Apr 18, 2018
1 parent cf06313 commit 94a37f5
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ def parse_physical_switch(switch)
PhysicalSwitchParser.parse_physical_switch(switch)
end

def parse_physical_server(node, rack = nil)
PhysicalServerParser.parse_physical_server(node, rack)
def parse_physical_server(node, compliance, rack = nil)
PhysicalServerParser.parse_physical_server(node, compliance, rack)
end

def parse_config_pattern(config_pattern)
ConfigPatternParser.parse_config_pattern(config_pattern)
end

def parse_compliance_policy(compliance_policies)
CompliancePolicyParser.parse_compliance_policy(compliance_policies)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module ManageIQ::Providers::Lenovo
class PhysicalInfraManager::Parser::CompliancePolicyParser < PhysicalInfraManager::Parser::ComponentParser
COMPLIANCE_NAME = 'No policy assigned'.freeze
COMPLIANCE_STATUS = {
'no' => 'Non-compliant',
'yes' => 'Compliant',
'' => 'None'
}.freeze

class << self
def parse_compliance_policy(compliance_policies)
parse_to_hash(compliance_policies)
end

private

def map_by_uuid(collection)
collection.collect do |result|
policy_name = result['policyName']
data = {
:policy_name => policy_name.empty? ? COMPLIANCE_NAME : policy_name,
:status => "#{COMPLIANCE_STATUS[result['endpointCompliant']]} #{result['message'].join(', ')}".strip,
}
# Return a tuple [key, value] for each result
[result['uuid'], data]
end.to_h
end

def parse_to_hash(persisted_results)
persisted_results.collect do |persisted_result|
if persisted_result.xITEs.present?
map_by_uuid(persisted_result.xITEs)
elsif persisted_result.racklist.present?
map_by_uuid(persisted_result.racklist)
end
end.compact.reduce({}, :merge)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class << self
#
# @return [Hash] containing the physical server information
#
def parse_physical_server(node, rack = nil)
def parse_physical_server(node, compliance, rack = nil)
result = parse(node, parent::ParserDictionaryConstants::PHYSICAL_SERVER)

# Keep track of the rack where this server is in, if it is in any rack
result[:physical_rack] = rack if rack

result[:ems_compliance_name] = compliance[:policy_name]
result[:ems_compliance_status] = compliance[:status]
result[:vendor] = "lenovo"
result[:type] = parent::ParserDictionaryConstants::MIQ_TYPES["physical_server"]
result[:power_state] = parent::ParserDictionaryConstants::POWER_STATE_MAP[node.powerStatus]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get_all_physical_infra
rack_uid, rack = @parser.parse_physical_rack(cab)

get_physical_servers(cab) do |node|
_, parsed = @parser.parse_physical_server(node, rack)
_, parsed = @parser.parse_physical_server(node, find_compliance(node), rack)
physical_servers << parsed
end

Expand All @@ -74,7 +74,7 @@ def get_all_physical_infra

nodes = get_physical_servers(standalone)
process_collection(nodes, :physical_servers) do |node|
@parser.parse_physical_server(node)
@parser.parse_physical_server(node, find_compliance(node))
end

@data[:physical_servers].concat(physical_servers)
Expand Down Expand Up @@ -120,6 +120,19 @@ def get_physical_switches
end
end

def find_compliance(node)
@compliance_policies ||= @connection.fetch_compliance_policies
@compliance_policies_parsed ||= @parser.parse_compliance_policy(@compliance_policies)
@compliance_policies_parsed[node.uuid] || create_default_compliance
end

def create_default_compliance
{
:policy_name => PhysicalInfraManager::Parser::CompliancePolicyParser::COMPLIANCE_NAME,
:status => PhysicalInfraManager::Parser::CompliancePolicyParser::COMPLIANCE_STATUS['']
}
end

def get_config_patterns
config_patterns = @connection.discover_config_pattern
process_collection(config_patterns, :customization_scripts) { |config_pattern| @parser.parse_config_pattern(config_pattern) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
VCR.insert_cassette("#{vcr_path}/mock_cabinet", options)
VCR.insert_cassette("#{vcr_path}/mock_config_patterns", options)
VCR.insert_cassette("#{vcr_path}/mock_switches", options)
VCR.insert_cassette("#{vcr_path}/mock_compliance_policy", options)
end
after(:all) do
while VCR.cassettes.last
Expand Down Expand Up @@ -147,6 +148,16 @@
end
end

it 'will retrieve compliance policy information from a physical server' do
ph1 = @result[:physical_servers][0]
ph2 = @result[:physical_servers][1]

expect(ph2[:ems_compliance_name]).to eq('No policy assigned')
expect(ph2[:ems_compliance_status]).to eq('None')
expect(ph1[:ems_compliance_name]).to eq('policy1')
expect(ph1[:ems_compliance_status]).to eq('Compliant')
end

it 'will retrieve the amout of memory in MB' do
physical_server = @result[:physical_servers][1]
memory_amount = physical_server[:computer_system][:hardware][:memory_mb]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
VCR.insert_cassette("#{vcr_path}/mock_cabinet", options)
VCR.insert_cassette("#{vcr_path}/mock_config_patterns", options)
VCR.insert_cassette("#{vcr_path}/mock_switches", options)
VCR.insert_cassette("#{vcr_path}/mock_compliance_policy", options)
VCR.insert_cassette("#{vcr_path}/full_refresh", options)
end
after(:all) do
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 94a37f5

Please sign in to comment.