foreman_snapshot_management 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7507324502c4b729cde6774cf001a3783ac85729317a579c7b9bd94f5f21fefd
4
- data.tar.gz: 8a4c85f4afb3d6ef0e27c204a9dcd72b6d297c7f775bc1f00f36f30b49c77f75
3
+ metadata.gz: 398453ebe0f63f659d1431bc72ffb86f7f911d2d34798ee705e31ecb6d9a6258
4
+ data.tar.gz: 1b87ef1a4f9b1ee6801a6e92374bafdbbe5e186bbe7631c0eb77a0eefec00863
5
5
  SHA512:
6
- metadata.gz: 9e3dc6c2ef546c2ebf8c0945dade24a16cdc27fb0fc8bdd7e73b42e2e4c66869bf1c9769511ea3b53796cbb039f1b975aba40d755447886fa8bbbd0a73251e9d
7
- data.tar.gz: a325a2bc81c54066b4579a50a4622b944bda27c9aedc42c19f7894c6ad90196aa93581dc2b12d5f639b052d8496ca3efac2d1c9013701caba6a3fa79d364d13a
6
+ metadata.gz: 99977335ef42eec7289476e3571206d3542551d22731e2e72dff9027d6aff81aaffb665e692a83cd3674ae422670322b90577531202772e29f305af8914e2c8a
7
+ data.tar.gz: fb935d293dd020528086ec7b55d6226ee8a401b08d0dd94ee73268a00e032200fc48e94f074d13cfed38baed97f516137320beeb20135d4b66d097026d666809
data/README.md CHANGED
@@ -16,6 +16,16 @@ As Hypervisor VMware vSphere is supported.
16
16
 
