Skip to content

Commit

Permalink
Merge pull request #22918 from agrare/pluggable_server_roles
Browse files Browse the repository at this point in the history
Pluggable Server Roles
  • Loading branch information
Fryguy authored Feb 29, 2024
2 parents 340776c + 67c3971 commit 95269ac
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
10 changes: 8 additions & 2 deletions app/models/server_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ class ServerRole < ApplicationRecord

def self.seed
server_roles = all.index_by(&:name)
CSV.foreach(fixture_path, :headers => true, :skip_lines => /^#/).each do |csv_row|
action = csv_row.to_hash

server_role_paths = [fixture_path] + Vmdb::Plugins.server_role_paths

csv_rows = server_role_paths.flat_map do |path|
CSV.foreach(path, :headers => true, :skip_lines => /^#/).map(&:to_hash)
end

csv_rows.each do |action|
rec = server_roles[action['name']]
if rec.nil?
_log.info("Creating Server Role [#{action['name']}]")
Expand All @@ -25,6 +30,7 @@ def self.seed
end
end
end

@zone_scoped_roles = @region_scoped_roles = nil
end

Expand Down
7 changes: 7 additions & 0 deletions lib/vmdb/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ def asset_paths
end
end

def server_role_paths
@server_role_paths ||= filter_map do |engine|
file = engine.root.join("config/server_roles.csv")
file if file.exist?
end
end

def systemd_units
@systemd_units ||= begin
flat_map { |engine| engine.root.join("systemd").glob("*.*") }
Expand Down
33 changes: 31 additions & 2 deletions spec/models/server_role_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
end

context "With Seeding" do
let(:plugin_server_role_paths) { [] }

before do
@csv = <<-CSV.gsub(/^\s+/, "")
name,description,max_concurrent,external_failover,role_scope
Expand All @@ -55,15 +57,21 @@
web_services,Web Services,0,false,region
CSV

allow(File).to receive(:open).and_return(StringIO.new(@csv))
ServerRole.seed
allow(File).to receive(:open).and_call_original
allow(File).to receive(:open).with(ServerRole.fixture_path, "r", any_args).and_return(StringIO.new(@csv))

expect(Vmdb::Plugins).to receive(:server_role_paths).and_return(plugin_server_role_paths)
end

it "should create proper number of rows" do
ServerRole.seed

expect(@csv.split("\n").length - 1).to eq(ServerRole.count)
end

it "should import rows properly" do
ServerRole.seed

roles = @csv.split("\n")
roles.shift
roles.each do |role|
Expand All @@ -86,5 +94,26 @@
end
end
end

context "with a plugin" do
let(:plugin_server_role_path) { ManageIQ::Api::Engine.root.join("config/server_roles.csv") }
let(:plugin_server_role_paths) { [plugin_server_role_path] }

before do
@plugin_csv = <<-CSV.gsub(/^\s+/, "")
name,description,max_concurrent,external_failover,role_scope
special_api_role,Special API Role,0,false,region
CSV

allow(File).to receive(:open).with(plugin_server_role_path, "r", any_args).and_return(StringIO.new(@plugin_csv))
end

it "should create proper number of rows" do
ServerRole.seed

expected_role_count = @csv.split("\n").length + @plugin_csv.split("\n").length - 2
expect(expected_role_count).to eq(ServerRole.count)
end
end
end
end

0 comments on commit 95269ac

Please sign in to comment.