Skip to content

Commit

Permalink
Fixes #27952 - Values lost on Host and profiles edit page (#43)
Browse files Browse the repository at this point in the history
* Fixes #27952 - Values lost on Host and profiles edit page

* refactored resource group

* handling changes from #42

* fixed premium_os_disk and commented for azure properties

* Refactored Interfaces and Added Region attribute on AzureRM compute resource

* fixes compute profile interfaces and associate vm

* Refactoring code

* changes for nics and minor refactoring
  • Loading branch information
apuntamb authored and ShimShtein committed Oct 31, 2019
1 parent 6f2772a commit d40391d
Show file tree
Hide file tree
Showing 16 changed files with 383 additions and 304 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
module ForemanAzureRM
module Concerns
module HostsControllerExtensions
extend ActiveSupport::Concern

def sizes
if (azure_rm_resource = Image.unscoped.find_by_uuid(params[:image_id])).present?
resource = azure_rm_resource.compute_resource
render :json => resource.vm_sizes(params[:region_string]).map { |size| size.name }
azure_rm_resource = ComputeResource.unscoped.find_by_id(params[:compute_resource_id])
if azure_rm_resource.present?
render :json => azure_rm_resource.vm_sizes.map { |size| size.name }
else
no_sizes = _('The region you selected has no sizes associated with it')
render :json => "[\"#{no_sizes}\"]"
end
end

