foreman_expire_hosts 2.1.0 → 2.1.1

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
  SHA1:
3
- metadata.gz: dff569ea01095e51ec33f834ac418f0727562f74
4
- data.tar.gz: 0ee7ddb292e6c9939363cbc11d1e824b137b30af
3
+ metadata.gz: bf158e1beab470ea6dc171ed30d7e11b7194dcd4
4
+ data.tar.gz: 026d9ba320a50dc94c2cb336e869ce5b22a1b912
5
5
  SHA512:
6
- metadata.gz: 6ec7cc032b18d3f485060ecad97a57a2de20355b9ed719380f7a9b425155f18b0aa4f276649bb06e138822fcc3f7a8eb3604180f2b8d4b61dd69564ee0b3ea66
7
- data.tar.gz: 6c62884427e54bc0900810558e346534685ae53942799138e0f97c65353b594ee8a2e4e9fe6d8baac42e4f16324d5aa9886f973e9351828585351e545e2b05ee
6
+ metadata.gz: 777d04912617e0a33c2dc932e3456b73a7b7fbf9d3a4ffee980f5bcba33b1cca574f4a18c7c5c8a74dd9d50cd5aa345bea75403f3043f56940cf7b06f06dff3e
7
+ data.tar.gz: d99b1ac558856087474aa314e433ed54ca584b2411d12ad3c0697465febdc6defd9743ab35a79cb913312b89c1c0bb7f9d5a2d66709b708e5f1dd33d09c51c8c
data/.rubocop_todo.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2016-06-07 15:24:39 +0200 using RuboCop version 0.39.0.
3
+ # on 2016-10-20 20:55:41 +0200 using RuboCop version 0.42.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -8,7 +8,7 @@
8
8
 
9
9
  # Offense count: 4
10
10
  # Cop supports --auto-correct.
11
- # Configuration parameters: IgnoreEmptyBlocks.
11
+ # Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments.
12
12
  Lint/UnusedBlockArgument:
13
13
  Exclude:
14
14
  - 'lib/expire_hosts_notifications.rb'
@@ -32,9 +32,9 @@ Metrics/AbcSize:
32
32
 
33
33
  # Offense count: 3
34
34
  Metrics/CyclomaticComplexity:
35
- Max: 9
35
+ Max: 11
36
36
 
37
- # Offense count: 69
37
+ # Offense count: 86
38
38
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
39
39
  # URISchemes: http, https
40
40
  Metrics/LineLength:
@@ -43,11 +43,16 @@ Metrics/LineLength:
43
43
  # Offense count: 1
44
44
  # Configuration parameters: CountComments.
45
45
  Metrics/ModuleLength:
46
- Max: 111
46
+ Max: 110
47
47
 
48
48
  # Offense count: 3
49
49
  Metrics/PerceivedComplexity:
50
- Max: 9
50
+ Max: 11
51
+
52
+ # Offense count: 2
53
+ Rails/OutputSafety:
54
+ Exclude:
55
+ - 'app/helpers/concerns/foreman_expire_hosts/audits_helper_extensions.rb'
51
56
 
52
57
  # Offense count: 4
53
58
  # Cop supports --auto-correct.
@@ -59,16 +64,21 @@ Style/EmptyLinesAroundClassBody:
59
64
  - 'app/models/setting/expire_hosts.rb'
60
65
  - 'lib/expire_hosts_notifications.rb'
61
66
 
62
- # Offense count: 69
67
+ # Offense count: 105
63
68
  # Cop supports --auto-correct.
64
- # Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues.
69
+ # Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
65
70
  # SupportedStyles: ruby19, ruby19_no_mixed_keys, hash_rockets
66
71
  Style/HashSyntax:
67
72
  Enabled: false
68
73
 
69
- # Offense count: 8
74
+ # Offense count: 12
70
75
  # Cop supports --auto-correct.
71
76
  # Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SupportedStyles.
72
- # SupportedStyles: space, no_space
77
+ # SupportedStyles: space, no_space, compact
73
78
  Style/SpaceInsideHashLiteralBraces:
74
- Enabled: false
79
+ Exclude:
80
+ - 'app/controllers/concerns/foreman_expire_hosts/host_controller_extensions.rb'
81
+ - 'app/helpers/concerns/foreman_expire_hosts/hosts_helper_extensions.rb'
82
+ - 'lib/expire_hosts_notifications.rb'
83
+ - 'lib/foreman_expire_hosts/engine.rb'
84
+ - 'test/functional/concerns/hosts_controller_extensions_test.rb'
data/README.md CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  # Context
4
4
 
