Skip to content

Commit

Permalink
Update rubocop and use theforeman-rubocop gem
Browse files Browse the repository at this point in the history
  • Loading branch information
m-bucher committed Oct 30, 2023
1 parent 33c5eee commit 9f645c3
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 92 deletions.
58 changes: 15 additions & 43 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@

require:
- rubocop-performance
- rubocop-rails
- rubocop-minitest

AllCops:
TargetRubyVersion: 2.5
TargetRailsVersion: 5.2
Exclude:
- 'locale/**/*'
- 'node_modules/**/*'
- 'vendor/**/*'
- '*.spec'
inherit_gem:
theforeman-rubocop:
#- lenient.yml
#- default.yml
- strict.yml

Layout/LineLength:
Max: 190
Expand All @@ -20,7 +12,9 @@ Metrics/AbcSize:
Enabled: false

Metrics/BlockLength:
Enabled: false
Max: 50
Exclude:
- 'test/**/*.rb'

Metrics/ClassLength:
Max: 500
Expand All @@ -31,25 +25,8 @@ Metrics/MethodLength:
Metrics/ModuleLength:
Max: 400

Metrics/ParameterLists:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Naming/AccessorMethodName:
Enabled: false

Naming/FileName:
Exclude:
- 'db/seeds.d/*'

Naming/MethodName:
Exclude:
- 'app/models/concerns/orchestration/*.rb'

Rails:
Enabled: True
Max: 10

Rails/Date:
Exclude:
Expand All @@ -69,32 +46,27 @@ Style/ClassAndModuleChildren:
Style/Documentation:
Enabled: false

Style/EachWithObject:
Enabled: false

Style/FormatString:
Enabled: false
EnforcedStyle: percent

Style/FormatStringToken:
Enabled: false
EnforcedStyle: template

Style/GuardClause:
Enabled: false

# Support both ruby19 and hash_rockets
Style/HashSyntax:
EnforcedStyle: no_mixed_keys

Style/OptionalBooleanParameter:
Enabled: false
SupportedStyles:
- ruby19
- hash_rockets

Style/RedundantSelf:
Enabled: false

Style/RegexpLiteral:
Enabled: false

Style/RescueModifier:
EnforcedStyle: slashes
Enabled: false

Style/StringLiterals:
Expand Down
5 changes: 1 addition & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ source 'https://rubygems.org'
gemspec

gem "rdoc", "~> 6.3.1"
gem "rubocop", "~> 0.89.0"
gem "rubocop-minitest", "~> 0.10.3"
gem "rubocop-performance", "~> 1.8"
gem "rubocop-rails", "~> 2.8"
gem "theforeman-rubocop", "~> 0.1.0"
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ begin
require 'rubocop/rake_task'
RuboCop::RakeTask.new
rescue StandardError => _e
puts 'Rubocop not loaded.'
# 'Rubocop not loaded.'
end

RDoc::Task.new(:rdoc) do |rdoc|
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/api/v2/snapshots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SnapshotsController < V2::BaseController
meta :search => [{ :name => 'name', :type => 'string' }]
def index
if params[:search]
search = params[:search].match(/^\s*name\s*=\s*(\w+)\s*$/) || params[:search].match(/^\s*name\s*=\s*\"([^"]+)\"\s*$/)
search = params[:search].match(/^\s*name\s*=\s*(\w+)\s*$/) || params[:search].match(/^\s*name\s*=\s*"([^"]+)"\s*$/)
raise "Field '#{params[:search]}' not recognized for searching!" unless search

snapshot = resource_class.find_for_host_by_name(@host, search[1])
Expand All @@ -34,7 +34,8 @@ def index
param :host_id, :identifier_dottable, :required => true
param :id, :identifier_dottable, :required => true

def show; end
def show
end

def_param_group :snapshot do
param :snapshot, Hash, :required => true, :action_aware => true do
Expand All @@ -51,8 +52,8 @@ def show; end

def create
@snapshot = resource_class.new(snapshot_params.to_h.merge(host: @host).merge(
include_ram: Foreman::Cast.to_bool(params[:include_ram]), quiesce: Foreman::Cast.to_bool(params[:quiesce])
))
include_ram: Foreman::Cast.to_bool(params[:include_ram]), quiesce: Foreman::Cast.to_bool(params[:quiesce])
))
process_response @snapshot.create
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def update
end

