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 Mar 29, 2018
1 parent 565c01f commit 47acfef
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
31 changes: 31 additions & 0 deletions db/migrate/20180316164826_update_switch_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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 Switch < ActiveRecord::Base
self.inheritance_column = :_type_disabled

has_many :hosts, :through => :host_switches
has_many :host_switches
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.update_all(:type => nil)
end
end
end
54 changes: 54 additions & 0 deletions spec/migrations/20180316164826_update_switch_types_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require_migration

class UpdateSwitchTypes < ActiveRecord::Migration[5.0]
class Host < ActiveRecord::Base
self.inheritance_column = :_type_disabled # disable STI

has_many :host_switches
has_many :switches, :through => :host_switches
end

class HostSwitch < ActiveRecord::Base
belongs_to :host
belongs_to :switch
end
end

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")

migrate

expect(dvswitch.reload.type).to be_nil
expect(host_switch.reload.type).to be_nil
end
end
end

0 comments on commit 47acfef

Please sign in to comment.