Skip to content

Commit 783816a

Browse files
committed
Resolve DMI ChassisType for Windows too
1 parent 2a2e4de commit 783816a

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module Facts
4+
module Windows
5+
module Dmi
6+
module Chassis
7+
class Type
8+
FACT_NAME = 'dmi.chassis.type'
9+
ALIASES = 'chassistype'
10+
11+
def call_the_resolver
12+
fact_value = Facter::Resolvers::DMISystemEnclosure.resolve(:chassis_type)
13+
14+
[Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)]
15+
end
16+
end
17+
end
18+
end
19+
end
20+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
module Facter
4+
module Resolvers
5+
class DMISystemEnclosure < BaseResolver
6+
@log = Facter::Log.new(self)
7+
init_resolver
8+
9+
class << self
10+
# ChassisType
11+
12+
private
13+
14+
def post_resolve(fact_name, _options)
15+
@fact_list.fetch(fact_name) { read_facts(fact_name) }
16+
end
17+
18+
def read_facts(fact_name)
19+
win = Facter::Util::Windows::Win32Ole.new
20+
systemenclosure = win.return_first('SELECT ChassisTypes FROM Win32_SystemEnclosure')
21+
unless systemenclosure
22+
@log.debug 'WMI query returned no results for Win32_SystemEnclosure with values ChassisTypes.'
23+
return
24+
end
25+
26+
build_fact_list(systemenclosure)
27+
28+
# ChassisTypes is an Array on Windows - Convert the first one to a name
29+
chassis_to_name(@fact_list[fact_name][0]) if fact_name == :chassis_type
30+
31+
@fact_list[fact_name]
32+
end
33+
34+
def build_fact_list(systemenclosure)
35+
@fact_list[:chassis_type] = systemenclosure.ChassisTypes
36+
end
37+
38+
def chassis_to_name(chassis_type)
39+
types = ['Other', nil, 'Desktop', 'Low Profile Desktop', 'Pizza Box', 'Mini Tower', 'Tower',
40+
'Portable', 'Laptop', 'Notebook', 'Hand Held', 'Docking Station', 'All in One', 'Sub Notebook',
41+
'Space-Saving', 'Lunch Box', 'Main System Chassis', 'Expansion Chassis', 'SubChassis',
42+
'Bus Expansion Chassis', 'Peripheral Chassis', 'Storage Chassis', 'Rack Mount Chassis',
43+
'Sealed-Case PC', 'Multi-system', 'CompactPCI', 'AdvancedTCA', 'Blade', 'Blade Enclosure',
44+
'Tablet', 'Convertible', 'Detachable']
45+
@fact_list[:chassis_type] = types[chassis_type.to_i - 1]
46+
end
47+
end
48+
end
49+
end
50+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
describe Facts::Windows::Dmi::Chassis::Type do
4+
describe '#call_the_resolver' do
5+
subject(:fact) { Facts::Windows::Dmi::Chassis::Type.new }
6+
7+
let(:type) { 'Low Profile Desktop' }
8+
9+
before do
10+
allow(Facter::Resolvers::DMISystemEnclosure).to receive(:resolve).with(:chassis_type).and_return(type)
11+
end
12+
13+
it 'calls Facter::Resolvers::Windows::DMISystemEnclosure' do
14+
fact.call_the_resolver
15+
expect(Facter::Resolvers::DMISystemEnclosure).to have_received(:resolve).with(:chassis_type)
16+
end
17+
18+
it 'returns chassis type fact' do
19+
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \
20+
contain_exactly(an_object_having_attributes(name: 'dmi.chassis.type', value: type),
21+
an_object_having_attributes(name: 'chassistype', value: type, type: :legacy))
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)