Skip to content

Commit 48084a3

Browse files
committed
(FM-8336) Capture and expose attribute ordering from transport schema
When exposing attributes in the UI, they currently need to be alphabetised by their name, since the transmission through APIs does not preserve key order of the connection_info hash. To facilitate showing attributes in the order defined by the module author, this change embeds the key-insertion order from the TypeDefinition into the API response.
1 parent 0445fb0 commit 48084a3

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

lib/puppet/resource_api/transport.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ def register(schema)
88
raise Puppet::DevError, 'requires `:desc`' unless schema.key? :desc
99
raise Puppet::DevError, 'requires `:connection_info`' unless schema.key? :connection_info
1010
raise Puppet::DevError, '`:connection_info` must be a hash, not `%{other_type}`' % { other_type: schema[:connection_info].class } unless schema[:connection_info].is_a?(Hash)
11+
if schema[:connection_info_order].nil?
12+
schema[:connection_info_order] = schema[:connection_info].keys
13+
else
14+
raise Puppet::DevError, '`:connection_info_order` must be an array, not `%{other_type}`' % { other_type: schema[:connection_info_order].class } unless schema[:connection_info_order].is_a?(Array)
15+
end
1116

1217
unless transports[schema[:name]].nil?
1318
raise Puppet::DevError, 'Transport `%{name}` is already registered for `%{environment}`' % {

spec/integration/resource_api/transport_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
expect(transports['test_device'].definition).to include(name: 'test_device')
1212
end
1313

14+
it 'adds connection_info_order' do
15+
expect(transports['test_device'].definition).to include(connection_info_order: a_kind_of(Array))
16+
end
17+
1418
it 'can be called twice' do
1519
expect(Puppet::ResourceApi::Transport.list).to be_empty
1620
Puppet::ResourceApi::Transport.list_all_transports('rp_env')

spec/puppet/resource_api/transport_spec.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,36 @@ def change_environment(name = nil)
3030
end
3131

3232
describe '#register(schema)' do
33-
context 'when registering a schema with missing keys' do
33+
describe 'validation checks' do
3434
it { expect { described_class.register([]) }.to raise_error(Puppet::DevError, %r{requires a hash as schema}) }
3535
it { expect { described_class.register({}) }.to raise_error(Puppet::DevError, %r{requires a `:name`}) }
3636
it { expect { described_class.register(name: 'no connection info', desc: 'some description') }.to raise_error(Puppet::DevError, %r{requires `:connection_info`}) }
3737
it { expect { described_class.register(name: 'no description') }.to raise_error(Puppet::DevError, %r{requires `:desc`}) }
38-
it { expect { described_class.register(name: 'no hash attributes', desc: 'some description', connection_info: []) }.to raise_error(Puppet::DevError, %r{`:connection_info` must be a hash, not}) }
38+
it {
39+
expect {
40+
described_class.register(name: 'no hash connection_info',
41+
desc: 'some description',
42+
connection_info: [])
43+
} .to raise_error(Puppet::DevError, %r{`:connection_info` must be a hash, not})
44+
}
45+
it {
46+
expect(described_class.register(name: 'no array connection_info_order',
47+
desc: 'some description',
48+
connection_info: {}).definition).to have_key(:connection_info_order)
49+
}
50+
it {
51+
expect(described_class.register(name: 'no array connection_info_order',
52+
desc: 'some description',
53+
connection_info: {}).definition[:connection_info_order]).to eq []
54+
}
55+
it {
56+
expect {
57+
described_class.register(name: 'no array connection_info_order',
58+
desc: 'some description',
59+
connection_info: {},
60+
connection_info_order: {})
61+
}.to raise_error(Puppet::DevError, %r{`:connection_info_order` must be an array, not})
62+
}
3963
end
4064

4165
context 'when registering a minimal transport' do

0 commit comments

Comments
 (0)