Skip to content

Commit

Permalink
Merge pull request #61 from GabrielSVinha/provider_discover
Browse files Browse the repository at this point in the history
Create provider discovery feature.
  • Loading branch information
juliancheal authored Jul 27, 2017
2 parents 4096756 + 9643024 commit 479fe4d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
36 changes: 27 additions & 9 deletions app/models/manageiq/providers/lenovo/manager_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,47 @@ def raw_connect(username, password, host, port, verify_ssl)
# Factory method to create EmsLenovo with instances
# or images for the given authentication. Created EmsLenovo instances
# will automatically have EmsRefreshes queued up.
def discover(username, password, host, port, verify_ssl = 0)
new_emses = []
def discover(ip_address, port)
require 'xclarity_client'

if XClarityClient::Discover.responds?(ip_address, port)
new_ems = create!(
:name => "Discovered Provider ##{count + 1}",
:hostname => URI('https://' + ip_address),
:zone => Zone.default_zone
)

raw_connect(username, password, host, port, verify_ssl)
# Set empty authentications
create_default_authentications(new_ems)

EmsRefresh.queue_refresh(new_emses) unless new_emses.blank?
_log.info("Reached Lenovo XClarity Appliance with endpoint: #{ip_address}")
_log.info("Created EMS: #{new_ems.name} with id: #{new_ems.id}")
end

new_emses
EmsRefresh.queue_refresh(new_ems) unless new_ems.blank?
end

def discover_queue(username, password, zone = nil)
def discover_queue(ip_address, port, zone = nil)
MiqQueue.put(
:class_name => name,
:method_name => "discover_from_queue",
:args => [username, MiqPassword.encrypt(password)],
:args => [ip_address, port],
:zone => zone
)
end

private

def discover_from_queue(username, password)
discover(username, MiqPassword.decrypt(password))
def discover_from_queue(ip_address, port)
discover(ip_address, port)
end

def create_default_authentications(ems)
auth = Authentication.new
auth.userid = ''
auth.password = ''
auth.resource_id = ems.id
auth.save!
end
end
end
2 changes: 2 additions & 0 deletions manageiq-providers-lenovo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ Gem::Specification.new do |s|

s.add_dependency "xclarity_client", "~> 0.5.2"
s.add_development_dependency "codeclimate-test-reporter", "~> 1.0.0"
s.add_development_dependency "webmock", "~> 2.1.0"
s.add_development_dependency "simplecov"
s.add_dependency "faker", "~>1.8.3"
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'xclarity_client'
require 'faker'

describe ManageIQ::Providers::Lenovo::PhysicalInfraManager do
before :all do
Expand Down Expand Up @@ -125,11 +126,6 @@
end
end

it 'will execute discover successfully' do
result = described_class.new.class.discover(@auth[:user], @auth[:pass], @auth[:host], @auth[:port])
expect(result).to eq([])
end

it 'will execute discover_queue successfully' do
result = described_class.new.class.discover_queue(@auth[:user], @auth[:pass])
expect(result).to_not eq(nil)
Expand All @@ -152,4 +148,28 @@
client = described_class.new.connect(@auth)
expect(client).to be_a(XClarityClient::Client)
end

context 'valid discover' do
before :each do
EvmSpecHelper.local_miq_server(:zone => Zone.seed)
@port = Random.rand(10_000).to_s
@address = URI('https://' + Faker::Internet.ip_v4_address + ':' + @port)
WebMock.allow_net_connect!
stub_request(:get, File.join(@address.to_s, '/aicc')).to_return(:status => [200, 'OK'])
end

it 'should create a new instance' do
expect { described_class.discover(@address.host, @port) }.to change { described_class.count }.by 1
end
end

context 'invalid discover' do
before :each do
@port = Random.rand(10_000).to_s
@address = URI('https://' + Faker::Internet.ip_v4_address + ':' + @port)
end
it 'should not create a new instance' do
expect { described_class.discover(@address.host, @port) }.to change { described_class.count }.by 0
end
end
end

0 comments on commit 479fe4d

Please sign in to comment.