Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pluggable Server Roles #22918

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't actually necessary, I just don't think mocking File.open without any args is error prone


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