Skip to content

Commit

Permalink
changes for nics and minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
apuntamb committed Oct 31, 2019
1 parent 68d56f0 commit 791a389
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
30 changes: 15 additions & 15 deletions app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb
Expand Up @@ -76,19 +76,19 @@ def create_nics(region, args = {})
nics = []
args[:interfaces_attributes].each do |nic, attrs|
private_ip = (attrs[:private_ip] == 'false') ? false : true
pub_ip_alloc = case attrs[:public_ip]
when 'Static'
NetworkModels::IPAllocationMethod::Static
when 'Dynamic'
NetworkModels::IPAllocationMethod::Dynamic
when 'None'
nil
end
priv_ip_alloc = if private_ip
NetworkModels::IPAllocationMethod::Static
else
NetworkModels::IPAllocationMethod::Dynamic
end
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 = region
Expand All @@ -109,7 +109,7 @@ def create_nics(region, args = {})
nic_conf.name = "#{args[:vm_name]}-nic#{nic}"
nic_conf.private_ipallocation_method = priv_ip_alloc
nic_conf.subnet = subnets.select{ |subnet| subnet.id == attrs[:network] }.first
nic_conf.public_ipaddress = pip.present? ? pip : nil
nic_conf.public_ipaddress = pip
end
]
end
Expand Down Expand Up @@ -195,7 +195,7 @@ def create_managed_virtual_machine(vm_hash)

def create_vm_extension(region, args = {})
if args[:script_command].present? || args[:script_uris].present?
args[:script_uris] = args[:script_uris].to_s unless args[:script_uris].present?
args[:script_uris] ||= args[:script_uris].to_s
extension = ComputeModels::VirtualMachineExtension.new
if args[:platform] == 'Linux'
extension.publisher = 'Microsoft.Azure.Extensions'
Expand Down
15 changes: 6 additions & 9 deletions app/models/foreman_azure_rm/azure_rm.rb
Expand Up @@ -12,6 +12,8 @@ class AzureRM < ComputeResource
alias_attribute :region, :url
alias_attribute :tenant, :uuid

alias_method :available_subnets, :subnets

validates :user, :password, :url, :uuid, :app_ident, :presence => true

has_one :key_pair, :foreign_key => :compute_resource_id, :dependent => :destroy
Expand Down Expand Up @@ -125,10 +127,6 @@ def available_networks(attr = {})
subnets
end

def available_subnets
subnets
end

def virtual_networks
@virtual_networks ||= sdk.vnets.select { |vnet| vnet.location == region }
end
Expand All @@ -143,7 +141,7 @@ def subnets
end

def new_interface(attrs = {})
args = { :network => "", :public_ip => "", :private_ip => false }.merge(attrs.to_h)
args = { :network => "", :public_ip => "", :private_ip => false, 'persisted?' => false }.merge(attrs.to_h)
OpenStruct.new(args)
end

Expand All @@ -168,8 +166,8 @@ def vm_instance_defaults
def vm_nics(vm)
ifaces = []
vm.network_profile.network_interfaces.each do |nic|
nic_rg = nic.id.split('/')[4]
nic_name = nic.id.split('/')[-1]
nic_rg = ((split_nic_id = nic.id.split('/'))[4]
nic_name = split_nic_id[-1]
ifaces << sdk.vm_nic(nic_rg, nic_name)
end
ifaces
Expand Down Expand Up @@ -251,13 +249,12 @@ def create_vm(args = {})

def destroy_vm(uuid)
vm = find_vm_by_uuid(uuid)
vm_name = vm.name
rg_name = vm.resource_group
os_disk = vm.azure_vm.storage_profile.os_disk
data_disks = vm.azure_vm.storage_profile.data_disks
nic_ids = vm.network_interface_card_ids

sdk.delete_vm(rg_name, vm_name)
sdk.delete_vm(rg_name, vm.name)

nic_ids.each do |nic_id|
nic = sdk.vm_nic(rg_name, nic_id.split('/')[-1])
Expand Down
2 changes: 1 addition & 1 deletion app/models/foreman_azure_rm/azure_rm_compute.rb
Expand Up @@ -11,7 +11,7 @@ class AzureRMCompute
def initialize(azure_vm: ComputeModels::VirtualMachine.new,
sdk: sdk,
resource_group: azure_vm.resource_group,
nics: [] )
nics: [])

@azure_vm = azure_vm
@sdk = sdk
Expand Down

0 comments on commit 791a389

Please sign in to comment.