foreman_snapshot_management 1.3.0 → 1.4.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
- SHA1:
3
- metadata.gz: 92825bf181cfd185b7005b4ae8484a45fb76be22
4
- data.tar.gz: 69df917d45d4c2fee44194a275aad9b3a93b0484
2
+ SHA256:
3
+ metadata.gz: 7507324502c4b729cde6774cf001a3783ac85729317a579c7b9bd94f5f21fefd
4
+ data.tar.gz: 8a4c85f4afb3d6ef0e27c204a9dcd72b6d297c7f775bc1f00f36f30b49c77f75
5
5
  SHA512:
6
- metadata.gz: 1c6fe6bf1cd7b925a20bdd929e519901d193fecbf5817736c4056ffebe2f35bf8c293d313b820cf134bedf8489ccebf1ebd94ba30ff852f62e3ea7b0b9bfbf40
7
- data.tar.gz: a24566a0193ce31c7dc8060cee2d82a6c2ca82a232cdd813d7c7595d2211d378768511093e1084f88e5f7040283ace1cf63b37f49f76febe466c156ed1863c41
6
+ metadata.gz: 9e3dc6c2ef546c2ebf8c0945dade24a16cdc27fb0fc8bdd7e73b42e2e4c66869bf1c9769511ea3b53796cbb039f1b975aba40d755447886fa8bbbd0a73251e9d
7
+ data.tar.gz: a325a2bc81c54066b4579a50a4622b944bda27c9aedc42c19f7894c6ad90196aa93581dc2b12d5f639b052d8496ca3efac2d1c9013701caba6a3fa79d364d13a
data/Rakefile CHANGED
@@ -38,7 +38,7 @@ task default: :test
38
38
  begin
39
39
  require 'rubocop/rake_task'
40
40
  RuboCop::RakeTask.new
41
- rescue => _
41
+ rescue StandardError => _
42
42
  puts 'Rubocop not loaded.'
43
43
  end
44
44
 
@@ -29,10 +29,11 @@ module Api
29
29
 
30
30
  api :POST, '/hosts/:host_id/snapshots/', N_('Create a snapshot')
31
31
  param :host_id, :identifier_dottable, :required => true
32
+ param :include_ram, :bool, :default_value => false, :desc => N_('Whether to include the RAM state in the snapshot')
32
33
  param_group :snapshot, :as => :create
33
34
 
34
35
  def create
35
- @snapshot = resource_class.new(snapshot_params.to_h.merge(host: @nested_obj))
36
+ @snapshot = resource_class.new(snapshot_params.to_h.merge(host: @nested_obj).merge(include_ram: params[:include_ram]))
36
37
  process_response @snapshot.save
37
38
  end
38
39
 
@@ -21,7 +21,7 @@ module ForemanSnapshotManagement
21
21
  #
22
22
  # This method creates a Snapshot with a given name and optional description.
23
23
  def create
24
- @snapshot = Snapshot.new(snapshot_params.merge(host: @host))
24
+ @snapshot = Snapshot.new(snapshot_params.merge(host: @host).merge(include_ram: params[:snapshot][:include_ram]))
25
25
 
26
26
  if @snapshot.create
27
27
  process_success
@@ -67,12 +67,12 @@ module ForemanSnapshotManagement
67
67
  end
68
68
  end
69
69
 
70
+ private
71
+
70
72
  # Find Host
71
73
  #
72
74
  # This method is responsible that methods of the controller know the current host.
73
75
 
74
- private
75
-
76
76
  def find_host
77
77
  host_id = params[:host_id]
78
78
  if host_id.blank?
@@ -80,10 +80,9 @@ module ForemanSnapshotManagement
80
80
  return false
81
81
  end
82
82
  @host = Host.authorized("#{action_permission}_snapshots".to_sym, Host).friendly.find(host_id)
83
- unless @host
84
- not_found
85
- return(false)
86
- end
83
+ return @host if @host
84
+ not_found
85
+ false
87
86
  end
88
87
 
89
88
  def action_permission
@@ -5,8 +5,7 @@ module FogExtensions
5
5
  # Overwrite this to stop infinite recursion
6
6
  # TODO: Add proper test data
7
7
  def list_child_snapshots(_snapshot, _opts = {})
8
- [
9
- ]
8
+ []
10
9
  end
11
10
 
12
11
  def remove_snapshot(options = {})
@@ -7,9 +7,7 @@ module FogExtensions
7
7
  raise ArgumentError, 'snapshot is a required parameter' unless options.key? 'snapshot'