def subnets
azure_rm_image = Image.unscoped.find_by_uuid(params[:image_id])
if azure_rm_image.present?
azure_rm_resource = azure_rm_image.compute_resource
subnets = azure_rm_resource.subnets(params[:region])
azure_rm_resource = ComputeResource.unscoped.find_by_id(params[:compute_resource_id])
if azure_rm_resource.present?
subnets = azure_rm_resource.subnets
if subnets.present?
render :json => subnets
else
Expand Down
87 changes: 45 additions & 42 deletions app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def define_managed_storage_profile(vm_name, vhd_path, publisher, offer, sku, ver
os_disk.managed_disk = managed_disk_params
storage_profile.os_disk = os_disk

# Currently eliminating data disk creation since capability does not exist.
# TODO - disk creation for volume capability

if vhd_path.nil?
# We are using a marketplace image
Expand Down Expand Up @@ -72,45 +72,44 @@ def define_network_profile(network_interface_card_ids)
network_profile
end

def create_nics(args = {})
def create_nics(region, args = {})
nics = []
formatted_region = args[:azure_vm][:location].gsub(/\s+/, '').downcase
args[:interfaces_attributes].each do |nic, attrs|
attrs[:pubip_alloc] = attrs[:bridge]
attrs[:privip_alloc] = (attrs[:name] == 'false') ? false : true
pip_alloc = case attrs[:pubip_alloc]
when 'Static'
NetworkModels::IPAllocationMethod::Static
when 'Dynamic'
NetworkModels::IPAllocationMethod::Dynamic
when 'None'
nil
end
priv_ip_alloc = if attrs[:priv_ip_alloc]
NetworkModels::IPAllocationMethod::Static
else
NetworkModels::IPAllocationMethod::Dynamic
end
if pip_alloc.present?
private_ip = (attrs[:private_ip] == 'false') ? false : true
priv_ip_alloc = if private_ip
NetworkModels::IPAllocationMethod::Static
else
NetworkModels::IPAllocationMethod::Dynamic
end
pub_ip_alloc = case attrs[:public_ip]
when 'Static'
NetworkModels::IPAllocationMethod::Static
when 'Dynamic'
NetworkModels::IPAllocationMethod::Dynamic
when 'None'
nil
end
if pub_ip_alloc.present?
public_ip_params = NetworkModels::PublicIPAddress.new.tap do |ip|
ip.location = formatted_region
ip.public_ipallocation_method = pip_alloc
ip.location = region
ip.public_ipallocation_method = pub_ip_alloc
end
pip = sdk.create_or_update_pip(args[:azure_vm][:resource_group],
"#{args[:azure_vm][:vm_name]}-pip#{nic}",

pip = sdk.create_or_update_pip(args[:resource_group],
"#{args[:vm_name]}-pip#{nic}",
public_ip_params)
end
new_nic = sdk.create_or_update_nic(
args[:azure_vm][:resource_group],
"#{args[:azure_vm][:vm_name]}-nic#{nic}",
args[:resource_group],
"#{args[:vm_name]}-nic#{nic}",
NetworkModels::NetworkInterface.new.tap do |interface|
interface.location = formatted_region
interface.location = region
interface.ip_configurations = [
NetworkModels::NetworkInterfaceIPConfiguration.new.tap do |nic_conf|
nic_conf.name = "#{args[:azure_vm][:vm_name]}-nic#{nic}"
nic_conf.name = "#{args[:vm_name]}-nic#{nic}"
nic_conf.private_ipallocation_method = priv_ip_alloc
nic_conf.subnet = subnets(args[:azure_vm][:location]).select{ |subnet| subnet.id == attrs[:network] }.first
nic_conf.public_ipaddress = pip.present? ? pip : nil
nic_conf.subnet = subnets.select{ |subnet| subnet.id == attrs[:network] }.first
nic_conf.public_ipaddress = pip
end
]
end
Expand All @@ -120,7 +119,7 @@ def create_nics(args = {})
nics
end

def create_managed_virtual_machine(vm_hash)
def initialize_vm(vm_hash)
custom_data = vm_hash[:custom_data]
msg = "Creating Virtual Machine #{vm_hash[:name]} in Resource Group #{vm_hash[:resource_group]}."
logger.debug msg
Expand Down Expand Up @@ -183,30 +182,34 @@ def create_managed_virtual_machine(vm_hash)
vm.hardware_profile = ComputeModels::HardwareProfile.new.tap do |hw_profile|
hw_profile.vm_size = vm_hash[:vm_size]
end
vm.network_profile = define_network_profile(vm_hash[:network_interface_card_ids])
end

response = sdk.create_or_update_vm(vm_hash[:resource_group], vm_hash[:name], vm_create_params)
logger.debug "Virtual Machine #{vm_hash[:name]} Created Successfully."
response
vm_create_params
end

def create_managed_virtual_machine(vm_hash)
vm_params = initialize_vm(vm_hash)
vm_params.network_profile = define_network_profile(vm_hash[:network_interface_card_ids])
sdk.create_or_update_vm(vm_hash[:resource_group], vm_hash[:name], vm_params)
end

def create_vm_extension(args = {})
if args[:azure_vm][:script_command].present? || args[:azure_vm][:script_uris].present?
def create_vm_extension(region, args = {})
if args[:script_command].present? || args[:script_uris].present?
args[:script_uris] ||= args[:script_uris].to_s
extension = ComputeModels::VirtualMachineExtension.new
if args[:azure_vm][:platform] == 'Linux'
if args[:platform] == 'Linux'
extension.publisher = 'Microsoft.Azure.Extensions'
extension.virtual_machine_extension_type = 'CustomScript'
extension.type_handler_version = '2.0'
end
extension.auto_upgrade_minor_version = true
extension.location = args[:azure_vm][:location].gsub(/\s+/, '').downcase
extension.location = region
extension.settings = {
'commandToExecute' => args[:azure_vm][:script_command],
'fileUris' => args[:azure_vm][:script_uris].split(',')
'commandToExecute' => args[:script_command],
'fileUris' => args[:script_uris].split(',')
}
sdk.create_or_update_vm_extensions(args[:azure_vm][:resource_group],
args[:azure_vm][:vm_name],
sdk.create_or_update_vm_extensions(args[:resource_group],
args[:vm_name],
'ForemanCustomScript',
extension)
end
Expand Down
Loading

0 comments on commit d40391d

Please sign in to comment.