From e0495993c253aa6b90b1e7168c46bb93b143c73e Mon Sep 17 00:00:00 2001 From: Aditi Puntambekar Date: Thu, 9 Jan 2020 15:32:45 +0530 Subject: [PATCH] Fixes #28625 - Compute attributes set from CLI not shown on UI (#61) --- .../vm_extensions/managed_vm.rb | 59 +++++++++---------- app/models/foreman_azure_rm/azure_rm.rb | 3 +- .../form/azurerm/_base.html.erb | 2 +- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb b/app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb index 5318dce..6c04acf 100644 --- a/app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb +++ b/app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb @@ -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 @@ -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 @@ -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| @@ -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] @@ -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] @@ -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 diff --git a/app/models/foreman_azure_rm/azure_rm.rb b/app/models/foreman_azure_rm/azure_rm.rb index 664539d..14b4932 100644 --- a/app/models/foreman_azure_rm/azure_rm.rb +++ b/app/models/foreman_azure_rm/azure_rm.rb @@ -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) @@ -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? diff --git a/app/views/compute_resources_vms/form/azurerm/_base.html.erb b/app/views/compute_resources_vms/form/azurerm/_base.html.erb index a80ce16..5579162 100644 --- a/app/views/compute_resources_vms/form/azurerm/_base.html.erb +++ b/app/views/compute_resources_vms/form/azurerm/_base.html.erb @@ -185,6 +185,6 @@ :required => true, :label_size => "col-md-2", :id => 'azure_rm_image_id' - } + } if controller_name != "compute_attributes" %>