From 4f96ba512f93abd181283b9a65d4873948a0d21f Mon Sep 17 00:00:00 2001 From: jaisejose1123 Date: Mon, 25 Mar 2024 16:30:54 +0530 Subject: [PATCH] API for vm_infras in reconfigure form from manageiq-API under Vm Controller --- .../api/base_controller/generic.rb | 4 +- app/controllers/api/vms_controller.rb | 93 +++++++++++++++++++ config/api.yml | 4 + spec/requests/authentications_spec.rb | 2 +- 4 files changed, 101 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/base_controller/generic.rb b/app/controllers/api/base_controller/generic.rb index 47808cd78d..093396684d 100644 --- a/app/controllers/api/base_controller/generic.rb +++ b/app/controllers/api/base_controller/generic.rb @@ -246,9 +246,11 @@ def model_ident(model, type = nil) end def validate_id(id, key_id, klass) + if id.nil? || (key_id == "id" && !id.integer?) - raise BadRequestError, "Invalid #{klass} #{key_id} #{id || "nil"} specified" + raise BadRequestError, "Invalid #{klass} #{key_id} #{id || "nil"} specified" end + end end end diff --git a/app/controllers/api/vms_controller.rb b/app/controllers/api/vms_controller.rb index 5c6de7d691..b785d7ec88 100644 --- a/app/controllers/api/vms_controller.rb +++ b/app/controllers/api/vms_controller.rb @@ -53,6 +53,99 @@ def edit_resource(type, id, data) def delete_resource(type, id, _data = nil) enqueue_ems_action(type, id, "Deleting", :method_name => "destroy") end + + def vm_reconfigure_resource(type, id, data) + + new_row = { + :id => id.to_s, + :long_id => id.to_s, + :cells => [] + } + + unless id + data_spec = data.collect { |key, val| "#{key}=#{val}" }.join(", ") + raise NotFoundError, "Invalid #{type} resource specified - #{data_spec}" + end + resource = resource_search(id, type) + new_row[:tree_id] = TreeBuilder.build_node_id(resource) if resource + new_row[:cells] << {:is_checkbox => true} + new_row[:cells] << {:name => resource.name} + new_row[:cells] << {:hostname => resource&.host&.name} + new_row[:cells] << {:num_cpu => resource.num_cpu} + new_row[:cells] << {:cpu_cores_per_socket => resource.cpu_cores_per_socket} + new_row[:cells] << {:mem_cpu => "#{(resource.mem_cpu.to_i / 1024.0).to_i}GB"} + + opts = { + :name => type.to_s, + :is_subcollection => false, + :expand_resources => true, + :expand_actions => true, + :expand_custom_actions => true + } + resource_to_jbuilder(type, type, resource, opts).attributes! + end + + def vm_reconfigure_single_resource(type, id, data) + new_row = { + :id => id.to_s, + :long_id => id.to_s, + :cells => [], + :network_adapters => [], + :disks => [] + } + + unless id + data_spec = data.collect { |key, val| "#{key}=#{val}" }.join(", ") + raise NotFoundError, "Invalid #{type} resource specified - #{data_spec}" + end + resource = resource_search(id, type) + + new_row[:tree_id] = TreeBuilder.build_node_id(resource) if resource + new_row[:cells] << {:is_checkbox => true} + new_row[:cells] << {:name => resource.name} + new_row[:cells] << {:hostname => resource&.host&.name} + new_row[:cells] << {:num_cpu => resource.num_cpu} + new_row[:cells] << {:cpu_cores_per_socket => resource.cpu_cores_per_socket} + new_row[:cells] << {:mem_cpu => "#{(resource.mem_cpu.to_i / 1024.0).to_i}GB"} + + resource.hardware.guest_devices.order(:device_name => 'asc').each do |guest_device| + lan = Lan.find_by(:id => guest_device.lan_id) + new_row[:network_adapters] << {:name => guest_device.device_name, :vlan => lan.name, :mac => guest_device.address, :add_remove => ''} unless lan.nil? + end + + resource.hardware.disks.order(:filename).each do |disk| + next if disk.device_type != 'disk' + dsize, dunit = reconfigure_calculations(disk.size / (1024 * 1024)) + new_row[:disks] << {:hdFilename => disk.filename, + :hdType => disk.disk_type, + :hdMode => disk.mode, + :hdSize => dsize, + :hdUnit => dunit, + :add_remove => '', + :cb_bootable => disk.bootable} + + + end + + opts = { + :name => type.to_s, + :is_subcollection => false, + :expand_resources => true, + :expand_actions => true, + :expand_custom_actions => true + } + resource_to_jbuilder(type, type, resource, opts).attributes! + end + + def reconfigure_calculations(mbsize) + humansize = mbsize + fmt = "MB" + if mbsize.to_i > 1024 && (mbsize.to_i % 1024).zero? + humansize = mbsize.to_i / 1024 + fmt = "GB" + end + return humansize.to_s, fmt + end def set_owner_resource(type, id = nil, data = nil) api_resource(type, id, "Setting owner of") do |vm| diff --git a/config/api.yml b/config/api.yml index 4ff601c747..46aa694638 100644 --- a/config/api.yml +++ b/config/api.yml @@ -4659,6 +4659,10 @@ :identifier: - vm_show_list - sui_vm_details_view + - :name: vm_reconfigure + :identifier: vm_infras + - :name: vm_reconfigure_single + :identifier: vm_infras_single - :name: edit :identifier: vm_edit - :name: add_event diff --git a/spec/requests/authentications_spec.rb b/spec/requests/authentications_spec.rb index 2641f4b494..fbc4ff5496 100644 --- a/spec/requests/authentications_spec.rb +++ b/spec/requests/authentications_spec.rb @@ -489,4 +489,4 @@ def credential_types end end.deep_stringify_keys end -end +end \ No newline at end of file