5
- Foreman makes host creation extremely simple for a sysadmin or a user. However this simplicity leads to wasteful usage of compute resources. foreman_expiry plugin allows to specify an expiry date for the host. On this date the host will be deleted.
5
+ Foreman makes host creation extremely simple for a sysadmin or a user. However this simplicity leads to wasteful usage of compute resources. The foreman_expire_hosts plugin allows to specify an expiry date for the host. The host will be shutdown first and then automatically deleted after a grace period.
6
6
 
7
- This plugin add expired on(date) field to host form under Additional Information section. If we create any host with expiry date, then that host will be stopped on given date and then deleted. If a host has no expiration date set then that host will live forever (until it is deleted manually).
7
+ This plugin adds an expired on (date) field to the host form (Additional Information tab). If we create a host with expiry date, then that host will be stopped on the day after the given date and then deleted. If a host has no expiration date set then that host will live forever (until it is deleted manually).
8
8
 
9
- This plugin will send two warning notification before host expiry (see Settings). It also sends notifications when the host stopped on its expiry date and when host is deleted after few days (configured in settings).
9
+ This plugin will send two warning notifications before host expiry (see Settings). It also sends notifications when the host is stopped on its expiry date and when the host is deleted after the grace period (configured in settings).
10
10
 
11
11
  ## Compatibility
12
12
 
@@ -24,18 +24,11 @@ This plugin will send two warning notification before host expiry (see Settings)
24
24
 
25
25
 
26
26
  # Installation
