diff --git a/app/controllers/vms_controller.rb b/app/controllers/vms_controller.rb index 3014d17b..adc17214 100644 --- a/app/controllers/vms_controller.rb +++ b/app/controllers/vms_controller.rb @@ -45,7 +45,7 @@ def edit @sudo_user_ids = @vm.sudo_users.map(&:id) @non_sudo_user_ids = @vm.users.map(&:id) - @description = @vm.ensure_config.description + @configuration = @vm.ensure_config end def update @@ -54,8 +54,8 @@ def update notify_changed_users(@vm.users.map(&:id), info_params[:non_sudo_user_ids].map(&:to_i), false, @vm.name) @vm.sudo_users = info_params[:sudo_user_ids] @vm.users = info_params[:non_sudo_user_ids] - @vm.ensure_config.description = info_params[:description] - unless @vm.config.save + + unless @vm.ensure_config.update config_params flash[:error] = 'Description couldn\'t be saved.' redirect_to edit_vm_path(@vm.name) end @@ -153,10 +153,6 @@ def reset_vm redirect_back(fallback_location: root_path) end - # This controller doesn't use strong parameters - # https://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html - # Because no Active Record model is being wrapped - private def prepare_info_params @@ -189,11 +185,11 @@ def authorize_vm_access end def info_params - params.require(:vm_info).permit(:description, sudo_user_ids: [], non_sudo_user_ids: []) + params.require(:vm_info).permit(sudo_user_ids: [], non_sudo_user_ids: []) end def config_params - params.require(:virtual_machine_config).permit(:ip, :dns) + params.require(:vm_info).permit(:description, :ip, :dns) end def authenticate_root_user_or_admin diff --git a/app/views/vms/edit.html.erb b/app/views/vms/edit.html.erb index 65606498..6688897e 100644 --- a/app/views/vms/edit.html.erb +++ b/app/views/vms/edit.html.erb @@ -1,6 +1,8 @@ +<% require './app/assets/helpers/hart_form_builder' %> +

Edit VM information of <%= @vm.name %>

-<%= form_with(scope: :vm_info, url: { :controller => "vms", :action => "update", id: @vm.name }, method: :patch, local: true) do |form| %> +<%= form_with(scope: :vm_info, url: { :controller => "vms", :action => "update", id: @vm.name }, method: :patch, local: true, builder: HartFormBuilder) do |form| %>
<%= form.label :sudo_user_ids, "Users with sudo rights" %> <%= form.select :sudo_user_ids, @@ -19,9 +21,14 @@
<%= form.label :description, 'Description' %> - <%= form.text_area :description, cols: "40", rows: "4", class: 'form-control', value: @description %> + <%= form.text_area :description, cols: "40", rows: "4", class: 'form-control', value: @configuration.description %>
+ <% if current_user.admin? %> + <%= form.labeled_text_field 'IP', :ip, value: @configuration.ip %> + <%= form.labeled_text_field 'DNS', :dns, value: @configuration.dns %> + <%end %> +
<%= form.submit 'Update VM information', class: "btn btn-primary" %>
diff --git a/spec/views/vms/edit.html.erb_spec.rb b/spec/views/vms/edit.html.erb_spec.rb index 5972b3f6..d320b446 100644 --- a/spec/views/vms/edit.html.erb_spec.rb +++ b/spec/views/vms/edit.html.erb_spec.rb @@ -6,9 +6,20 @@ let(:vm) do v_sphere_vm_mock 'VM', vm_ware_tools: 'toolsInstalled' end + let(:configuration) do + config = double + allow(config).to receive(:description).and_return('hello world') + allow(config).to receive(:ip).and_return('127.0.0.1') + allow(config).to receive(:dns).and_return('8.8.8.8') + config + end + + let(:current_user) { FactoryBot.create :user } before do + sign_in current_user assign(:vm, vm) + assign(:configuration, configuration) render end