Skip to content

Commit

Permalink
Merge pull request #236 from agrare/add_host_switch_parser
Browse files Browse the repository at this point in the history
Parse Switches for Streaming Refresh
  • Loading branch information
Ladas authored May 3, 2018
2 parents 4a18e8f + f30c441 commit 452740e
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ module ManageIQ::Providers::Vmware::InfraManager::Inventory::Collector::Property
:DistributedVirtualPortgroup => [
"summary.name",
"config.key",
"config.defaultPortConfig",
"config.distributedVirtualSwitch",
"config.name",
"parent",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ManageIQ::Providers::Vmware::InfraManager::Inventory::Parser
include_concern :ComputeResource
include_concern :Datacenter
include_concern :Datastore
include_concern :DistributedVirtualSwitch
include_concern :Folder
include_concern :HostSystem
include_concern :ResourcePool
Expand Down Expand Up @@ -79,12 +80,20 @@ def parse_distributed_virtual_switch(object, props)
persister.switches.manager_uuids << object._ref
return if props.nil?

type = ManageIQ::Providers::Vmware::InfraManager::DistributedVirtualSwitch.name

switch_hash = {
:uid_ems => object._ref,
:type => type,
:shared => true,
}

persister.switches.build(switch_hash)
parse_dvs_config(switch_hash, props[:config])
parse_dvs_summary(switch_hash, props[:summary])

persister_switch = persister.switches.build(switch_hash)

parser_dvs_hosts(persister_switch, props)
end
alias parse_vmware_distributed_virtual_switch parse_distributed_virtual_switch

Expand Down Expand Up @@ -136,7 +145,37 @@ def parse_host_system(object, props)

def parse_network(object, props)
end
alias parse_distributed_virtual_portgroup parse_network

def parse_distributed_virtual_portgroup(object, props)
persister.lans.manager_uuids << object._ref
return if props.nil?

name = props.fetch_path(:summary, :name) || props.fetch_path(:config, :name)
name = CGI.unescape(name) unless name.nil?

default_port_config = props.fetch_path(:config, :defaultPortConfig)
security_policy = default_port_config&.securityPolicy

if security_policy
allow_promiscuous = security_policy.allowPromiscuous&.value
forged_transmits = security_policy.forgedTransmits&.value
mac_changes = security_policy.macChanges&.value
end

dvs = props.fetch_path(:config, :distributedVirtualSwitch)
switch = persister.switches.lazy_find(dvs._ref) unless dvs.nil?

lan_hash = {
:uid_ems => object._ref,
:name => name,
:switch => switch,
:allow_promiscuous => allow_promiscuous,
:forged_transmits => forged_transmits,
:mac_changes => mac_changes,
}

persister.lans.build(lan_hash)
end

def parse_resource_pool(object, props)
persister.resource_pools.manager_uuids << object._ref
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class ManageIQ::Providers::Vmware::InfraManager::Inventory::Parser
module DistributedVirtualSwitch
def parse_dvs_config(dvs_hash, config)
return if config.nil?

dvs_hash[:name] ||= CGI.unparse(config[:name]) if config[:name].present?
dvs_hash[:ports] = config[:numPorts] || 0
dvs_hash[:switch_uuid] ||= config[:uuid]

parse_dvs_security_policy(dvs_hash, config[:defaultPortConfig].securityPolicy)
end

def parse_dvs_summary(dvs_hash, summary)
return if summary.nil?

dvs_hash[:name] ||= CGI.unescape(summary[:name]) if summary[:name].present?
dvs_hash[:switch_uuid] ||= summary[:uuid]
end

def parse_dvs_security_policy(dvs_hash, security_policy)
return if security_policy.nil?

dvs_hash[:allow_promiscuous] = security_policy.allowPromiscuous&.value
dvs_hash[:forged_transmits] = security_policy.forgedTransmits&.value
dvs_hash[:mac_changes] = security_policy.macChanges&.value
end

def parser_dvs_hosts(switch, props)
hosts = props.fetch_path(:summary, :hostMember)
return if hosts.nil?

hosts.each do |host|
persister.host_switches.build(
:host => persister.hosts.lazy_find(host._ref),
:switch => switch
)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def parse_host_system_guest_devices(hardware, props)
:present => true,
:controller_type => 'ethernet',
:address => pnic.mac,
:switch => persister.switches.lazy_find(pnic.key)
# TODO: :switch => persister.switches.lazy_find(pnic.device)
)
end

