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

Tag associated resource for conversion hosts #18505

Merged
merged 4 commits into from
Mar 5, 2019
Merged
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah lets delete this if it isn't used...it doesn't look that helpful


# 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