Skip to content

Commit

Permalink
Add fetch_nic_ip_address
Browse files Browse the repository at this point in the history
  • Loading branch information
sue445 committed Apr 14, 2016
1 parent 9d224ab commit 54f3874
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/vagrant-cloudstack/action/read_ssh_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,7 @@ def read_ssh_info(cloudstack, machine)
end
end

nic_ip_address = server.nics[0]['ipaddress']

unless domain_config.ssh_network_id.nil?
# When ssh_network_id is specified, use the IP address that is linked to the network_id of nic
ssh_nic = server.nics.find { |nic| nic["networkid"] == domain_config.ssh_network_id }
nic_ip_address = ssh_nic["ipaddress"] if ssh_nic
end
nic_ip_address = fetch_nic_ip_address(server.nics, domain_config)

ssh_info = {
:host => pf_ip_address || nic_ip_address,
Expand All @@ -89,6 +83,21 @@ def read_ssh_info(cloudstack, machine)
ssh_info = ssh_info.merge({ :username => domain_config.ssh_user }) unless domain_config.ssh_user.nil?
ssh_info
end

def fetch_nic_ip_address(nics, domain_config)
ssh_nic =
if !domain_config.ssh_network_id.nil?
nics.find { |nic| nic["networkid"] == domain_config.ssh_network_id }
elsif !domain_config.ssh_network_name.nil?
nics.find { |nic| nic["networkname"] == domain_config.ssh_network_name }
else
# When without neither ssh_network_id and ssh_network_name, use 1st nic
nics[0]
end

ssh_nic ||= nics[0]
ssh_nic["ipaddress"]
end
end
end
end
Expand Down
80 changes: 80 additions & 0 deletions spec/vagrant-cloudstack/action/read_ssh_info_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'spec_helper'
require 'vagrant-cloudstack/action/read_ssh_info'
require 'vagrant-cloudstack/config'

describe VagrantPlugins::Cloudstack::Action::ReadSSHInfo do
let(:action) { VagrantPlugins::Cloudstack::Action::ReadSSHInfo.new(nil, nil) }

describe "#fetch_nic_ip_address" do
subject { action.fetch_nic_ip_address(nics, domain_config) }

let(:nics) do
[
{ "networkid" => "networkid1", "networkname" => "networkname1", "ipaddress" => "127.0.0.1" },
{ "networkid" => "networkid2", "networkname" => "networkname2", "ipaddress" => "127.0.0.2" },
{ "networkid" => "networkid3", "networkname" => "networkname3", "ipaddress" => "127.0.0.3" },
]
end

let(:ssh_network_id) { Vagrant::Plugin::V2::Config::UNSET_VALUE }
let(:ssh_network_name) { Vagrant::Plugin::V2::Config::UNSET_VALUE }

let(:domain_config) do
config = VagrantPlugins::Cloudstack::Config.new
config.domain_config :cloudstack do |cloudstack|
cloudstack.ssh_network_id = ssh_network_id
cloudstack.ssh_network_name = ssh_network_name
end
config.finalize!
config.get_domain_config(:cloudstack)
end

context "without neither ssh_network_id and ssh_network_name" do
it { should eq "127.0.0.1" }
end

context "with ssh_network_id" do
context "when exists in nics" do
let(:ssh_network_id) { "networkid2" }

it { should eq "127.0.0.2" }
end

context "when not exists in nics" do
let(:ssh_network_id) { "unknown" }

it { should eq "127.0.0.1" }
end
end

context "with ssh_network_id" do
context "when exists in nics" do
let(:ssh_network_name) { "networkname3" }

it { should eq "127.0.0.3" }
end

context "when not exists in nics" do
let(:ssh_network_name) { "unknown" }

it { should eq "127.0.0.1" }
end
end

context "with both ssh_network_id and ssh_network_name" do
context "when exists in nics" do
let(:ssh_network_id) { "networkid2" }
let(:ssh_network_name) { "networkname3" }

it { should eq "127.0.0.2" }
end

context "when not exists in nics" do
let(:ssh_network_id) { "unknown" }
let(:ssh_network_name) { "unknown" }

it { should eq "127.0.0.1" }
end
end
end
end

0 comments on commit 54f3874

Please sign in to comment.