17
17
  See [How_to_Install_a_Plugin](http://projects.theforeman.org/projects/foreman/wiki/How_to_Install_a_Plugin) for how to install Foreman plugins
18
18
 
19
+ ## Compatibility
20
+
21
+ | Foreman Version | Plugin Version |
22
+ | --------------- | -------------- |
23
+ | 1.17 | >= 1.5.0 |
24
+
25
+ ## Contributing
26
+
27
+ Fork and send a Pull Request. Thanks!
28
+
19
29
  ## Copyright
20
30
  Copyright (c) 2017 ATIX AG - http://www.atix.de
21
31
 
data/Rakefile CHANGED
@@ -20,28 +20,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
20
20
  rdoc.rdoc_files.include('lib/**/*.rb')
21
21
  end
22
22
 
23
- APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
23
+ APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
24
24
 
25
25
  Bundler::GemHelper.install_tasks
26
-
27
- require 'rake/testtask'
28
-
29
- Rake::TestTask.new(:test) do |t|
30
- t.libs << 'lib'
31
- t.libs << 'test'
32
- t.pattern = 'test/**/*_test.rb'
33
- t.verbose = false
34
- end
35
-
36
- task default: :test
37
-
38
- begin
39
- require 'rubocop/rake_task'
40
- RuboCop::RakeTask.new
41
- rescue StandardError => _
42
- puts 'Rubocop not loaded.'
43
- end
44
-
45
- task :default do
46
- Rake::Task['rubocop'].execute
47
- end
@@ -1,9 +1,14 @@
1
1
  module ForemanSnapshotManagement
2
2
  class SnapshotsController < ApplicationController
3
+ include Foreman::Controller::ActionPermissionDsl
3
4
  include ::Foreman::Controller::Parameters::Snapshot
4
5
 
5
- before_action :find_host
6
- before_action :check_snapshot_capability
6
+ MULTIPLE_ACTIONS = %w[select_multiple_host create_multiple_host].freeze
7
+
8
+ before_action :find_hosts, only: MULTIPLE_ACTIONS
9
+ before_action :find_host, except: MULTIPLE_ACTIONS
10
+ before_action :check_snapshot_capability, except: MULTIPLE_ACTIONS
11
+ before_action :check_multiple_snapshot_capability, only: MULTIPLE_ACTIONS
7
12
  before_action :enumerate_snapshots, only: [:index]
8
13
  before_action :find_snapshot, only: %i[destroy revert update]
9
14
  helper_method :xeditable?
@@ -67,8 +72,44 @@ module ForemanSnapshotManagement
67
72
  end
68
73
  end
69
74
 
75
+ define_action_permission ['select_multiple_host', 'create_multiple_host'], :create
76
+ def select_multiple_host; end
77
+
78
+ def create_multiple_host
79
+ data = snapshot_params
80
+ snapshots_created = 0
81
+ errors = []
82
+ @hosts.each do |h|
83
+ s = Snapshot.new(data.merge(host: h))
84
+ if s.create
85
+ snapshots_created += 1
86
+ else
87
+ errors << [h.name, s.errors.full_messages.to_sentence]
88
+ end
89
+ end
90
+ error _('Error occurred while creating Snapshot for<br /><dl>%s</dl>') % errors.map { |e| "<dt>#{e[0]}</dt><dd>#{e[1]}</dd>" }.join('<br />') unless errors.empty?
91
+ if snapshots_created > 0
92
+ msg = _('Created %{snapshots} for %{num} %{hosts}') % {
93
+ snapshots: n_('Snapshot', 'Snapshots', snapshots_created),
94
+ num: snapshots_created,
95
+ hosts: n_('host', 'hosts', snapshots_created)
96
+ }
97
+ # for backwards compatibility
98
+ if respond_to? :success
99
+ success msg
100
+ else
101
+ notice msg
102
+ end
103
+ end
104
+ redirect_back_or_to hosts_path
105
+ end
106
+
70
107
  private
71
108
 
109
+ def snapshot_params
110
+ params.require(:snapshot).permit(:name, :description, :include_ram)
111
+ end
112
+
72
113
  # Find Host
73
114
  #
74
115
  # This method is responsible that methods of the controller know the current host.
@@ -85,6 +126,28 @@ module ForemanSnapshotManagement
85
126
  false
86
127
  end
87
128
 
129
+ def find_hosts
130
+ resource_base = Host.authorized("#{action_permission}_snapshots".to_sym, Host).friendly
131
+
132
+ # Lets search by name or id and make sure one of them exists first
133
+ @hosts = resource_base.search_for(params[:search]) if params.key?(:search)
134
+ @hosts ||= resource_base.where('hosts.id IN (?) or hosts.name IN (?)', params[:host_ids], params[:host_names]) if params.key?(:host_names) || params.key?(:host_ids)
135
+
136
+ if @hosts.empty?
137
+ error _('No hosts were found with that id, name or query filter')
138
+ redirect_to(hosts_path)
139
+ return false
140
+ end
141
+
142
+ @hosts
143
+ rescue StandardError => error
144
+ message = _('Something went wrong while selecting hosts - %s') % error
145
+ error(message)
146
+ Foreman::Logging.exception(message, error)
147
+ redirect_to hosts_path
148
+ false
149
+ end
150
+
88
151
  def action_permission
89
152
  case params[:action]
90
153
  when 'revert'
@@ -94,6 +157,16 @@ module ForemanSnapshotManagement
94
157
  end
95
158
  end
96
159
 
160
+ def check_multiple_snapshot_capability
161
+ capable_hosts = @hosts.select { |h| h.capabilities.include?(:snapshots) }
162
+ if capable_hosts.empty?
163
+ warning _('No capable hosts found.')
164
+ @hosts = Host.where('false')
165
+ else
166
+ @hosts = Host.where('hosts.id IN (?)', capable_hosts.map(&:id))
167
+ end
168
+ end
169
+
97
170
  def check_snapshot_capability
98
171
  not_found unless @host.compute_resource && @host.compute_resource.capabilities.include?(:snapshots)
99
172
  end
@@ -0,0 +1,7 @@
1
+ module ForemanSnapshotManagement
2
+ module HostsHelperExtension
3
+ def multiple_actions
4
+ super + [[_('Create Snapshot'), select_multiple_host_snapshots_path]]
5
+ end
6
+ end
7
+ end
@@ -162,6 +162,9 @@ module ForemanSnapshotManagement
162
162
  rescue Foreman::WrappedException => e
163
163
  errors.add(:base, e.wrapped_exception.message)
164
164
  false
165
+ rescue ActiveRecord::RecordInvalid => e
166
+ errors.add(:base, e.message)
167
+ false
165
168
  end
166
169
  end
167
170
  end
@@ -0,0 +1,17 @@
1
+ <%= render 'hosts/selected_hosts', :hosts => @hosts %>
2
+
3
+ <% if @hosts.empty? %>
4
+ <%= _('No capable hosts selected') %>
5
+ <% else %>
6
+ <%= form_for :snapshot, :url => create_multiple_host_snapshots_path(:host_ids => @hosts.map{ | h| h.id}) do |f| %>
7
+ <%= text_f f, :name,
8
+ { :label => _("Snapshot")}
9
+ %>
10
+ <%= textarea_f f, :description,
11
+ { :label => _("Description")}
12
+ %>
13
+ <%= checkbox_f f, :include_ram,
14
+ { :label => _("Include RAM")}
15
+ %>
16
+ <% end %>
17
+ <% end %>
data/config/routes.rb CHANGED
@@ -25,4 +25,10 @@ Rails.application.routes.draw do
25
25
  end
26
26
  end
27
27
  end
28
+ resources :snapshots, module: 'foreman_snapshot_management', only: [] do
29
+ collection do
30
+ post :select_multiple_host
31
+ post :create_multiple_host
32
+ end
33
+ end
28
34
  end
@@ -6,10 +6,11 @@ module ForemanSnapshotManagement
6
6
 
7
7
  config.autoload_paths += Dir["#{config.root}/app/models/concerns"]
8
8
  config.autoload_paths += Dir["#{config.root}/app/controllers/concerns"]
9
+ config.autoload_paths += Dir["#{config.root}/app/helpers/concerns"]
9
10
 
10
11
  initializer 'foreman_snapshot_management.register_plugin', before: :finisher_hook do |_app|
11
12
  Foreman::Plugin.register :foreman_snapshot_management do
12
- requires_foreman '>= 1.14'
13
+ requires_foreman '>= 1.17'
13
14
 
14
15
  apipie_documented_controllers ["#{ForemanSnapshotManagement::Engine.root}/app/controllers/api/v2/*.rb"]
15
16
 
@@ -74,6 +75,7 @@ module ForemanSnapshotManagement
74
75
  begin
75
76
  # Load Foreman extensions
76
77
  ::Foreman::Model::Vmware.send(:prepend, ForemanSnapshotManagement::VmwareExtensions)
78
+ ::HostsHelper.send(:prepend, ForemanSnapshotManagement::HostsHelperExtension)
77
79
 
78
80
  # Load Fog extensions
79
81
  if Foreman::Model::Vmware.available?
@@ -92,7 +94,7 @@ module ForemanSnapshotManagement
92
94
  end
93
95
 
94
96
  initializer 'foreman_snapshot_management.register_gettext', after: :load_config_initializers do |_app|
95
- locale_dir = File.join(File.expand_path('../../..', __FILE__), 'locale')
97
+ locale_dir = File.join(File.expand_path('../..', __dir__), 'locale')
96
98
  locale_domain = 'foreman_snapshot_management'
97
99
  Foreman::Gettext::Support.add_text_domain locale_domain, locale_dir
98
100
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanSnapshotManagement
2
- VERSION = '1.4.0'.freeze
2
+ VERSION = '1.5.0'.freeze
3
3
  end
@@ -7,7 +7,9 @@ module ForemanSnapshotManagement
7
7
  ComputeResource.find_by_id(cr.id)
8
8
  end
9
9
  let(:uuid) { '5032c8a5-9c5e-ba7a-3804-832a03e16381' }
10
+ let(:uuid2) { 'a7169e20-74d3-4367-afc2-d355716e7555' }
10
11
  let(:host) { FactoryBot.create(:host, :managed, :compute_resource => compute_resource, :uuid => uuid) }
12
+ let(:host2) { FactoryBot.create(:host, :managed, :compute_resource => compute_resource, :uuid => uuid2) }
11
13
  let(:snapshot_id) { 'snapshot-0101' }
12
14
  setup { ::Fog.mock! }
13
15
  teardown { ::Fog.unmock! }
@@ -25,7 +27,13 @@ module ForemanSnapshotManagement
25
27
  test 'create valid' do
26
28
  post :create, params: { :host_id => host.to_param, :snapshot => { :name => 'test' } }, session: set_session_user
27
29
  assert_redirected_to host_url(host, :anchor => 'snapshots')
28
- assert_includes flash[:notice], 'Successfully created Snapshot.'
30
+ assert_includes flash[:notice] || flash[:success], 'Successfully created Snapshot.'
31
+ end
32
+
33
+ test 'create valid multiple' do
34
+ post :create_multiple_host, params: { :host_ids => [host.id, host2.id], :snapshot => { :name => 'test' } }, session: set_session_user
35
+ assert_redirected_to hosts_url
36
+ assert_includes flash[:notice] || flash[:success], 'Created Snapshots for 2 hosts'
29
37
  end
30
38
 
31
39
  test 'create invalid' do
@@ -34,13 +42,20 @@ module ForemanSnapshotManagement
34
42
  assert_redirected_to host_url(host, :anchor => 'snapshots')
35
43
  assert_includes flash[:error], 'Error occurred while creating Snapshot'
36
44
  end
45
+
46
+ test 'create invalid multiple' do
47
+ ForemanSnapshotManagement::Snapshot.any_instance.stubs(:create).returns(false)
48
+ post :create_multiple_host, params: { :host_ids => [host.id, host2.id], :snapshot => { :name => nil } }, session: set_session_user
49
+ assert_redirected_to hosts_url
50
+ assert_match(/^Error occurred while creating Snapshot for/, flash[:error])
51
+ end
37
52
  end
38
53
 
39
54
  context 'DELETE #destroy' do
40
55
  test 'destroy successful' do
41
56
  delete :destroy, params: { :host_id => host.to_param, :id => snapshot_id }, session: set_session_user
42
57
  assert_redirected_to host_url(host, :anchor => 'snapshots')
43
- assert_includes flash[:notice], 'Successfully deleted Snapshot.'
58
+ assert_includes flash[:notice] || flash[:success], 'Successfully deleted Snapshot.'
44
59
  end
45
60
 
46
61
  test 'destroy with error' do
@@ -55,7 +70,7 @@ module ForemanSnapshotManagement
55
70
  test 'revert successful' do
56
71
  put :revert, params: { :host_id => host.to_param, :id => snapshot_id }, session: set_session_user
57
72
  assert_redirected_to host_url(host, :anchor => 'snapshots')
58
- assert_includes flash[:notice], 'VM successfully rolled back.'
73
+ assert_includes flash[:notice] || flash[:success], 'VM successfully rolled back.'
59
74
  end
60
75
 
61
76
  test 'revert with error' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_snapshot_management
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ATIX AG
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-19 00:00:00.000000000 Z
11
+ date: 2018-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deface
@@ -65,6 +65,7 @@ files:
65
65
  - app/controllers/api/v2/snapshots_controller.rb
66
66
  - app/controllers/concerns/foreman/controller/parameters/snapshot.rb
67
67
  - app/controllers/foreman_snapshot_management/snapshots_controller.rb
68
+ - app/helpers/concerns/foreman_snapshot_management/hosts_helper_extension.rb
68
69
  - app/helpers/foreman_snapshot_management/snapshot_helper.rb
69
70
  - app/models/concerns/fog_extensions/vsphere/snapshots/mock.rb
70
71
  - app/models/concerns/fog_extensions/vsphere/snapshots/real.rb
@@ -82,6 +83,7 @@ files:
82
83
  - app/views/foreman_snapshot_management/hosts/_snapshots_tab.html.erb
83
84
  - app/views/foreman_snapshot_management/hosts/_snapshots_tab_content.html.erb
84
85
  - app/views/foreman_snapshot_management/snapshots/_index.html.erb
86
+ - app/views/foreman_snapshot_management/snapshots/select_multiple_host.html.erb
85
87
  - config/routes.rb
86
88
  - lib/foreman_snapshot_management.rb
87
89
  - lib/foreman_snapshot_management/engine.rb