Expand Down Expand Up @@ -221,7 +221,33 @@ def parse_host_system_guest_devices(hardware, props)
end

def parse_host_system_switches(host, props)
# TODO
network = props.fetch_path(:config, :network)
return if network.blank?

type = ManageIQ::Providers::Vmware::InfraManager::HostVirtualSwitch.name

switches = network[:vswitch]
switches.to_a.each do |switch|
security_policy = switch.spec&.policy&.security
if security_policy
allow_promiscuous = security_policy[:allowPromiscuous]
forged_transmits = security_policy[:forgedTransmits]
mac_changes = security_policy[:macChanges]
end

persister_switch = persister.switches.build(
:uid_ems => switch[:name],
:name => switch[:name],
:type => type,
:ports => switch[:numPorts],
:mtu => switch[:mtu],
:allow_promiscuous => allow_promiscuous,
:forged_transmits => forged_transmits,
:mac_changes => mac_changes,
)

persister.host_switches.build(:host => host, :switch => persister_switch)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
assert_table_counts
assert_specific_datacenter
assert_specific_folder
assert_specific_host
assert_specific_cluster
assert_specific_resource_pool
assert_specific_switch
assert_specific_lan
assert_specific_dvswitch
assert_specific_dvportgroup
assert_specific_vm
end
end
Expand Down Expand Up @@ -62,9 +63,9 @@
it "migrate a virtual machine" do
vm = ems.vms.find_by(:ems_ref => 'vm-107')

expect(vm.host.ems_ref).to eq("host-89")
expect(vm.host.ems_ref).to eq("host-93")
run_targeted_refresh(targeted_update_set(vm_migrate_object_update))
expect(vm.reload.host.ems_ref).to eq("host-13")
expect(vm.reload.host.ems_ref).to eq("host-94")
end

def run_targeted_refresh(update_set)
Expand Down Expand Up @@ -107,7 +108,7 @@ def vm_migrate_object_update
:kind => "modify",
:obj => RbVmomi::VIM.VirtualMachine(vim, "vm-107"),
:changeSet => [
RbVmomi::VIM.PropertyChange(:name => "summary.runtime.host", :op => "assign", :val => RbVmomi::VIM.HostSystem(vim, "host-13")),
RbVmomi::VIM.PropertyChange(:name => "summary.runtime.host", :op => "assign", :val => RbVmomi::VIM.HostSystem(vim, "host-94")),
],
:missingSet => [],
)
Expand All @@ -134,6 +135,8 @@ def assert_table_counts
expect(ems.resource_pools.count).to eq(72)
expect(ems.storages.count).to eq(1)
expect(ems.vms_and_templates.count).to eq(512)
expect(ems.switches.count).to eq(5)
expect(ems.lans.count).to eq(12)
end

def assert_specific_datacenter
Expand Down Expand Up @@ -163,37 +166,68 @@ def assert_specific_folder
# TODO: check relationships
end

def assert_specific_host
host = ems.hosts.find_by(:ems_ref => "host-14")

expect(host).not_to be_nil

switch = host.switches.find_by(:uid_ems => "vSwitch0")

expect(switch).not_to be_nil
expect(switch).to have_attributes(
:name => "vSwitch0",
:uid_ems => "vSwitch0",
:ports => 64,
:allow_promiscuous => false,
:forged_transmits => true,
:mac_changes => true,
:mtu => 1500,
:type => "ManageIQ::Providers::Vmware::InfraManager::HostVirtualSwitch",
)

vnic = host.hardware.guest_devices.find_by(:uid_ems => "vmnic0")
expect(vnic).not_to be_nil
expect(vnic).to have_attributes(
:device_name => "vmnic0",
:device_type => "ethernet",
:location => "03:00.0",
:controller_type => "ethernet",
:uid_ems => "vmnic0",
# TODO: :switch => switch,
)
end

def assert_specific_cluster
cluster = ems.ems_clusters.find_by(:ems_ref => "domain-c87")
cluster = ems.ems_clusters.find_by(:ems_ref => "domain-c12")