define_action_permission ['select_multiple_host', 'create_multiple_host'], :create
def select_multiple_host; end
def select_multiple_host
end

def create_multiple_host
data = snapshot_params
Expand All @@ -95,7 +96,7 @@ def create_multiple_host
msg = _('Created %{snapshots} for %{num} %{hosts}') % {
snapshots: n_('Snapshot', 'Snapshots', snapshots_created),
num: snapshots_created,
hosts: n_('host', 'hosts', snapshots_created)
hosts: n_('host', 'hosts', snapshots_created),
}
# for backwards compatibility
if respond_to? :success
Expand All @@ -108,10 +109,11 @@ def create_multiple_host
end

def snapshot_mode_assignment(snapshot_mode, data)
if snapshot_mode == 'memory'
case snapshot_mode
when 'memory'
data[:include_ram] = true
data[:quiesce] = false
elsif snapshot_mode == 'quiesce'
when 'quiesce'
data[:include_ram] = false
data[:quiesce] = true
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def status_task(_node, _upid)
'exitstatus' => 'OK',
'status' => 'stopped',
'id' => '100',
'pid' => 15_891
'pid' => 15_891,
}
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/fog_extensions/vsphere/snapshots/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def remove_snapshot(options = {})
raise ArgumentError, 'removeChildren is a required parameter' unless options.key? 'removeChildren'

{
'task_state' => 'success'
'task_state' => 'success',
}
end

Expand All @@ -25,7 +25,7 @@ def rename_snapshot(options = {})
raise ArgumentError, 'description is a required parameter' unless options.key? 'description'

{
'task_state' => 'success'
'task_state' => 'success',
}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def remove_snapshot(options = {})
task.wait_for_completion

{
'task_state' => task.info.state
'task_state' => task.info.state,
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def capabilities
# This method creates a Snapshot with a given name and optional description.
def create_snapshot(host, name, description, include_ram = false, _quiesce = false)
server = find_vm_by_uuid host.uuid
raise _('Name must contain at least 2 characters starting with alphabet. Valid characters are A-Z a-z 0-9 _') unless /^[A-Za-z][\w]{1,}$/.match?(name)
raise _('Name must contain at least 2 characters starting with alphabet. Valid characters are A-Z a-z 0-9 _') unless /^[A-Za-z]\w{1,}$/.match?(name)

include_ram = include_ram ? 1 : 0
snapshot = server.snapshots.create(name: name, vmstate: include_ram)
Expand Down
21 changes: 11 additions & 10 deletions app/models/foreman_snapshot_management/snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ class Snapshot
include ActiveModel::ForbiddenAttributesProtection

define_model_callbacks :create, :save, :destroy, :revert
attr_accessor :id, :raw_snapshot, :parent, :snapshot_mode
attr_writer :create_time, :quiesce
attr_reader :name, :description, :include_ram, :host_id, :quiesce
attr_accessor :id, :raw_snapshot, :parent, :snapshot_mode, :quiesce
attr_writer :create_time
attr_reader :name, :description, :include_ram, :host_id

define_attribute_methods :name, :description, :include_ram

def self.model_name
Struct.new(:name, :klass, :singular, :plural, :element,
:human, :collection, :param_key, :i18n_key, :route_key, :singular_route_key).new(
'ForemanSnapshotManagement::Snapshot', ForemanSnapshotManagement::Snapshot,
'foreman_snapshot_management_snapshot', 'foreman_snapshot_management_snapshots',
'snapshot', 'Snapshot', 'foreman_snapshot_management/snapshots',
'snapshot', :'foreman_snapshot_management/snapshot', 'foreman_snapshot_management_snapshots',
'foreman_snapshot_management_snapshot'
)
:human, :collection, :param_key, :i18n_key, :route_key, :singular_route_key).new(
'ForemanSnapshotManagement::Snapshot', ForemanSnapshotManagement::Snapshot,
'foreman_snapshot_management_snapshot', 'foreman_snapshot_management_snapshots',
'snapshot', 'Snapshot', 'foreman_snapshot_management/snapshots',
'snapshot', :'foreman_snapshot_management/snapshot', 'foreman_snapshot_management_snapshots',
'foreman_snapshot_management_snapshot'
)
end

