From 96bf42f22b9e645f2f4def434132e206460ed115 Mon Sep 17 00:00:00 2001 From: Tom Braun Date: Wed, 27 Feb 2019 19:04:21 +0100 Subject: [PATCH 1/5] admins can configure ip and dns of vms --- app/controllers/vms_controller.rb | 6 +++++- app/views/vms/edit.html.erb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/controllers/vms_controller.rb b/app/controllers/vms_controller.rb index 3014d17b..19677da9 100644 --- a/app/controllers/vms_controller.rb +++ b/app/controllers/vms_controller.rb @@ -46,6 +46,8 @@ def edit @sudo_user_ids = @vm.sudo_users.map(&:id) @non_sudo_user_ids = @vm.users.map(&:id) @description = @vm.ensure_config.description + @ip = @vm.ensure_config.ip + @dns = @vm.ensure_config.dns end def update @@ -55,6 +57,8 @@ def update @vm.sudo_users = info_params[:sudo_user_ids] @vm.users = info_params[:non_sudo_user_ids] @vm.ensure_config.description = info_params[:description] + @ip = @vm.ensure_config.ip = info_params[:ip] + @dns = @vm.ensure_config.dns = info_params[:dns] unless @vm.config.save flash[:error] = 'Description couldn\'t be saved.' redirect_to edit_vm_path(@vm.name) @@ -189,7 +193,7 @@ 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(:description, :ip, :dns, sudo_user_ids: [], non_sudo_user_ids: []) end def config_params diff --git a/app/views/vms/edit.html.erb b/app/views/vms/edit.html.erb index 65606498..7533f9b3 100644 --- a/app/views/vms/edit.html.erb +++ b/app/views/vms/edit.html.erb @@ -22,6 +22,18 @@ <%= form.text_area :description, cols: "40", rows: "4", class: 'form-control', value: @description %> + <% if current_user.admin? %> +
+ <%= form.label :ip, "IP" %> + <%= form.text_field :ip, class: 'form-control', value: @ip %> +
+ +
+ <%= form.label :dns, "DNS" %> + <%= form.text_field :dns, class: 'form-control', value: @dns %> +
+ <%end %> +
<%= form.submit 'Update VM information', class: "btn btn-primary" %>
From a33a354115de6a981e0d7e6a1fb5275806f21cde Mon Sep 17 00:00:00 2001 From: Tom Braun Date: Wed, 27 Feb 2019 22:23:20 +0100 Subject: [PATCH 2/5] fix tests --- spec/views/vms/edit.html.erb_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/views/vms/edit.html.erb_spec.rb b/spec/views/vms/edit.html.erb_spec.rb index 5972b3f6..77fff822 100644 --- a/spec/views/vms/edit.html.erb_spec.rb +++ b/spec/views/vms/edit.html.erb_spec.rb @@ -7,7 +7,10 @@ v_sphere_vm_mock 'VM', vm_ware_tools: 'toolsInstalled' end + let(:current_user) { FactoryBot.create :user } + before do + sign_in current_user assign(:vm, vm) render end From 989dfffa06182dfa1ef52ada4d94f7572bec020a Mon Sep 17 00:00:00 2001 From: Tom Braun Date: Thu, 28 Feb 2019 19:34:58 +0100 Subject: [PATCH 3/5] use hart formbuilder --- app/views/vms/edit.html.erb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/app/views/vms/edit.html.erb b/app/views/vms/edit.html.erb index 7533f9b3..a732699c 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, @@ -23,15 +25,8 @@
<% if current_user.admin? %> -
- <%= form.label :ip, "IP" %> - <%= form.text_field :ip, class: 'form-control', value: @ip %> -
- -
- <%= form.label :dns, "DNS" %> - <%= form.text_field :dns, class: 'form-control', value: @dns %> -
+ <%= form.labeled_text_field 'IP', :ip, value: @ip %> + <%= form.labeled_text_field 'DNS', :dns, value: @dns %> <%end %>
From 6f80fb6eed1c54d92d681596716c4178a8ce3751 Mon Sep 17 00:00:00 2001 From: Tom Braun Date: Fri, 1 Mar 2019 12:03:11 +0100 Subject: [PATCH 4/5] applies requested changes --- app/controllers/vms_controller.rb | 12 ++++-------- app/views/vms/edit.html.erb | 6 +++--- spec/views/vms/edit.html.erb_spec.rb | 8 ++++++++ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/controllers/vms_controller.rb b/app/controllers/vms_controller.rb index 19677da9..c6d4068e 100644 --- a/app/controllers/vms_controller.rb +++ b/app/controllers/vms_controller.rb @@ -45,9 +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 - @ip = @vm.ensure_config.ip - @dns = @vm.ensure_config.dns + @configuration = @vm.ensure_config end def update @@ -56,9 +54,7 @@ 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] - @ip = @vm.ensure_config.ip = info_params[:ip] - @dns = @vm.ensure_config.dns = info_params[:dns] + @vm.ensure_config.update config_params unless @vm.config.save flash[:error] = 'Description couldn\'t be saved.' redirect_to edit_vm_path(@vm.name) @@ -193,11 +189,11 @@ def authorize_vm_access end def info_params - params.require(:vm_info).permit(:description, :ip, :dns, 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 a732699c..6688897e 100644 --- a/app/views/vms/edit.html.erb +++ b/app/views/vms/edit.html.erb @@ -21,12 +21,12 @@
<%= 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: @ip %> - <%= form.labeled_text_field 'DNS', :dns, value: @dns %> + <%= form.labeled_text_field 'IP', :ip, value: @configuration.ip %> + <%= form.labeled_text_field 'DNS', :dns, value: @configuration.dns %> <%end %>
diff --git a/spec/views/vms/edit.html.erb_spec.rb b/spec/views/vms/edit.html.erb_spec.rb index 77fff822..d320b446 100644 --- a/spec/views/vms/edit.html.erb_spec.rb +++ b/spec/views/vms/edit.html.erb_spec.rb @@ -6,12 +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 From e3f8664d7e74265fcd5c1a5967fceb0553942e2e Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Mar 2019 16:13:19 +0100 Subject: [PATCH 5/5] Replace update+save with a single update call --- app/controllers/vms_controller.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app/controllers/vms_controller.rb b/app/controllers/vms_controller.rb index c6d4068e..adc17214 100644 --- a/app/controllers/vms_controller.rb +++ b/app/controllers/vms_controller.rb @@ -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.update config_params - 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