Skip to content

Commit 1341033

Browse files
authored
Merge pull request #152 from DavidS/FM-7726-context-transport
(FM-7726) implement `context.transport` to provide access
2 parents 8e64a3d + bd042cf commit 1341033

File tree

8 files changed

+50
-11
lines changed

8 files changed

+50
-11
lines changed

lib/puppet/resource_api/base_context.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def device
2525
raise 'Received device() on an unprepared BaseContext. Use a PuppetContext instead.'
2626
end
2727

28+
def transport
29+
raise 'No transport available.'
30+
end
31+
2832
def failed?
2933
@failed
3034
end

lib/puppet/resource_api/io_context.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
require 'puppet/resource_api/base_context'
22

33
# Implement Resource API Conext to log through an IO object, defaulting to `$stderr`.
4-
# There is no access to a device here.
4+
# There is no access to a device here. You can supply a transport if necessary.
55
class Puppet::ResourceApi::IOContext < Puppet::ResourceApi::BaseContext
6-
def initialize(definition, target = $stderr)
6+
attr_reader :transport
7+
8+
def initialize(definition, target = $stderr, transport = nil)
79
super(definition)
810
@target = target
11+
@transport = transport
912
end
1013

1114
protected

lib/puppet/resource_api/puppet_context.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
require 'puppet/util/logging'
33

44
# Implement Resource API Context to log through Puppet facilities
5-
# and access/expose the puppet process' current device
5+
# and access/expose the puppet process' current device/transport
66
class Puppet::ResourceApi::PuppetContext < Puppet::ResourceApi::BaseContext
77
def device
88
# TODO: evaluate facter_url setting for loading config if there is no `current` NetworkDevice
99
raise 'no device configured' unless Puppet::Util::NetworkDevice.current
1010
Puppet::Util::NetworkDevice.current
1111
end
1212

13+
def transport
14+
device.transport
15+
end
16+
1317
def log_exception(exception, message: 'Error encountered', trace: false)
1418
super(exception, message: message, trace: trace || Puppet[:trace])
1519
end

lib/puppet/resource_api/transport/wrapper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Puppet::ResourceApi::Transport::Wrapper` to interface between the Util::NetworkDevice
66
class Puppet::ResourceApi::Transport::Wrapper
7-
attr_reader :transport
7+
attr_reader :transport, :schema
88

99
def initialize(name, url_or_config)
1010
if url_or_config.is_a? String
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
require 'puppet/util/network_device/simple/device'
1+
require 'puppet/resource_api/transport/wrapper'
22

33
module Puppet::Util::NetworkDevice::Test_device # rubocop:disable Style/ClassAndModuleCamelCase
4-
# A simple test device returning hardcoded facts
5-
class Device < Puppet::Util::NetworkDevice::Simple::Device
6-
def facts
7-
{ 'foo' => 'bar' }
4+
class Device < Puppet::ResourceApi::Transport::Wrapper
5+
def initialize(url_or_config, _options = {})
6+
puts url_or_config.inspect
7+
super('test_device', url_or_config)
88
end
99
end
1010
end

spec/puppet/resource_api/base_context_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ def send_log(log, msg)
341341
it { expect { described_class.new(definition).device }.to raise_error RuntimeError, %r{Received device\(\) on an unprepared BaseContext\. Use a PuppetContext instead} }
342342
end
343343

344+
describe '#transport' do
345+
it { expect { described_class.new(definition).transport }.to raise_error RuntimeError, %r{No transport available\.} }
346+
end
347+
344348
describe '#send_log' do
345349
it { expect { described_class.new(definition).send_log(nil, nil) }.to raise_error RuntimeError, %r{Received send_log\(\) on an unprepared BaseContext\. Use IOContext, or PuppetContext instead} }
346350
end

spec/puppet/resource_api/io_context_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
require 'puppet/resource_api/io_context'
33

44
RSpec.describe Puppet::ResourceApi::IOContext do
5-
subject(:context) { described_class.new(definition, io) }
5+
subject(:context) { described_class.new(definition, io, transport) }
66

77
let(:definition) { { name: 'some_resource', attributes: {} } }
8+
let(:transport) { nil }
89

910
let(:io) { StringIO.new('', 'w') }
1011

@@ -18,4 +19,13 @@
1819
expect(io.string).to match %r{warning}i
1920
end
2021
end
22+
23+
describe '#transport' do
24+
it { expect(context.transport).to be_nil }
25+
context 'when passing in a transport' do
26+
let(:transport) { instance_double(Object, 'transport') }
27+
28+
it { expect(context.transport).to eq transport }
29+
end
30+
end
2131
end

spec/puppet/resource_api/puppet_context_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,21 @@
1818
end
1919
end
2020

21-
context 'with no NetworkDevice configured' do
21+
context 'when a Transport::Wrapper device is configured' do
22+
let(:device) { instance_double('Puppet::Util::NetworkDevice::Test_device::Device', 'device') }
23+
let(:transport) { instance_double('Puppet::Transport::TestDevice', 'transport') }
24+
25+
before(:each) do
26+
allow(Puppet::Util::NetworkDevice).to receive(:current).and_return(device)
27+
allow(device).to receive(:transport).and_return(transport)
28+
end
29+
30+
it 'returns the transport' do
31+
expect(context.transport).to eq(transport)
32+
end
33+
end
34+
35+
context 'with nothing configured' do
2236
before(:each) do
2337
allow(Puppet::Util::NetworkDevice).to receive(:current).and_return(nil)
2438
end

0 commit comments

Comments
 (0)