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 vlan info in parser #135

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require_relative 'component_parser'

module ManageIQ::Providers::Lenovo
module Parsers
class FirmwareParser < ComponentParser
class << self
#
# Parses a firmware into a Hash
#
# @param [Hash] firmware - object containing details for the firmware
#
# @return [Hash] the firmware as required by the application
#
def parse_firmware(firmware)
{
:name => "#{firmware["role"]} #{firmware["name"]}-#{firmware["status"]}".strip,
:build => firmware["build"],
:version => firmware["version"],
:release_date => firmware["date"],
}
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,14 @@ def get_total_cores(node)
end

def get_firmwares(node)
node.firmware&.map { |firmware| parse_firmware(firmware) }
node.firmware&.map { |firmware| FirmwareParser.parse_firmware(firmware) }
end

def get_guest_devices(node)
guest_devices = get_addin_cards(node)
guest_devices << parse_management_device(node)
end

def parse_firmware(firmware)
{
:name => "#{firmware["role"]} #{firmware["name"]}-#{firmware["status"]}",
:build => firmware["build"],
:version => firmware["version"],
:release_date => firmware["date"],
}
end

def get_addin_cards(node)
parsed_addin_cards = []
# For each of the node's addin cards, parse the addin card and then see
Expand Down Expand Up @@ -168,7 +159,7 @@ def get_guest_device_firmware(card)
firmware = card["firmware"]
unless firmware.nil?
device_fw = firmware.map do |fw|
parse_firmware(fw)
FirmwareParser.parse_firmware(fw)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require_relative 'component_parser'

module ManageIQ::Providers::Lenovo
module Parsers
class SwitchParser < ComponentParser
class << self
#
# Parses a switch into a Hash
#
# @param [XClarityClient::Switch] switch - object containing details for the switch
#
# @return [Hash] the switch data as required by the application
#
def parse_switch(switch)
result = parse(switch, ParserDictionaryConstants::SWITCH)

result[:type] = ParserDictionaryConstants::MIQ_TYPES["switch"]
result[:hardware] = get_hardwares(switch)

return switch.uuid, result
end

private

def get_hardwares(node)
{
:firmwares => get_firmwares(node),
:guest_devices => get_ports(node),
:networks => get_networks(node)
}
end

def get_ports(node)
node.ports&.map { |port| parse_port(port) }
end

def get_networks(node)
get_parsed_switch_ip_interfaces_by_key(node.ipInterfaces, 'IPv4assignments', node.ipv4Addresses, false) + get_parsed_switch_ip_interfaces_by_key(node.ipInterfaces, 'IPv6assignments', node.ipv6Addresses, true)
end

def get_parsed_switch_ip_interfaces_by_key(ip_interfaces, key, address_list, is_ipv6 = false)
ip_interfaces&.flat_map { |interface| interface[key] }
.select { |assignment| address_list.include?(assignment['address']) }
.map { |assignment| parse_network(assignment, is_ipv6) }
end

def parse_network(assignment, is_ipv6 = false)
result = parse(assignment, ParserDictionaryConstants::SWITCH_NETWORK)

result[:ipaddress] = assignment['address'] unless is_ipv6
result[:ipv6address] = assignment['address'] if is_ipv6

result
end

def parse_port(port)
{
:device_name => port["portName"].presence || port["port"],
:device_type => "physical_port",
:peer_mac_address => port["peerMacAddress"].presence,
:vlan_key => port["PVID"].presence,
:vlan_enabled => port["PVID"].present?
}
end

def get_firmwares(node)
node.firmware&.map { |firmware| FirmwareParser.parse_firmware(firmware) }
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def self.get_instance(version)
parser.new
end

def parse_switch(node)
Parsers::SwitchParser.parse_switch(node)
end

def parse_physical_server(node)
Parsers::PhysicalServerParser.parse_physical_server(node)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ParserDictionaryConstants

MIQ_TYPES = {
"physical_server" => "ManageIQ::Providers::Lenovo::PhysicalInfraManager::PhysicalServer",
"switch" => "ManageIQ::Providers::Lenovo::PhysicalInfraManager::Switch",
"template" => "ManageIQ::Providers::Lenovo::PhysicalInfraManager::Template",
}.freeze