def self.any?
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end
end

constraints(host_id: %r{[^\/]+}) do
constraints(host_id: %r{[^/]+}) do
resources :hosts, only: [] do
resources :snapshots, module: 'foreman_snapshot_management', only: [:index, :create, :destroy, :update] do
member do
Expand Down
36 changes: 18 additions & 18 deletions lib/foreman_snapshot_management/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,49 @@ class Engine < ::Rails::Engine
security_block :foreman_snapshot_management do
permission :view_snapshots, {
:'foreman_snapshot_management/snapshots' => [:index],
:'api/v2/snapshots' => [:index, :show]
:'api/v2/snapshots' => [:index, :show],
}, :resource_type => 'Host'

permission :create_snapshots, {
:'foreman_snapshot_management/snapshots' => [:create, :select_multiple_host, :create_multiple_host],
:'api/v2/snapshots' => [:create]
:'api/v2/snapshots' => [:create],
}, :resource_type => 'Host'

permission :edit_snapshots, {
:'foreman_snapshot_management/snapshots' => [:update],
:'api/v2/snapshots' => [:update]
:'api/v2/snapshots' => [:update],
}, :resource_type => 'Host'

permission :destroy_snapshots, {
:'foreman_snapshot_management/snapshots' => [:destroy],
:'api/v2/snapshots' => [:destroy]
:'api/v2/snapshots' => [:destroy],
}, :resource_type => 'Host'

permission :revert_snapshots, {
:'foreman_snapshot_management/snapshots' => [:revert],
:'api/v2/snapshots' => [:revert]
:'api/v2/snapshots' => [:revert],
}, :resource_type => 'Host'
end

# Adds roles if they do not exist
role 'Snapshot Viewer',
[:view_snapshots],
'Role granting permission only to view snapshots for hosts'
[:view_snapshots],
'Role granting permission only to view snapshots for hosts'
role 'Snapshot Manager',
[
:view_snapshots,
:create_snapshots,
:edit_snapshots,
:destroy_snapshots,
:revert_snapshots
],
'Role granting permission to manage snapshots for hosts'
[
:view_snapshots,
:create_snapshots,
:edit_snapshots,
:destroy_snapshots,
:revert_snapshots,
],
'Role granting permission to manage snapshots for hosts'

extend_page('hosts/show') do |context|
context.add_pagelet :main_tabs,
:name => N_('Snapshots'),
:partial => 'hosts/snapshots_tab',
:onlyif => proc { |host| host&.compute_resource&.capable?(:snapshots) }
:name => N_('Snapshots'),
:partial => 'hosts/snapshots_tab',
:onlyif => proc { |host| host&.compute_resource&.capable?(:snapshots) }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/api/v2/snapshots_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Api::V2::SnapshotsControllerTest < ActionController::TestCase
let(:tax_location) { Location.find_by(name: 'Location 1') }
let(:tax_organization) { Organization.find_by(name: 'Organization 1') }
let(:compute_resource) do
cr = FactoryBot.create(:compute_resource, :vmware, :uuid => 'Solutions', :locations => [tax_location], organizations: [tax_organization])
cr = FactoryBot.create(:compute_resource, :vmware, :uuid => 'Solutions', :locations => [tax_location], :organizations => [tax_organization])
ComputeResource.find_by(id: cr.id)
end
let(:uuid) { '5032c8a5-9c5e-ba7a-3804-832a03e16381' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SnapshotsControllerTest < ActionController::TestCase
let(:tax_location) { Location.find_by(name: 'Location 1') }
let(:tax_organization) { Organization.find_by(name: 'Organization 1') }
let(:compute_resource) do
cr = FactoryBot.create(:compute_resource, :vmware, :uuid => 'Solutions', :locations => [tax_location], organizations: [tax_organization])
cr = FactoryBot.create(:compute_resource, :vmware, :uuid => 'Solutions', :locations => [tax_location], :organizations => [tax_organization])
ComputeResource.find_by(id: cr.id)
end
let(:uuid) { '5032c8a5-9c5e-ba7a-3804-832a03e16381' }
Expand Down

0 comments on commit 9f645c3

Please sign in to comment.