8
8
  raise ArgumentError, 'removeChildren is a required parameter' unless options.key? 'removeChildren'
9
9
 
10
- unless ::Fog::Compute::Vsphere::Snapshot === options['snapshot']
11
- raise ArgumentError, 'snapshot is a required parameter'
12
- end
10
+ raise ArgumentError, 'snapshot is a required parameter' unless ::Fog::Compute::Vsphere::Snapshot === options['snapshot']
13
11
 
14
12
  task = options['snapshot'].mo_ref.RemoveSnapshot_Task(
15
13
  removeChildren: options['removeChildren']
@@ -28,9 +26,7 @@ module FogExtensions
28
26
  raise ArgumentError, 'name is a required parameter' unless options.key? 'name'
29
27
  raise ArgumentError, 'description is a required parameter' unless options.key? 'description'
30
28
 
31
- unless ::Fog::Compute::Vsphere::Snapshot === options['snapshot']
32
- raise ArgumentError, 'snapshot is a required parameter'
33
- end
29
+ raise ArgumentError, 'snapshot is a required parameter' unless ::Fog::Compute::Vsphere::Snapshot === options['snapshot']
34
30
 
35
31
  options['snapshot'].mo_ref.RenameSnapshot(
36
32
  name: options['name'],
@@ -9,11 +9,13 @@ module ForemanSnapshotManagement
9
9
  include ActiveModel::ForbiddenAttributesProtection
10
10
 
11
11
  define_model_callbacks :create, :save, :destroy, :revert
12
- attr_accessor :id, :raw_snapshot, :name, :description, :host_id, :parent, :create_time
13
- define_attribute_methods :name, :description
12
+ attr_accessor :id, :raw_snapshot, :parent
13
+ attr_writer :create_time
14
+ attr_reader :name, :description, :include_ram, :host_id
15
+ define_attribute_methods :name, :description, :include_ram
14
16
 
15
17
  def self.all_for_host(host)
16
- snapshots = host.compute_resource.get_snapshots(host.uuid).map do |raw_snapshot|
18
+ host.compute_resource.get_snapshots(host.uuid).map do |raw_snapshot|
17
19
  new_from_vmware(host, raw_snapshot)
18
20
  end
19
21
  end
@@ -51,8 +53,8 @@ module ForemanSnapshotManagement
51
53
  _('Snapshot')
52
54
  end
53
55
 
54
- def formatted_create_time()
55
- create_time.strftime("%F %H:%M")
56
+ def formatted_create_time
57
+ create_time.strftime('%F %H:%M')
56
58
  end
57
59
 
58
60
  def persisted?
@@ -69,23 +71,26 @@ module ForemanSnapshotManagement
69
71
  @description = value
70
72
  end
71
73
 
74
+ def include_ram=(value)
75
+ raise Exception('Cannot modify include_ram on existing snapshots.') if persisted?
76
+ @include_ram = value
77
+ end
78
+
72
79
  # host accessors
73
80
  def host
74
81
  @host ||= Host.find(@host_id)
75
82
  end
76
83
 
77
84
  def host_id=(host_id)
78
- if @host_id != host_id
79
- @host_id = host_id
80
- @host = nil
81
- end
85
+ return if @host_id == host_id
86
+ @host_id = host_id
87
+ @host = nil
82
88
  end
83
89
 
84
90
  def host=(host)
85
- if @host_id != host.id
86
- @host_id = host.id
87
- @host = host
88
- end
91
+ return if @host_id == host.id
92
+ @host_id = host.id
93
+ @host = host
89
94
  end
90
95
 
91
96
  def create_time
@@ -111,7 +116,7 @@ module ForemanSnapshotManagement
111
116
  handle_snapshot_errors do
112
117
  host.audit_comment = "Create snapshot #{name}"
113
118
  host.save!
114
- host.compute_resource.create_snapshot(host.uuid, name, description)
119
+ host.compute_resource.create_snapshot(host.uuid, name, description, include_ram)
115
120
  changes_applied
116
121
  end
117
122
  end
@@ -8,8 +8,8 @@ module ForemanSnapshotManagement
8
8
  # Create a Snapshot.
9
9
  #
10
10
  # This method creates a Snapshot with a given name and optional description.
11
- def create_snapshot(uuid, name, description)
12
- task = client.vm_take_snapshot('instance_uuid' => uuid, 'name' => name, 'description' => description)
11
+ def create_snapshot(uuid, name, description, include_ram = false)
12
+ task = client.vm_take_snapshot('instance_uuid' => uuid, 'name' => name, 'description' => description, 'memory' => include_ram)
13
13
  task_successful?(task)
14
14
  rescue RbVmomi::Fault => e
15
15
  Foreman::Logging.exception('Error creating VMWare Snapshot', e)
@@ -56,9 +56,7 @@ module ForemanSnapshotManagement
56
56
  snapshot = client.snapshots(server_id: server_id).get(snapshot_id)
57
57
  # Workaround for https://github.com/fog/fog-vsphere/commit/d808255cd19c3d43d3227825f1e0d72d3f6ee6b9
58
58
  # Remove, when fog-vshpere 1.11 lands in foreman
59
- while snapshot && snapshot.ref != snapshot_id
60
- snapshot = snapshot.get_child(snapshot_id)
61
- end
59
+ snapshot = snapshot.get_child(snapshot_id) while snapshot && snapshot.ref != snapshot_id
62
60
  snapshot
63
61
  end
64
62
 
@@ -4,6 +4,7 @@
4
4
  <tr>
5
5
  <th class="col-md-1"><%= _('Snapshot') %></th>
6
6
  <th class="col-md-2"><%= _('Description') %></th>
7
+ <th class="col-md-1"><%= _('Include RAM') %></th>
7
8
  <th class="col-md-1"><%= _('Action') %></th>
8
9
  </tr>
9
10
  </thead>
@@ -17,7 +18,10 @@
17
18
  <%= f.text_field :description, class: 'form-control' %>
18
19
  </td>
19
20
  <td>
20
- <%= f.submit _('Create'), class: 'btn btn-success' %>
21
+ <%= f.check_box :include_ram, class: 'form-control' %>
22
+ </td>
23
+ <td>
24
+ <%= f.submit _('Create'), class: 'btn btn-success', :onclick => "$(this).attr('disabled', 'disabled'); $(this).parents('form').submit();" %>
21
25
  </td>
22
26
  </tr>
23
27
  <% end %>
@@ -38,10 +42,12 @@
38
42
  <%= snapshot.description %>
39
43
  <% end %>
40
44
  </td>
45
+ <td>
46
+ </td>
41
47
  <td>
42
48
  <%= action_buttons(
43
- display_link_if_authorized(_('Rollback'), hash_for_revert_host_snapshot_path(host_id: @host, id: snapshot.id).merge(:auth_object => @host, :permission => :revert_snapshots), method: :put, class: 'btn btn-primary', data: {confirm: _("Are you sure to revert this Snapshot?")}),
44
- display_delete_if_authorized(hash_for_host_snapshot_path(host_id: @host, id: snapshot.id).merge(:auth_object => @host, :permission => :delete_snapshots), data: {confirm: _("Are you sure to delete this Snapshot?")}),
49
+ display_link_if_authorized(_('Rollback'), hash_for_revert_host_snapshot_path(host_id: @host, id: snapshot.id).merge(:auth_object => @host, :permission => :revert_snapshots), method: :put, class: 'btn btn-primary', data: {confirm: _('Are you sure to revert this Snapshot?'), 'disable-with': _('Reverting...')}),
50
+ display_delete_if_authorized(hash_for_host_snapshot_path(host_id: @host, id: snapshot.id).merge(:auth_object => @host, :permission => :destroy_snapshots), data: {confirm: _("Are you sure to delete this Snapshot?"), 'disable-with': _('Deleting...')}),
45
51
  ) %>
46
52
  </td>
47
53
  </tr>
@@ -80,7 +80,7 @@ module ForemanSnapshotManagement
80
80
  Fog::Compute::Vsphere::Real.send(:prepend, FogExtensions::Vsphere::Snapshots::Real)
81
81
  Fog::Compute::Vsphere::Mock.send(:prepend, FogExtensions::Vsphere::Snapshots::Mock)
82
82
  end
83
- rescue => e
83
+ rescue StandardError => e
84
84
  Rails.logger.warn "ForemanSnapshotManagement: skipping engine hook (#{e})"
85
85
  end
86
86
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanSnapshotManagement
2
- VERSION = '1.3.0'.freeze
2
+ VERSION = '1.4.0'.freeze
3
3
  end
@@ -19,7 +19,7 @@ namespace :foreman_snapshot_management do
19
19
  "#{ForemanSnapshotManagement::Engine.root}/lib/**/*.rb",
20
20
  "#{ForemanSnapshotManagement::Engine.root}/test/**/*.rb"]
21
21
  end
22
- rescue
22
+ rescue StandardError
23
23
  puts 'Rubocop not loaded.'
24
24
  end
25
25
 
@@ -30,6 +30,4 @@ end
30
30
  Rake::Task[:test].enhance ['test:foreman_snapshot_management']
31
31
 
32
32
  load 'tasks/jenkins.rake'
33
- if Rake::Task.task_defined?(:'jenkins:unit')
34
- Rake::Task['jenkins:unit'].enhance ['test:foreman_snapshot_management', 'foreman_snapshot_management:rubocop']
35
- end
33
+ Rake::Task['jenkins:unit'].enhance ['test:foreman_snapshot_management', 'foreman_snapshot_management:rubocop'] if Rake::Task.task_defined?(:'jenkins:unit')
@@ -12,7 +12,7 @@ class Api::V2::SnapshotsControllerTest < ActionController::TestCase
12
12
  teardown { ::Fog.unmock! }
13
13
 
14
14
  test 'should get index' do
15
- get :index, { :host_id => host.to_param }
15
+ get :index, params: { :host_id => host.to_param }
16
16
  assert_response :success
17
17
  assert_not_nil assigns(:snapshots)
18
18
  body = ActiveSupport::JSON.decode(@response.body)
@@ -21,7 +21,7 @@ class Api::V2::SnapshotsControllerTest < ActionController::TestCase
21
21
  end
22
22
 
23
23
  test 'should show snapshot' do
24
- get :show, { :host_id => host.to_param, :id => snapshot_id }
24
+ get :show, params: { :host_id => host.to_param, :id => snapshot_id }
25
25
  assert_not_nil assigns(:snapshot)
26
26
  assert_response :success
27
27
  body = ActiveSupport::JSON.decode(@response.body)
@@ -29,29 +29,29 @@ class Api::V2::SnapshotsControllerTest < ActionController::TestCase
29
29
  end
30
30
 
31
31
  test 'should 404 for unknown snapshot' do
32
- get :show, { :host_id => host.to_param, :id => 'does-not-exist' }
32
+ get :show, params: { :host_id => host.to_param, :id => 'does-not-exist' }
33
33
  assert_response :not_found
34
34
  end
35
35
 
36
36
  test 'should create snapshot' do
37
- post :create, { :host_id => host.to_param, :name => 'test' }
37
+ post :create, params: { :host_id => host.to_param, :name => 'test' }
38
38
  assert_response :created
39
39
  assert_not_nil assigns(:snapshot)
40
40
  end
41
41
 
42
42
  test 'should update snapshot' do
43
43
  name = 'test'
44
- put :update, { :host_id => host.to_param, :id => snapshot_id.to_param, :name => name.to_param }
44
+ put :update, params: { :host_id => host.to_param, :id => snapshot_id.to_param, :name => name.to_param }
45
45
  assert_response :success
46
46
  end
47
47
 
48
48
  test 'should destroy snapshot' do
49
- delete :destroy, { :host_id => host.to_param, :id => snapshot_id.to_param }
49
+ delete :destroy, params: { :host_id => host.to_param, :id => snapshot_id.to_param }
50
50
  assert_response :success
51
51
  end
52
52
 
53
53
  test 'should revert snapshot' do
54
- put :revert, { :host_id => host.to_param, :id => snapshot_id.to_param }
54
+ put :revert, params: { :host_id => host.to_param, :id => snapshot_id.to_param }
55
55
  assert_response :success
56
56
  end
57
57
  end
@@ -14,7 +14,7 @@ module ForemanSnapshotManagement
14
14
 
15
15
  context 'GET #index' do
16
16
  test 'should get index' do
17
- get :index, { :host_id => host.to_param }, set_session_user
17
+ get :index, params: { :host_id => host.to_param }, session: set_session_user
18
18
  assert_response :success
19
19
  assert_not_nil assigns(:snapshots)
20
20
  assert_template 'foreman_snapshot_management/snapshots/_index'
@@ -23,14 +23,14 @@ module ForemanSnapshotManagement
23
23
 
24
24
  context 'POST #create' do
25
25
  test 'create valid' do
26
- post :create, { :host_id => host.to_param, :snapshot => { :name => 'test' } }, set_session_user
26
+ post :create, params: { :host_id => host.to_param, :snapshot => { :name => 'test' } }, session: set_session_user
27
27
  assert_redirected_to host_url(host, :anchor => 'snapshots')
28
28
  assert_includes flash[:notice], 'Successfully created Snapshot.'
29
29
  end
30
30
 
31
31
  test 'create invalid' do
32
32
  ForemanSnapshotManagement::Snapshot.any_instance.stubs(:create).returns(false)
33
- post :create, { :host_id => host.to_param, :snapshot => { :name => nil } }, set_session_user
33
+ post :create, params: { :host_id => host.to_param, :snapshot => { :name => nil } }, session: set_session_user
34
34
  assert_redirected_to host_url(host, :anchor => 'snapshots')
35
35
  assert_includes flash[:error], 'Error occurred while creating Snapshot'
36
36
  end
@@ -38,14 +38,14 @@ module ForemanSnapshotManagement
38
38
 
39
39
  context 'DELETE #destroy' do
40
40
  test 'destroy successful' do
41
- delete :destroy, { :host_id => host.to_param, :id => snapshot_id }, set_session_user
41
+ delete :destroy, params: { :host_id => host.to_param, :id => snapshot_id }, session: set_session_user
42
42
  assert_redirected_to host_url(host, :anchor => 'snapshots')
43
43
  assert_includes flash[:notice], 'Successfully deleted Snapshot.'
44
44
  end
45
45
 
46
46
  test 'destroy with error' do
47
47
  ForemanSnapshotManagement::Snapshot.any_instance.stubs(:destroy).returns(false)
48
- delete :destroy, { :host_id => host.to_param, :id => snapshot_id }, set_session_user
48
+ delete :destroy, params: { :host_id => host.to_param, :id => snapshot_id }, session: set_session_user
49
49
  assert_redirected_to host_url(host, :anchor => 'snapshots')
50
50
  assert_includes flash[:error], 'Error occurred while removing Snapshot'
51
51
  end
@@ -53,14 +53,14 @@ module ForemanSnapshotManagement
53
53
 
54
54
  context 'PUT #revert' do
55
55
  test 'revert successful' do
56
- put :revert, { :host_id => host.to_param, :id => snapshot_id }, set_session_user
56
+ put :revert, params: { :host_id => host.to_param, :id => snapshot_id }, session: set_session_user
57
57
  assert_redirected_to host_url(host, :anchor => 'snapshots')
58
58
  assert_includes flash[:notice], 'VM successfully rolled back.'
59
59
  end
60
60
 
61
61
  test 'revert with error' do
62
62
  ForemanSnapshotManagement::Snapshot.any_instance.stubs(:revert).returns(false)
63
- put :revert, { :host_id => host.to_param, :id => snapshot_id }, set_session_user
63
+ put :revert, params: { :host_id => host.to_param, :id => snapshot_id }, session: set_session_user
64
64
  assert_redirected_to host_url(host, :anchor => 'snapshots')
65
65
  assert_includes flash[:error], 'Error occurred while rolling back VM'
66
66
  end
@@ -69,7 +69,7 @@ module ForemanSnapshotManagement
69
69
  context 'PUT #update' do
70
70
  test 'update successful' do
71
71
  data = { 'name' => 'test 2', 'description' => '' }
72
- put :update, { :host_id => host.to_param, :id => snapshot_id, :snapshot => data }, set_session_user
72
+ put :update, params: { :host_id => host.to_param, :id => snapshot_id, :snapshot => data }, session: set_session_user
73
73
  assert_response :success
74
74
  body = ActiveSupport::JSON.decode(@response.body)
75
75
  assert_equal(data, body)
@@ -77,7 +77,7 @@ module ForemanSnapshotManagement
77
77
 
78
78
  test 'update with error' do
79
79
  ForemanSnapshotManagement::Snapshot.any_instance.stubs(:save).returns(false)
80
- put :update, { :host_id => host.to_param, :id => snapshot_id, :snapshot => { :name => 'test 2' } }, set_session_user
80
+ put :update, params: { :host_id => host.to_param, :id => snapshot_id, :snapshot => { :name => 'test 2' } }, session: set_session_user
81
81
  assert_response :unprocessable_entity
82
82
  end
83
83
  end
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.3.0
4
+ version: 1.4.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: 2017-12-15 00:00:00.000000000 Z
11
+ date: 2018-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deface
@@ -25,33 +25,33 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rubocop
28
+ name: rdoc
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.49.1
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 0.49.1
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rdoc
42
+ name: rubocop
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 0.49.1
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 0.49.1
55
55
  description: Foreman-plugin to manage snapshots in a vSphere environment.
56
56
  email:
57
57
  - info@atix.de
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  version: '0'
114
114
  requirements: []
115
115
  rubyforge_project:
116
- rubygems_version: 2.5.2.1
116
+ rubygems_version: 2.7.6
117
117
  signing_key:
118
118
  specification_version: 4
119
119
  summary: Snapshot Management for VMware vSphere