27
- Please see the Foreman manual for appropriate instructions:
28
-
29
- * [Foreman: How to Install a Plugin](http://theforeman.org/manuals/latest/index.html#6.Plugins)
30
-
31
- The gem name is "foreman_expire_hosts".
32
-
33
- RPM users can install the "tfm-rubygem-foreman_expire_hosts" or
34
- "rubygem-foreman_expire_hosts" packages.
27
+ See [Plugins install instructions](https://theforeman.org/plugins/) for how to install Foreman plugins.
28
+ TLDR: You need to install the package `tfm-rubygem-foreman_expire_hosts` on RPM-based systems or use foreman-installer.
35
29
 
36
30
  This plugin has not been packeged for Debian, yet.
37
31
 
38
-
39
32
  # Post installation
40
33
 
41
34
  This plugin needs additional column in hosts table. Please run migration with below command
@@ -37,7 +37,7 @@ module ForemanExpireHosts
37
37
 
38
38
  def datepicker_f(f, attr, options = {})
39
39
  field(f, attr, options) do
40
- addClass options, "form-control"
40
+ addClass options, 'form-control'
41
41
  date = f.text_field attr, options
42
42
  addon = input_group_addon('', :class => 'glyphicon glyphicon-calendar')
43
43
  input_group date, addon
@@ -35,7 +35,7 @@ module ForemanExpireHosts
35
35
  errors.add(:expired_on, _('no permission to edit'))
36
36
  end
37
37
  end
38
- errors[:expired_on].empty?
38
+ true
39
39
  end
40
40
 
41
41
  def expires?
@@ -73,7 +73,14 @@ module ForemanExpireHosts
73
73
  end
74
74
 
75
75
  def can_modify_expiry_date?
76
- (new_record? || (defined?(Rails::Console) || (User.current && (User.current.admin || (Setting[:can_owner_modify_host_expiry_date] || ((owner_type == 'User' && owner.id == User.current.id) || (owner_type == 'Usergroup' && owner.users.map(&:id).include?(User.current.id))))))))
76
+ return true if new_record?
77
+ return true if defined?(Rails::Console)
78
+ return true unless User.current
79
+ return true if User.current.admin?
80
+ return true if self.owner_type.nil? || self.owner.nil?
81
+ Setting[:can_owner_modify_host_expiry_date] &&
82
+ ((self.owner_type == 'User' && self.owner == User.current) ||
83
+ (self.owner_type == 'Usergroup' && self.owner.users.include?(User.current)))
77
84
  end
78
85
 
79
86
  private
@@ -12,7 +12,7 @@ module ExpireHostsNotifications
12
12
  def catch_delivery_errors(message, hosts = [])
13
13
  yield
14
14
  rescue => error
15
- message = _("%{message} for Hosts %{hosts}") % {:message => message, :hosts => hosts.map(&:name).to_sentence}
15
+ message = _('%{message} for Hosts %{hosts}') % {:message => message, :hosts => hosts.map(&:name).to_sentence}
16
16
  Foreman::Logging.exception(message, error)
17
17
  end
18
18
 
@@ -80,11 +80,10 @@ module ExpireHostsNotifications
80
80
  days_before_expiry = Setting["notify#{num}_days_before_host_expiry"].to_i
81
81
  expiry_date = (Date.today + days_before_expiry)
82
82
  notifiable_hosts = Host.with_expire_date(expiry_date)
83
- unless notifiable_hosts.empty?
84
- hosts_by_user(notifiable_hosts).each do |user_id, hosts_hash|
85
- catch_delivery_errors(_('Failed to deliver expiring hosts notification'), notifiable_hosts) do
86
- ExpireHostsMailer.expiry_warning_notification(hosts_hash['email'], expiry_date, hosts_hash['hosts']).deliver_now
87
- end
83
+ return if notifiable_hosts.empty?
84
+ hosts_by_user(notifiable_hosts).each do |user_id, hosts_hash|
85
+ catch_delivery_errors(_('Failed to deliver expiring hosts notification'), notifiable_hosts) do
86
+ ExpireHostsMailer.expiry_warning_notification(hosts_hash['email'], expiry_date, hosts_hash['hosts']).deliver_now
88
87
  end
89
88
  end
90
89
  end
@@ -27,7 +27,7 @@ module ForemanExpireHosts
27
27
  register_custom_status HostStatus::ExpirationStatus
28
28
 
29
29
  # strong parameters
30
- parameter_filter Host::Managed, :expires_on
30
+ parameter_filter Host::Managed, :expired_on
31
31
 
32
32
  security_block :hosts do
33
33
  permission :edit_hosts, {:hosts => [:select_multiple_expiration, :update_multiple_expiration]}
@@ -1,3 +1,3 @@
1
1
  module ForemanExpireHosts
2
- VERSION = '2.1.0'.freeze
2
+ VERSION = '2.1.1'.freeze
3
3
  end
@@ -2,7 +2,7 @@ FactoryGirl.modify do
2
2
  factory :host do
3
3
  trait :without_validation do
4
4
  # Turn off validation so we can create a host with an expiry date in the past
5
- to_create {|instance| instance.save(validate: false) }
5
+ to_create { |instance| instance.save(validate: false) }
6
6
  end
7
7
 
8
8
  trait :expires_in_a_week do
@@ -2,9 +2,54 @@ require 'test_plugin_helper'
2
2
 
3
3
  class HostsControllerTest < ActionController::TestCase
4
4
  setup do
5
+ User.current = FactoryGirl.build(:user, :admin)
6
+ disable_orchestration
5
7
  setup_settings
6
8
  end
7
9
 
10
+ describe 'host creation' do
11
+ test 'new host with expiration date' do
12
+ expiration_date = Date.today + 14
13
+ assert_difference 'Host.count' do
14
+ post :create, {
15
+ :host => {
16
+ :name => 'myotherfullhost',
17
+ :mac => 'aabbecddee06',
18
+ :ip => '2.3.4.125',
19
+ :domain_id => domains(:mydomain).id,
20
+ :operatingsystem_id => operatingsystems(:redhat).id,
21
+ :architecture_id => architectures(:x86_64).id,
22
+ :environment_id => environments(:production).id,
23
+ :subnet_id => subnets(:one).id,
24
+ :medium_id => media(:one).id,
25
+ :pxe_loader => 'Grub2 UEFI',
26
+ :realm_id => realms(:myrealm).id,
27
+ :disk => 'empty partition',
28
+ :puppet_proxy_id => smart_proxies(:puppetmaster).id,
29
+ :root_pass => 'xybxa6JUkz63w',
30
+ :location_id => taxonomies(:location1).id,
31
+ :organization_id => taxonomies(:organization1).id,
32
+ :expired_on => expiration_date
33
+ }
34
+ }, set_session_user
35
+ end
36
+ h = Host.search_for('myotherfullhost').first
37
+ assert_equal expiration_date, h.expired_on
38
+ assert_redirected_to host_url(assigns['host'])
39
+ end
40
+ end
41
+
42
+ describe 'updating a host' do
43
+ let(:host) { FactoryGirl.create(:host) }
44
+
45
+ test 'should add expiration date to host' do
46
+ expiration_date = Date.today + 14
47
+ put :update, { :id => host.name, :host => {:expired_on => expiration_date} }, set_session_user
48
+ h = Host.find(host.id)
49
+ assert_equal expiration_date, h.expired_on
50
+ end
51
+ end
52
+
8
53
  describe 'setting expiration date on multiple hosts' do
9
54
  before do
10
55
  as_admin do
@@ -56,7 +56,7 @@ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
56
56
 
57
57
  test 'should send two notifications for two users' do
58
58
  owner2 = FactoryGirl.create(:user, :with_mail)
59
- host2 = FactoryGirl.create(:host, :expires_in_a_week, :owner => owner2)
59
+ FactoryGirl.create(:host, :expires_in_a_week, :owner => owner2)
60
60
  ExpireHostsMailer.expects(:expiry_warning_notification).twice
61
61
  ExpireHostsNotifications.deliver_expiry_warning_notification
62
62
  end
@@ -69,7 +69,7 @@ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
69
69
  hosts = [host1, host2]
70
70
  Foreman::Logging.expects(:exception).with('failure for Hosts Alpha and Bravo', anything)
71
71
  ExpireHostsNotifications.catch_delivery_errors('failure', hosts) do
72
- raise "Test"
72
+ raise 'Test'
73
73
  end
74
74
  end
75
75
  end
@@ -1,7 +1,7 @@
1
1
  require 'test_plugin_helper'
2
2
 
3
3
  class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
4
- EXPIRATION_SCOPES = ['expiring', 'expired', 'expiring_today', 'expired_past_grace_period']
4
+ EXPIRATION_SCOPES = ['expiring', 'expired', 'expiring_today', 'expired_past_grace_period'].freeze
5
5
 
6
6
  setup do
7
7
  User.current = FactoryGirl.build(:user, :admin)
@@ -31,6 +31,41 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
31
31
  end
32
32
  end
33
33
 
34
+ context 'changing expiration date for user owned host' do
35
+ setup do
36
+ @user = FactoryGirl.create(:user)
37
+ @host = FactoryGirl.create(:host, :expired, :owner => @user)
38
+ end
39
+
40
+ test 'admin should be able to change expiration date' do
41
+ @host.expired_on = Date.today + 5
42
+ assert_valid @host
43
+ end
44
+
45
+ test 'user should not be able to change expiration date' do
46
+ as_user FactoryGirl.build(:user) do
47
+ @host.expired_on = Date.today + 5
48
+ refute_valid @host
49
+ end
50
+ end
51
+
52
+ test 'owner should not be able to change expiration date if forbidden in settings' do
53
+ Setting[:can_owner_modify_host_expiry_date] = false
54
+ as_user @user do
55
+ @host.expired_on = Date.today + 5
56
+ refute_valid @host
57
+ end
58
+ end
59
+
60
+ test 'owner should be able to change expiration date if allowed in settings' do
61
+ Setting[:can_owner_modify_host_expiry_date] = true
62
+ as_user @user do
63
+ @host.expired_on = Date.today + 5
64
+ assert_valid @host
65
+ end
66
+ end
67
+ end
68
+
34
69
  context 'a host without expiration' do
35
70
  setup do
36
71
  @host = FactoryGirl.build(:host)
@@ -155,12 +190,12 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
155
190
  private
156
191
 
157
192
  def exists_only_in_scopes(host, valid_scopes)
158
- host.save(validate: false)
159
- (EXPIRATION_SCOPES - valid_scopes).each do |scope|
160
- refute Host::Managed.send(scope).exists?(host.id), "Host should not exist in #{scope} scope"
161
- end
162
- valid_scopes.each do |scope|
163
- assert Host::Managed.send(scope).exists?(host.id), "Host should exist in #{scope} scope"
164
- end
193
+ host.save(validate: false)
194
+ (EXPIRATION_SCOPES - valid_scopes).each do |scope|
195
+ refute Host::Managed.send(scope).exists?(host.id), "Host should not exist in #{scope} scope"
196
+ end
197
+ valid_scopes.each do |scope|
198
+ assert Host::Managed.send(scope).exists?(host.id), "Host should exist in #{scope} scope"
199
+ end
165
200
  end
166
201
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_expire_hosts
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nagarjuna Rachaneni
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-10-20 00:00:00.000000000 Z
12
+ date: 2016-10-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: deface