Skip to content

Commit

Permalink
Merge 87fa6a6 into 41c4a09
Browse files Browse the repository at this point in the history
  • Loading branch information
BraunTom committed Mar 6, 2019
2 parents 41c4a09 + 87fa6a6 commit 706949c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
14 changes: 5 additions & 9 deletions app/controllers/vms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions app/views/vms/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<% require './app/assets/helpers/hart_form_builder' %>

<h1>Edit VM information of <span style="font-style: italic;"><%= @vm.name %></span></h1>

<%= 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| %>
<div class="field">
<%= form.label :sudo_user_ids, "Users with sudo rights" %>
<%= form.select :sudo_user_ids,
Expand All @@ -19,9 +21,14 @@

<div class="field">
<%= 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 %>
</div>

<% if current_user.admin? %>
<%= form.labeled_text_field 'IP', :ip, value: @configuration.ip %>
<%= form.labeled_text_field 'DNS', :dns, value: @configuration.dns %>
<%end %>

<div class="actions">
<%= form.submit 'Update VM information', class: "btn btn-primary" %>
</div>
Expand Down
11 changes: 11 additions & 0 deletions spec/views/vms/edit.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 706949c

Please sign in to comment.