Expand All @@ -38,6 +39,24 @@ class ParserDictionaryConstants
# and your values (string) corresponds to the attributes of the
# source object who the value will be set as value of the key of result hash.
# see +ManageIQ::Providers::Lenovo::Parse#parse+
SWITCH = {
:name => 'name',
:uid_ems => 'uuid',
:switch_uuid => 'uuid',
:health_state => 'overallHealthState',
:power_state => 'powerState',
:product_name => 'productName',
:part_number => 'partNumber',
:serial_number => 'serialNumber',
:description => 'description',
:manufacturer => 'manufacturer',
}.freeze

SWITCH_NETWORK = {
:subnet_mask => 'subnet',
:default_gateway => 'gateway',
}.freeze

PHYSICAL_SERVER = {
:name => 'name',
:ems_ref => 'uuid',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ module Parsers
end
end

require_relative 'components/firmware_parser'
require_relative 'components/switch_parser'
require_relative 'components/physical_server_parser'
require_relative 'components/config_pattern_parser'
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def ems_inv_to_hashes

get_physical_servers
discover_ip_physical_infra
get_switches
get_config_patterns

$log.info("#{log_header}...Complete")
Expand All @@ -56,6 +57,12 @@ def get_physical_servers
process_collection(nodes, :physical_servers) { |node| @parser.parse_physical_server(node) }
end

def get_switches
@all_switches ||= @connection.discover_switches

process_collection(@all_switches, :switches) { |switch| @parser.parse_switch(switch) }
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
@@ -0,0 +1,6 @@
module ManageIQ::Providers
class Lenovo::PhysicalInfraManager::Switch < ::Switch
belongs_to :ext_management_system, :foreign_key => :ems_id, :inverse_of => :switches,
:class_name => "ManageIQ::Providers::PhysicalInfraManager"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
VCR.insert_cassette("#{vcr_path}/mock_aicc", options)
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)
end
after(:all) do
while VCR.cassettes.last
Expand Down Expand Up @@ -36,6 +37,57 @@
expect(described_class.miq_template_type).to eq("ManageIQ::Providers::Lenovo::PhysicalInfraManager::Template")
end

context "parse switches" do
before do
@result = ems_inv_to_hashes
end

it 'will retrieve switches' do
expect(@result[:switches].size).to eq(1)
end

it 'will retrieve switch details from switches' do
switch = @result[:switches].first

expect(switch[:name]).to eq("ThinkAgile-VX-NE1032-SW03")
expect(switch[:uid_ems]).to eq("00000000000010008000A48CDB984B00")
expect(switch[:switch_uuid]).to eq("00000000000010008000A48CDB984B00")
expect(switch[:health_state]).to eq("Non-Critical")
expect(switch[:power_state]).to eq("On")
expect(switch[:product_name]).to eq("Lenovo ThinkSystem NE1032 RackSwitch")
end

it 'will retrieve a port from switches' do
switch = @result[:switches].first
port = switch[:hardware][:guest_devices].first

expect(port[:peer_mac_address]).to eq("7c:d3:0a:e6:47:51")
expect(port[:device_type]).to eq("physical_port")
expect(port[:vlan_enabled]).to eq(true)
expect(port[:vlan_key]).to eq("\"Lenovo-Network-VLAN546\"")
end

it 'will retrieve a firmware from switches' do
switch = @result[:switches].first
firmware = switch[:hardware][:firmwares].first

expect(firmware[:name]).to eq("Uboot-N/A")
expect(firmware[:version]).to eq("10.4.2.0")
end

it 'will retrieve network details from switches' do
switch = @result[:switches].first
hardware = switch[:hardware]
network_ipv4 = hardware[:networks].first
network_ipv6 = hardware[:networks].second

expect(network_ipv4[:subnet_mask]).to eq("127.0.0.1")
expect(network_ipv4[:default_gateway]).to eq("0.0.0.0")
expect(network_ipv4[:ipaddress]).to eq("10.243.4.79")
expect(network_ipv6[:ipv6address]).to eq("fe80:0:0:0:a68c:dbff:fe98:4b00")
end
end

context 'parse physical servers' do
before do
@result = ems_inv_to_hashes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
VCR.insert_cassette("#{vcr_path}/mock_aicc", options)
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}/full_refresh", options)
end
after(:all) do
Expand Down
Loading