Skip to content

Commit 385447e

Browse files
authored
Merge pull request #148 from da-ar/transport_connect
(FM-7600) RSAPI Transport connect method
2 parents ad91a0f + 80faf28 commit 385447e

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

lib/puppet/resource_api/transport.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,25 @@ def register(schema)
1212
@transports[schema[:name]] = Puppet::ResourceApi::TransportSchemaDef.new(schema)
1313
end
1414
module_function :register # rubocop:disable Style/AccessModifierDeclarations
15+
16+
def connect(name, connection_info)
17+
validate(name, connection_info)
18+
begin
19+
require "puppet/transport/#{name}"
20+
class_name = name.split('_').map { |e| e.capitalize }.join
21+
Puppet::Transport.const_get(class_name).new(connection_info)
22+
rescue LoadError, NameError => detail
23+
raise Puppet::DevError, 'Cannot load transport for `%{target}`: %{detail}' % { target: name, detail: detail }
24+
end
25+
end
26+
module_function :connect # rubocop:disable Style/AccessModifierDeclarations
27+
28+
def self.validate(name, connection_info)
29+
@transports ||= {}
30+
transport_schema = @transports[name]
31+
raise Puppet::DevError, 'Transport for `%{target}` not registered' % { target: name } if transport_schema.nil?
32+
33+
transport_schema.check_schema(connection_info)
34+
transport_schema.validate(connection_info)
35+
end
1536
end

lib/puppet/resource_api/type_definition.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ class TransportSchemaDef < BaseTypeDefinition
5050
def initialize(definition)
5151
super(definition, :connection_info)
5252
end
53+
54+
def validate(resource)
55+
# enforce mandatory attributes
56+
missing_attrs = []
57+
58+
attributes.each do |name, _options|
59+
type = @data_type_cache[attributes[name][:type]]
60+
61+
if resource[name].nil? && !(type.instance_of? Puppet::Pops::Types::POptionalType)
62+
missing_attrs << name
63+
end
64+
end
65+
66+
error_msg = "The following mandatory attributes were not provided:\n * " + missing_attrs.join(", \n * ")
67+
raise Puppet::ResourceError, error_msg if missing_attrs.any?
68+
end
5369
end
5470

5571
# Base RSAPI schema Object

spec/puppet/resource_api/transport_schema_def_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,21 @@
2525
it { expect(type.attributes).to be_key(:user) }
2626
end
2727
end
28+
29+
describe '#validate' do
30+
context 'when resource is missing attributes' do
31+
let(:resource) { {} }
32+
33+
it 'raises an error listing the missing attributes' do
34+
expect { type.validate(resource) }.to raise_error Puppet::ResourceError, %r{host}
35+
expect { type.validate(resource) }.to raise_error Puppet::ResourceError, %r{user}
36+
end
37+
end
38+
39+
context 'when resource has all its attributes' do
40+
let(:resource) { { host: '1234', user: '4321' } }
41+
42+
it { expect { type.validate(resource) }.not_to raise_error }
43+
end
44+
end
2845
end

spec/puppet/resource_api/transport_spec.rb

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
desc: 'the user to connect as',
4545
},
4646
password: {
47-
type: 'Sensitive[String]',
47+
type: 'String',
48+
sensitive: true,
4849
desc: 'the password to make the connection',
4950
},
5051
},
@@ -76,4 +77,72 @@
7677
)
7778
}
7879
end
80+
81+
context 'when connecting to a transport' do
82+
let(:name) { 'test_target' }
83+
let(:connection_info) do
84+
{
85+
name: 'test_target',
86+
desc: 'a basic transport',
87+
connection_info: {
88+
host: {
89+
type: 'String',
90+
desc: 'the host ip address or hostname',
91+
},
92+
},
93+
}
94+
end
95+
96+
context 'when the transport file does not exist' do
97+
it 'throws a DevError' do
98+
expect(described_class).to receive(:validate).with(name, connection_info)
99+
expect { described_class.connect(name, connection_info) }.to raise_error Puppet::DevError,
100+
%r{Cannot load transport for `test_target`}
101+
end
102+
end
103+
104+
context 'when the transport file does exist' do
105+
context 'with and incorrectly defined transport' do
106+
it 'throws a DevError' do
107+
expect(described_class).to receive(:validate).with(name, connection_info)
108+
expect(described_class).to receive(:require).with('puppet/transport/test_target')
109+
expect { described_class.connect(name, connection_info) }.to raise_error Puppet::DevError,
110+
%r{uninitialized constant Puppet::Transport}
111+
end
112+
end
113+
114+
context 'with a correctly defined transport' do
115+
let(:test_target) { double('Puppet::Transport::TestTarget') } # rubocop:disable RSpec/VerifiedDoubles
116+
117+
it 'loads initiates the class successfully' do
118+
expect(described_class).to receive(:require).with('puppet/transport/test_target')
119+
expect(described_class).to receive(:validate).with(name, connection_info)
120+
121+
stub_const('Puppet::Transport::TestTarget', test_target)
122+
expect(test_target).to receive(:new).with(connection_info)
123+
124+
described_class.connect(name, connection_info)
125+
end
126+
end
127+
end
128+
end
129+
130+
describe '#self.validate' do
131+
context 'when the transport being validated has not be registered' do
132+
it { expect { described_class.validate('wibble', {}) }.to raise_error Puppet::DevError, %r{Transport for `wibble` not registered} }
133+
end
134+
135+
context 'when the transport being validated has been registered' do
136+
let(:schema) { { name: 'validate', desc: 'a minimal connection', connection_info: {} } }
137+
138+
it 'continues to validate the connection_info' do
139+
# rubocop:disable RSpec/AnyInstance
140+
expect_any_instance_of(Puppet::ResourceApi::TransportSchemaDef).to receive(:check_schema).with({})
141+
expect_any_instance_of(Puppet::ResourceApi::TransportSchemaDef).to receive(:validate).with({})
142+
# rubocop:enable RSpec/AnyInstance
143+
described_class.register(schema)
144+
described_class.validate('validate', {})
145+
end
146+
end
147+
end
79148
end

0 commit comments

Comments
 (0)