expect(cluster).not_to be_nil
expect(cluster).to have_attributes(
:drs_automation_level => "manual",
:drs_enabled => true,
:drs_migration_threshold => 3,
:effective_cpu => 47_992,
:effective_memory => 68_698_505_216,
:ems_ref => "domain-c87",
:effective_cpu => 47_984,
:effective_memory => 62_780_342_272,
:ems_ref => "domain-c12",
:ha_admit_control => true,
:ha_enabled => false,
:ha_max_failures => 1,
:name => "DC0_C1",
:uid_ems => "domain-c87",
:name => "DC0_C0",
:uid_ems => "domain-c12",
)
end

def assert_specific_resource_pool
resource_pool = ems.resource_pools.find_by(:ems_ref => "resgroup-88")
resource_pool = ems.resource_pools.find_by(:ems_ref => "resgroup-92")

expect(resource_pool).not_to be_nil
expect(resource_pool).to have_attributes(
:cpu_limit => 47_992,
:cpu_reserve => 47_992,
:cpu_limit => 47_984,
:cpu_reserve => 47_984,
:cpu_reserve_expand => true,
:cpu_shares => 4_000,
:cpu_shares_level => nil,
:memory_limit => 65_516,
:memory_reserve => 65_516,
:memory_limit => 59_872,
:memory_reserve => 59_872,
:memory_reserve_expand => true,
:memory_shares => 163_840,
:memory_shares_level => "normal",
Expand All @@ -202,16 +236,41 @@ def assert_specific_resource_pool
)
end

def assert_specific_switch
# TODO: check a switch
def assert_specific_dvswitch
dvs = ems.switches.find_by(:uid_ems => "dvs-8")

expect(dvs).not_to be_nil
expect(dvs).to have_attributes(
:uid_ems => "dvs-8",
:name => "DC0_DVS",
:ports => 288,
:switch_uuid => "4e 1f 2b 50 19 20 4c f7-f3 11 41 90 35 76 52 7b",
:type => "ManageIQ::Providers::Vmware::InfraManager::DistributedVirtualSwitch",
:allow_promiscuous => false,
:forged_transmits => false,
:mac_changes => false,
)
expect(dvs.lans.count).to eq(3)
end

def assert_specific_lan
# TODO: check a lan
def assert_specific_dvportgroup
lan = ems.lans.find_by(:uid_ems => "dvportgroup-10")

expect(lan).not_to be_nil
expect(lan).to have_attributes(
:name => "DC0_DVPG0",
:uid_ems => "dvportgroup-10",
:allow_promiscuous => false,
:forged_transmits => false,
:mac_changes => false,
:tag => nil,
)

expect(lan.switch.uid_ems).to eq("dvs-8")
end

def assert_specific_vm
vm = ems.vms.find_by(:ems_ref => "vm-17")
vm = ems.vms.find_by(:ems_ref => "vm-21")

expect(vm).to have_attributes(
:connection_state => "connected",
Expand All @@ -221,7 +280,7 @@ def assert_specific_vm
:cpu_shares => 1000,
:cpu_shares_level => "normal",
:cpu_affinity => nil,
:ems_ref => "vm-17",
:ems_ref => "vm-21",
:location => "DC0_C0_RP0_VM1/DC0_C0_RP0_VM1.vmx",
:memory_reserve => 0,
:memory_reserve_expand => false,
Expand All @@ -231,12 +290,12 @@ def assert_specific_vm
:name => "DC0_C0_RP0_VM1",
:raw_power_state => "poweredOn",
:type => "ManageIQ::Providers::Vmware::InfraManager::Vm",
:uid_ems => "423d8331-b640-489f-e3be-61d33a04a258",
:uid_ems => "422bf630-7e83-c7dd-f226-56b41f3c50ef",
:vendor => "vmware",
)

expect(vm.hardware).to have_attributes(
:bios => "423d8331-b640-489f-e3be-61d33a04a258",
:bios => "422bf630-7e83-c7dd-f226-56b41f3c50ef",
:cpu_cores_per_socket => 1,
:cpu_sockets => 1,
:cpu_total_cores => 1,
Expand All @@ -259,7 +318,7 @@ def assert_specific_vm
)

expect(vm.host).not_to be_nil
expect(vm.host.ems_ref).to eq("host-12")
expect(vm.host.ems_ref).to eq("host-16")
end
end
end
Loading

0 comments on commit 452740e

Please sign in to comment.