diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb index e908f99..3c46da0 100644 --- a/app/controllers/hosts_controller.rb +++ b/app/controllers/hosts_controller.rb @@ -1,57 +1,63 @@ class HostsController < ApplicationController - before_action :fetch_host, only: [:show, :edit, :update, :destroy, :submit_config] + before_action :fetch_host, only: [:show, :edit, :update, :destroy, :submit_config, :revoke] # GET /hosts def new @host = Host.new end # POST /hosts def create @host = Host.new(fetch_params) if @host.save redirect_to host_path @host else render :new end end # GET /hosts/1 def show; end # GET /hosts/1/edit def edit; end # PATCH /hosts/1 def update updates = fetch_params.slice(:port, :password) if updates.present? && @host.update_attributes(updates) @host.recalculate redirect_to host_path @host else render :edit end end # DELETE /hosts/1 def destroy @host.destroy redirect_to root_path end # POST /hosts/1/submit_config def submit_config @host.dispatch_to_bacula redirect_to host_path(@host) end + # DELETE /hosts/1/revoke + def revoke + @host.remove_from_bacula + redirect_to root_path + end + private def fetch_host @host = Host.includes(job_templates: [:fileset, :schedule]).find(params[:id]) end def fetch_params params.require(:host).permit(:fqdn, :port, :password) end end diff --git a/app/views/hosts/_host_details.html.erb b/app/views/hosts/_host_details.html.erb index 46e9add..3bbe717 100644 --- a/app/views/hosts/_host_details.html.erb +++ b/app/views/hosts/_host_details.html.erb @@ -1,50 +1,57 @@
Name <%= @host.name %>
FQDN <%= @host.fqdn %>
FDPort <%= @host.port %>
Password <%= @host.password %>
File Retention <%= @host.file_retention %> days
Job Retention <%= @host.job_retention %> days
Auto Prune <%= @host.auto_prune_human %>
Created <%= I18n.l(@host.created_at, format: :long) %>
<%= link_to 'Edit', edit_host_path(@host), class: "btn btn-primary", role: "button" %>
- <% if @host.ready? %> + <% if @host.needs_dispatch? %>
- <%= link_to 'Send to Bacula', submit_config_host_path(@host), method: :post, + <%= link_to 'Deploy Changes', submit_config_host_path(@host), method: :post, class: 'btn btn-success', role: 'button' %>
<% end %> + <% if @host.needs_revoke? %> +
+ <%= link_to 'Remove From Bacula', revoke_host_path(@host), method: :delete, + class: 'btn btn-danger', role: 'button' %> +
+ <% end %> +
diff --git a/config/routes.rb b/config/routes.rb index 855b79c..1c5a7b3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,20 +1,21 @@ Rails.application.routes.draw do resources :clients, only: [:index, :show] resources :hosts, only: [:new, :create, :show, :edit, :update, :destroy] do member do post :submit_config + delete :revoke end resources :jobs, only: [:new, :create, :show, :edit, :update, :destroy] do member do patch :toggle_enable end end resources :filesets, only: [:show, :new, :create, :destroy] resources :schedules, only: [:show, :new, :edit, :create, :update, :destroy] end root 'clients#index' end diff --git a/spec/controllers/hosts_controller_spec.rb b/spec/controllers/hosts_controller_spec.rb index d738736..a508dc9 100644 --- a/spec/controllers/hosts_controller_spec.rb +++ b/spec/controllers/hosts_controller_spec.rb @@ -1,113 +1,128 @@ require 'spec_helper' describe HostsController do describe 'GET #new' do before { get :new } it 'initializes a host' do expect(assigns(:host)).to be end it 'renders' do expect(response).to render_template(:new) end end describe 'PATCH #update' do let!(:host) { FactoryGirl.create(:host) } context 'with valid params' do let(:params) do { id: host.id, host: { port: 9999, password: 'wrong_pass' } } end it 'updates the host' do expect { patch :update, params }. to change { [host.reload.port, host.reload.password] }. to([9999, 'wrong_pass']) end it 'redirects to host_show' do patch :update, params expect(response).to redirect_to(host_path(host)) end end context 'with fqdn in params' do let(:params) do { id: host.id, host: { fqdn: 'another.host.gr' } } end it 'does not update the host' do expect { patch :update, params }. to_not change { host.reload.fqdn } end it 'renders the edit page' do patch :update, params expect(response).to render_template(:edit) end end end describe 'POST #create' do context 'with valid params' do let(:params) do { host: FactoryGirl.build(:host).attributes.symbolize_keys. slice(:password, :fqdn, :port) } end it 'creates the host' do expect { post :create, params }. to change { Host.count }.by(1) end it 'redirects to root' do post :create, params expect(response).to redirect_to(host_path(Host.last)) end end context 'with invalid params' do let(:params) do { host: FactoryGirl.build(:host).attributes.symbolize_keys. slice(:fqdn, :port) } end before { post :create, params } it 'initializes a host with errors' do expect(assigns(:host)).to be end it 'renders :new' do expect(response).to render_template(:new) end end end describe 'POST #submit_config' do let(:host) { FactoryGirl.create(:host, :configured) } let(:params) { { id: host.id } } it 'redirects to root' do post :submit_config, params expect(response).to redirect_to(host_path(host)) end it 'calls submit_config_to_bacula on host' do Host.any_instance.should_receive(:dispatch_to_bacula) post :submit_config, params end end + + describe 'DELETE #revoke' do + let(:host) { FactoryGirl.create(:host, status: Host::STATUSES[:for_removal]) } + let(:params) { { id: host.id } } + + it 'redirects to root' do + delete :revoke, params + expect(response).to redirect_to(root_path) + end + + it 'calls remove_from_bacula on host' do + Host.any_instance.should_receive(:remove_from_bacula) + delete :revoke, params + end + end end diff --git a/spec/routing/host_routing_spec.rb b/spec/routing/host_routing_spec.rb index 371105e..4b2e28d 100644 --- a/spec/routing/host_routing_spec.rb +++ b/spec/routing/host_routing_spec.rb @@ -1,32 +1,37 @@ require 'spec_helper' describe HostsController do it 'routes GET /hosts/new' do expect(get('/hosts/new')).to route_to(controller: 'hosts', action: 'new') end it 'routes POST /hosts' do expect(post('/hosts')).to route_to(controller: 'hosts', action: 'create') end it 'routes GET /hosts/1' do expect(get('/hosts/1')).to route_to(controller: 'hosts', action: 'show', id: '1') end it 'routes GET /hosts/1/edit' do expect(get('/hosts/1/edit')).to route_to(controller: 'hosts', action: 'edit', id: '1') end it 'routes PUT /hosts/1' do expect(put('/hosts/1')).to route_to(controller: 'hosts', action: 'update', id: '1') end it 'routes DELETE /hosts/1' do expect(delete('/hosts/1')).to route_to(controller: 'hosts', action: 'destroy', id: '1') end it 'routes POST /hosts/1/submit_config' do expect(post('/hosts/1/submit_config')). to route_to(controller: 'hosts', action: 'submit_config', id: '1') end + + it 'routes DELETE /hosts/1/revoke' do + expect(delete('/hosts/1/revoke')). + to route_to(controller: 'hosts', action: 'revoke', id: '1') + end end