Skip to content

Commit

Permalink
Fixes #28625 - Compute attributes set from CLI not shown on UI (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
apuntamb authored and ShimShtein committed Jan 9, 2020
1 parent d135307 commit e049599
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
59 changes: 28 additions & 31 deletions app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ module VMExtensions
module ManagedVM
extend ActiveSupport::Concern

def define_managed_storage_profile(vm_name, vhd_path, publisher, offer, sku, version,
os_disk_caching, platform, premium_os_disk)
def define_managed_storage_profile(vm_name, os_disk_caching, platform, premium_os_disk)
storage_profile = ComputeModels::StorageProfile.new
os_disk = ComputeModels::OSDisk.new
managed_disk_params = ComputeModels::ManagedDiskParameters.new
Expand Down Expand Up @@ -35,22 +34,10 @@ 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

# TODO - disk creation for volume capability

if vhd_path.nil?
# We are using a marketplace image
storage_profile.image_reference = image_reference(publisher, offer,
sku, version)
else
# We are using a custom managed image
image_ref = ComputeModels::ImageReference.new
image_ref.id = vhd_path
storage_profile.image_reference = image_ref
end
storage_profile
end

def image_reference(publisher, offer, sku, version)
def marketplace_image_reference(publisher, offer, sku, version)
image_reference = ComputeModels::ImageReference.new
image_reference.publisher = publisher
image_reference.offer = offer
Expand All @@ -59,6 +46,30 @@ def image_reference(publisher, offer, sku, version)
image_reference
end

def define_image(vhd_path)
# If image UUID begins with / it is a custom managed image
# Otherwise it is a marketplace URN
unless vhd_path.start_with?('/')
urn = vhd_path.split(':')
publisher = urn[0]
offer = urn[1]
sku = urn[2]
version = urn[3]
vhd_path = nil
end

if vhd_path.nil?
# For marketplace image
image_reference = marketplace_image_reference(publisher, offer, sku, version)
else
# For custom managed image
image_ref = ComputeModels::ImageReference.new
image_ref.id = vhd_path
image_reference = image_ref
end
image_reference
end

def define_network_profile(network_interface_card_ids)
network_interface_cards = []
network_interface_card_ids.each_with_index do |id, index|
Expand Down Expand Up @@ -133,16 +144,6 @@ def initialize_vm(vm_hash)
sub_resource.id = vm_hash[:availability_set_id]
vm.availability_set = sub_resource
end
# If image UUID begins with / it is a custom managed image
# Otherwise it is a marketplace URN
unless vm_hash[:vhd_path].start_with?('/')
urn = vm_hash[:vhd_path].split(':')
vm_hash[:publisher] = urn[0]
vm_hash[:offer] = urn[1]
vm_hash[:sku] = urn[2]
vm_hash[:version] = urn[3]
vm_hash[:vhd_path] = nil
end

vm.os_profile = ComputeModels::OSProfile.new.tap do |os_profile|
os_profile.computer_name = vm_hash[:name]
Expand Down Expand Up @@ -173,14 +174,9 @@ def initialize_vm(vm_hash)
end
vm.storage_profile = define_managed_storage_profile(
vm_hash[:name],
vm_hash[:vhd_path],
vm_hash[:publisher],
vm_hash[:offer],
vm_hash[:sku],
vm_hash[:version],
vm_hash[:os_disk_caching],
vm_hash[:platform],
vm_hash[:premium_os_disk],
vm_hash[:premium_os_disk]
)
vm.hardware_profile = ComputeModels::HardwareProfile.new.tap do |hw_profile|
hw_profile.vm_size = vm_hash[:vm_size]
Expand All @@ -193,6 +189,7 @@ def initialize_vm(vm_hash)
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])
vm_params.storage_profile.image_reference = define_image(vm_hash[:vhd_path])
sdk.create_or_update_vm(vm_hash[:resource_group], vm_hash[:name], vm_params)
end

Expand Down
3 changes: 1 addition & 2 deletions app/models/foreman_azure_rm/azure_rm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_connection(options = {})
end

def new_vm(args = {})
return AzureRmCompute.new(sdk: sdk) if args.empty? || args[:image_id].nil?
return AzureRmCompute.new(sdk: sdk) if args.empty?
opts = vm_instance_defaults.merge(args.to_h).deep_symbolize_keys
# convert rails nested_attributes into a plain hash
nested_args = opts.delete(:interfaces_attributes)
Expand All @@ -104,7 +104,6 @@ def new_vm(args = {})
platform: opts[:platform],
ssh_key_data: opts[:ssh_key_data],
os_disk_caching: opts[:os_disk_caching],
vhd_path: opts[:image_id],
premium_os_disk: opts[:premium_os_disk]
)
if opts[:interfaces].present?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,6 @@
:required => true,
:label_size => "col-md-2",
:id => 'azure_rm_image_id'
}
} if controller_name != "compute_attributes"
%>
</div>

0 comments on commit e049599

Please sign in to comment.