Skip to content

Commit

Permalink
Set the switch types for VMware switches
Browse files Browse the repository at this point in the history
Update existing switches to set their types to either
DistributedVirtualSwitch for shared switches or HostVirtualSwitch for
non-shared switches.
  • Loading branch information
agrare committed Apr 2, 2018
1 parent 565c01f commit 10edec6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
43 changes: 43 additions & 0 deletions db/migrate/20180316164826_update_switch_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class UpdateSwitchTypes < ActiveRecord::Migration[5.0]
VMWARE_HOSTS = %w(ManageIQ::Providers::Vmware::InfraManager::Host
ManageIQ::Providers::Vmware::InfraManager::HostEsx).freeze
VMWARE_DISTRIBUTED_VIRTUAL_SWITCH = "ManageIQ::Providers::Vmware::InfraManager::DistributedVirtualSwitch".freeze
VMWARE_HOST_VIRTUAL_SWITCH = "ManageIQ::Providers::Vmware::InfraManager::HostVirtualSwitch".freeze

class Host < ActiveRecord::Base
self.inheritance_column = :_type_disabled # disable STI

has_many :host_switches, :class_name => "UpdateSwitchTypes::HostSwitch"
has_many :switches, :through => :host_switches, :class_name => "UpdateSwitchTypes::Switch"
end

class HostSwitch < ActiveRecord::Base
belongs_to :host, :class_name => "UpdateSwitchTypes::Host"
belongs_to :switch, :class_name => "UpdateSwitchTypes::Switch"
end

class Switch < ActiveRecord::Base
self.inheritance_column = :_type_disabled

has_many :hosts, :through => :host_switches, :class_name => "UpdateSwitchTypes::Host"
has_many :host_switches, :class_name => "UpdateSwitchTypes::HostSwitch"
end

def up
say_with_time("Setting switch types") do
# Switches connected to VMware hosts which are shared are DVS type
Switch.distinct.where(:type => nil, :shared => true).joins(:hosts).where(:hosts => {:type => VMWARE_HOSTS})
.update_all(:type => VMWARE_DISTRIBUTED_VIRTUAL_SWITCH)

# Switches connected to VMware hosts which are not shared are standard host switches
Switch.distinct.where(:type => nil, :shared => [false, nil]).joins(:hosts).where(:hosts => {:type => VMWARE_HOSTS})
.update_all(:type => VMWARE_HOST_VIRTUAL_SWITCH)
end
end

def down
say_with_time("Clearing all switch types") do
Switch.where(:type => [VMWARE_DISTRIBUTED_VIRTUAL_SWITCH, VMWARE_HOST_VIRTUAL_SWITCH]).update_all(:type => nil)
end
end
end
43 changes: 43 additions & 0 deletions spec/migrations/20180316164826_update_switch_types_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require_migration

describe UpdateSwitchTypes do
let(:switch_stub) { migration_stub(:Switch) }
let(:host_stub) { migration_stub(:Host) }

migration_context :up do
it "migrates a series of representative rows" do
dvswitch = switch_stub.create!(:name => "DVS", :uid_ems => "dvswitch-1", :shared => true)
host_switch = switch_stub.create!(:name => "vSwitch0", :uid_ems => "vSwitch0")
physical_switch = switch_stub.create!(:name => "Physical Switch", :uid_ems => "switch-1",
:type => "ManageIQ::Providers::Lenovo::PhysicalInfraManager::PhysicalSwitch")

host_stub.create!(:type => "ManageIQ::Providers::Vmware::InfraManager::HostEsx").tap do |host|
host.host_switches.create!(:host => host, :switch => dvswitch)
host.host_switches.create!(:host => host, :switch => host_switch)
end

migrate

expect(dvswitch.reload.type).to eq("ManageIQ::Providers::Vmware::InfraManager::DistributedVirtualSwitch")
expect(host_switch.reload.type).to eq("ManageIQ::Providers::Vmware::InfraManager::HostVirtualSwitch")
expect(physical_switch.reload.type).to eq("ManageIQ::Providers::Lenovo::PhysicalInfraManager::PhysicalSwitch")
end
end

migration_context :down do
it "migrates a series of representative rows" do
dvswitch = switch_stub.create!(:name => "DVS", :uid_ems => "dvswitch-1", :shared => true,
:type => "ManageIQ::Providers::Vmware::InfraManager::DistributedVirtualSwitch")
host_switch = switch_stub.create!(:name => "vSwitch0", :uid_ems => "vSwitch0",
:type => "ManageIQ::Providers::Vmware::InfraManager::HostVirtualSwitch")
physical_switch = switch_stub.create!(:name => "Physical Switch", :uid_ems => "switch-1",
:type => "ManageIQ::Providers::Lenovo::PhysicalInfraManager::PhysicalSwitch")

migrate

expect(dvswitch.reload.type).to be_nil
expect(host_switch.reload.type).to be_nil
expect(physical_switch.reload.type).to eq("ManageIQ::Providers::Lenovo::PhysicalInfraManager::PhysicalSwitch")
end
end
end

0 comments on commit 10edec6

Please sign in to comment.