Skip to content

Commit

Permalink
Merge pull request #172 from djberg96/optional_nic
Browse files Browse the repository at this point in the history
Allow for possibility of private IP
  • Loading branch information
Bronagh Sorota authored Dec 19, 2017
2 parents 676c6f2 + 70fbc2f commit 9b9fba3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ManageIQ::Providers::Azure::CloudManager::Provision::Cloning
def do_clone_task_check(clone_task_ref)
source.with_provider_connection do |azure|
vms = Azure::Armrest::VirtualMachineService.new(azure)
vms = ::Azure::Armrest::VirtualMachineService.new(azure)
instance = vms.get(clone_task_ref[:vm_name], clone_task_ref[:vm_resource_group])
status = instance.properties.provisioning_state
return true if status == "Succeeded"
Expand All @@ -18,7 +18,7 @@ def gather_storage_account_properties
sas = nil

source.with_provider_connection do |azure|
sas = Azure::Armrest::StorageAccountService.new(azure)
sas = ::Azure::Armrest::StorageAccountService.new(azure)
end

return if sas.nil?
Expand All @@ -35,7 +35,7 @@ def gather_storage_account_properties
source_uri = image.uri

target_uri = File.join(endpoint, "manageiq", dest_name + "_" + SecureRandom.uuid + ".vhd")
rescue Azure::Armrest::ResourceNotFoundException => err
rescue ::Azure::Armrest::ResourceNotFoundException => err
_log.error("Error Class=#{err.class.name}, Message=#{err.message}")
end

Expand All @@ -47,8 +47,6 @@ def custom_data
end

def prepare_for_clone_task
nic_id = associated_nic || create_nic

# TODO: Ideally this would be a check against source.storage or source.disks
if source.ems_ref =~ /.+:.+:.+:.+/
urn_keys = %w(publisher offer sku version)
Expand Down Expand Up @@ -82,13 +80,22 @@ def prepare_for_clone_task
:caching => 'ReadWrite',
:osType => os
}
},
:networkProfile => {
:networkInterfaces => [{:id => nic_id}],
}
}
}

# The -1 value is set in ProvisionWorkflow to distinguish between the
# desire for a new Public IP address vs a private IP.
#
if floating_ip
nic_id = associated_nic || create_nic(true)
else
public_ip = options[:floating_ip_address].first == -1
nic_id = create_nic(public_ip)
end

cloud_options[:properties][:networkProfile] = {:networkInterfaces => [{:id => nic_id}]}

if target_uri
cloud_options[:properties][:storageProfile][:osDisk][:name] = dest_name + SecureRandom.uuid + '.vhd'
cloud_options[:properties][:storageProfile][:osDisk][:image] = {:uri => source_uri}
Expand All @@ -100,6 +107,7 @@ def prepare_for_clone_task
end

cloud_options[:properties][:osProfile][:customData] = custom_data unless userdata_payload.nil?

cloud_options
end

Expand All @@ -125,41 +133,47 @@ def associated_nic
floating_ip.try(:network_port).try(:ems_ref)
end

def create_nic
def create_nic(with_public_ip = true)
source.with_provider_connection do |azure|
nis = Azure::Armrest::Network::NetworkInterfaceService.new(azure)
ips = Azure::Armrest::Network::IpAddressService.new(azure)
ip = ips.create("#{dest_name}-publicIp", resource_group.name, :location => region)
network_options = build_nic_options(ip.id)
nis = ::Azure::Armrest::Network::NetworkInterfaceService.new(azure)

if with_public_ip
ips = ::Azure::Armrest::Network::IpAddressService.new(azure)
ip = ips.create("#{dest_name}-publicIp", resource_group.name, :location => region)
network_options = build_nic_options(ip.id)
else
network_options = build_nic_options
end

return nis.create(dest_name, resource_group.name, network_options).id
end
end

def build_nic_options(ip)
def build_nic_options(ip_id = nil)
ip_config = {
:name => dest_name,
:properties => {
:subnet => {:id => cloud_subnet.ems_ref}
}
}

ip_config[:properties][:publicIPAddress] = {:id => ip_id} if ip_id

network_options = {
:location => region,
:properties => {
:ipConfigurations => [
:name => dest_name,
:properties => {
:subnet => {
:id => cloud_subnet.ems_ref
},
:publicIPAddress => {
:id => ip
},
}
],
:ipConfigurations => [ip_config]
}
}

network_options[:properties][:networkSecurityGroup] = {:id => security_group.ems_ref} if security_group

network_options
end

def start_clone(clone_options)
source.with_provider_connection do |azure|
vms = Azure::Armrest::VirtualMachineService.new(azure)
vms = ::Azure::Armrest::VirtualMachineService.new(azure)
vm = vms.create(dest_name, resource_group.name, clone_options)

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def allowed_cloud_subnets(_options = {})
end
end

def allowed_floating_ip_addresses(options = {})
super(options).merge(-1 => 'New')
end

private

def dialog_name_from_automate(message = 'get_dialog_name')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def product_name
end

it "without floating_ip create new nic and assign to network profile" do
allow(floating_ip).to receive(:floating_ip).and_return(nil)
allow(subject).to receive(:floating_ip).and_return(nil)
allow(subject).to receive(:options).and_return({:floating_ip_address => [-1, 'New']})
expect(subject.prepare_for_clone_task[:properties][:networkProfile][:networkInterfaces][0][:id]).to eq(nic_id)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
end

it "allowed_floating_ips" do
expect(workflow.allowed_floating_ip_addresses.length).to eq(2)
expect(workflow.allowed_floating_ip_addresses.length).to eq(3)
expect(workflow.allowed_floating_ip_addresses[-1]).to eq('New')
end
end
end
Expand Down

0 comments on commit 9b9fba3

Please sign in to comment.