Skip to content

Commit

Permalink
Merge pull request #18505 from djberg96/conversion_host_resource_tag
Browse files Browse the repository at this point in the history
Tag associated resource for conversion hosts
  • Loading branch information
agrare authored Mar 5, 2019
2 parents 335fa68 + 70dd7bd commit a7b4e8e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
31 changes: 28 additions & 3 deletions app/models/conversion_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class ConversionHost < ApplicationRecord

include_concern 'Configurations'

after_create :tag_resource_as_enabled
after_destroy :tag_resource_as_disabled

# To be eligible, a conversion host must have the following properties
# - A transport mechanism is configured for source (set by 3rd party)
# - Credentials are set on the resource and SSH connection works
Expand All @@ -42,8 +45,8 @@ def check_ssh_connection
end

def source_transport_method
return 'vddk' if vddk_transport_supported
return 'ssh' if ssh_transport_supported
return 'vddk' if vddk_transport_supported?
return 'ssh' if ssh_transport_supported?
end

def ipaddress(family = 'ipv4')
Expand Down Expand Up @@ -189,17 +192,39 @@ def install_conversion_host_module
_log.error("Ansible module installation failed for '#{resource.name}'}with [#{e.class}: #{e.message}]")
end

# Wrapper method for the various tag_resource_as_xxx methods.
#--
# TODO: Do we need this?
#
def tag_resource_as(status)
send("tag_resource_as_#{status}")
end

# Tag the associated resource as enabled. The following tags are set or removed:
#
# - 'v2v_transformation_host/true' (added)
# - 'v2v_transformation_host/vddk' (added if vddk supported)
# - 'v2v_transformation_host/ssh' (added if ssh supported)
# - 'v2v_transformation_host/false' (removed if present)
#
def tag_resource_as_enabled
resource.tag_add('v2v_transformation_host/true')
resource.tag_add('v2v_transformation_method/vddk')
resource.tag_add('v2v_transformation_method/vddk') if vddk_transport_supported?
resource.tag_add('v2v_transformation_method/ssh') if ssh_transport_supported?
resource.tag_remove('v2v_transformation_host/false')
end

# Tag the associated resource as disabled. The following tags are set or removed:
#
# - 'v2v_transformation_host/false' (added)
# - 'v2v_transformation_host/true' (removed if present)
# - 'v2v_transformation_host/vddk' (removed if present)
# - 'v2v_transformation_host/ssh' (removed if present)
#
def tag_resource_as_disabled
resource.tag_add('v2v_transformation_host/false')
resource.tag_remove('v2v_transformation_host/true')
resource.tag_remove('v2v_transformation_method/vddk')
resource.tag_remove('v2v_transformation_method/ssh')
end
end
26 changes: 25 additions & 1 deletion spec/models/conversion_host/configurations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe ConversionHost do
let(:conversion_host) { FactoryBot.create(:conversion_host, :resource => vm) }
let(:conversion_host) { FactoryBot.create(:conversion_host, :resource => vm, :ssh_transport_supported => true) }
let(:params) do
{
:name => 'transformer',
Expand All @@ -11,9 +11,11 @@

context "processing configuration requests" do
let(:vm) { FactoryBot.create(:vm_openstack) }

before(:each) do
allow(ConversionHost).to receive(:new).and_return(conversion_host)
end

context ".enable" do
let(:expected_notify) do
{
Expand All @@ -24,6 +26,7 @@
}
}
end

it "to succeed and send notification" do
allow(conversion_host).to receive(:enable_conversion_host_role)
expect(Notification).to receive(:create).with(expected_notify)
Expand All @@ -36,6 +39,17 @@
expect(Notification).to receive(:create).with(expected_notify)
expect { described_class.enable(params) }.to raise_error(StandardError)
end

it "tags the associated resource as expected" do
allow(conversion_host).to receive(:enable_conversion_host_role)
taggings = conversion_host.resource.taggings
tag_names = taggings.map { |tagging| tagging.tag.name }

expect(tag_names).to contain_exactly(
'/user/v2v_transformation_host/true',
'/user/v2v_transformation_method/ssh'
)
end
end

context "#disable" do
Expand All @@ -61,6 +75,16 @@
expect(Notification).to receive(:create).with(expected_notify)
expect { conversion_host.disable }.to raise_error(StandardError)
end

it "tags the associated resource as expected" do
allow(conversion_host).to receive(:disable_conversion_host_role)
expect(Notification).to receive(:create).with(expected_notify)
conversion_host.disable
taggings = conversion_host.resource.taggings
tag_names = taggings.map { |tagging| tagging.tag.name }

expect(tag_names).to contain_exactly('/user/v2v_transformation_host/false')
end
end
end

Expand Down

0 comments on commit a7b4e8e

Please sign in to comment.