diff --git a/app/models/server_role.rb b/app/models/server_role.rb index 291541a66d1..0ad4329501b 100644 --- a/app/models/server_role.rb +++ b/app/models/server_role.rb @@ -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']}]") @@ -25,6 +30,7 @@ def self.seed end end end + @zone_scoped_roles = @region_scoped_roles = nil end diff --git a/lib/vmdb/plugins.rb b/lib/vmdb/plugins.rb index 4edcc4d07a1..2f80b809e38 100644 --- a/lib/vmdb/plugins.rb +++ b/lib/vmdb/plugins.rb @@ -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("*.*") } diff --git a/spec/models/server_role_spec.rb b/spec/models/server_role_spec.rb index 6c3d75c99ea..0844bb2cca8 100644 --- a/spec/models/server_role_spec.rb +++ b/spec/models/server_role_spec.rb @@ -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 @@ -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| @@ -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