Skip to content

Commit

Permalink
Merge pull request #65 from Thomas-Franklin/pdk-1143
Browse files Browse the repository at this point in the history
(PDK-1143) changes to work with composite namevars from simple provider
  • Loading branch information
DavidS authored Oct 3, 2018
2 parents dcc97b8 + a17cabc commit 73296bb
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 210 deletions.
20 changes: 10 additions & 10 deletions lib/puppet/provider/panos_path_monitor_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def munge(entry)
entry
end

def xml_from_should(_name, should)
def xml_from_should(name, should)
builder = Builder::XmlMarkup.new
builder.entry('name' => should[:path]) do
builder.entry('name' => name[:path]) do
builder.source(should[:source])
builder.destination(should[:destination])
builder.interval(should[:interval])
Expand Down Expand Up @@ -49,20 +49,20 @@ def get(context)
results
end

def create(context, _name, should)
paths = should[:route].split('/')
def create(context, name, should)
paths = name[:route].split('/')
context.type.definition[:base_xpath] = "/config/devices/entry/network/virtual-router/entry[@name='#{paths[0]}']/routing-table/#{@version_label}/static-route/entry[@name='#{paths[1]}']/path-monitor/monitor-destinations" # rubocop:disable Metrics/LineLength
context.device.set_config(context.type.definition[:base_xpath], xml_from_should(should[:name], should))
context.device.set_config(context.type.definition[:base_xpath], xml_from_should(name, should))
end

def update(context, _name, should)
paths = should[:route].split('/')
def update(context, name, should)
paths = name[:route].split('/')
context.type.definition[:base_xpath] = "/config/devices/entry/network/virtual-router/entry[@name='#{paths[0]}']/routing-table/#{@version_label}/static-route/entry[@name='#{paths[1]}']/path-monitor/monitor-destinations" # rubocop:disable Metrics/LineLength
context.device.set_config(context.type.definition[:base_xpath], xml_from_should(should[:name], should))
context.device.set_config(context.type.definition[:base_xpath], xml_from_should(name, should))
end

def delete(context, name)
names = name.split('/')
context.device.delete_config(context.type.definition[:base_xpath] + "/entry[@name='#{names[0]}']/routing-table/#{@version_label}/static-route/entry[@name='#{names[1]}']/path-monitor/monitor-destinations/entry[@name='#{names[2]}']") # rubocop:disable Metrics/LineLength
names = name[:route].split('/')
context.device.delete_config(context.type.definition[:base_xpath] + "/entry[@name='#{names[0]}']/routing-table/#{@version_label}/static-route/entry[@name='#{names[1]}']/path-monitor/monitor-destinations/entry[@name='#{name[:path]}']") # rubocop:disable Metrics/LineLength
end
end
49 changes: 10 additions & 39 deletions lib/puppet/provider/panos_static_route_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,9 @@ def munge(entry)
entry
end

def set(context, changes) # Overriding set to provide the delete method with the vr_name, in order to specify the route to be deleted
changes.each do |name, change|
is = change.key?(:is) ? change[:is] : (get(context) || []).find { |r| r[:name] == name }

context.type.check_schema(is) unless change.key?(:is)

should = change[:should]

raise 'PanosStaticRouteBase cannot be used with a Type that is not ensurable' unless context.type.ensurable?

is = { route: name, ensure: 'absent' } if is.nil?
should = { route: name, ensure: 'absent' } if should.nil?

if is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'present'
context.creating(name) do
create(context, name, should)
end
elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'present'
context.updating(name) do
update(context, name, should)
end
elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'absent'
context.deleting(should[:route]) do
delete(context, should[:route], should[:vr_name])
end
end
end
end

def xml_from_should(_name, should)
def xml_from_should(name, should)
builder = Builder::XmlMarkup.new
builder.entry('name' => should[:route]) do
builder.entry('name' => name[:route]) do
unless should[:nexthop_type] == 'none'
builder.nexthop do
builder.__send__(should[:nexthop_type], should[:nexthop]) unless should[:nexthop_type] == 'discard'
Expand Down Expand Up @@ -111,19 +82,19 @@ def get(context)
end

# Overiding the following methods to point the xpath into the correct VR.
def create(context, _name, should)
context.type.definition[:base_xpath] = "/config/devices/entry/network/virtual-router/entry[@name='#{should[:vr_name]}']/routing-table/#{@version_label}/static-route"
def create(context, name, should)
context.type.definition[:base_xpath] = "/config/devices/entry/network/virtual-router/entry[@name='#{name[:vr_name]}']/routing-table/#{@version_label}/static-route"
validate_should(should)
context.device.set_config(context.type.definition[:base_xpath], xml_from_should(should[:name], should))
context.device.set_config(context.type.definition[:base_xpath], xml_from_should(name, should))
end

def update(context, _name, should)
context.type.definition[:base_xpath] = "/config/devices/entry/network/virtual-router/entry[@name='#{should[:vr_name]}']/routing-table/#{@version_label}/static-route"
def update(context, name, should)
context.type.definition[:base_xpath] = "/config/devices/entry/network/virtual-router/entry[@name='#{name[:vr_name]}']/routing-table/#{@version_label}/static-route"
validate_should(should)
context.device.set_config(context.type.definition[:base_xpath], xml_from_should(should[:name], should))
context.device.set_config(context.type.definition[:base_xpath], xml_from_should(name, should))
end

def delete(context, name, vr_name)
context.device.delete_config(context.type.definition[:base_xpath] + "/entry[@name='#{vr_name}']/routing-table/#{@version_label}/static-route/entry[@name='#{name}']")
def delete(context, name)
context.device.delete_config(context.type.definition[:base_xpath] + "/entry[@name='#{name[:vr_name]}']/routing-table/#{@version_label}/static-route/entry[@name='#{name[:route]}']")
end
end
2 changes: 1 addition & 1 deletion spec/support/shared_examples.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RSpec.shared_examples 'xml_from_should(name, should)' do |test_data, provider|
test_data.each do |test|
it "creates expected XML for `#{test[:desc]}`" do
expect(provider.xml_from_should(test[:attrs][:name], test[:attrs])).to eq(test[:xml].gsub(%r{\n(\s*[^<])?}, ''))
expect(provider.xml_from_should((test[:attrs][:name]) ? test[:attrs][:name] : test[:name], test[:attrs])).to eq(test[:xml].gsub(%r{\n(\s*[^<])?}, ''))
end
end
end
Expand Down
45 changes: 31 additions & 14 deletions spec/unit/puppet/provider/panos_path_monitor_base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@
end
end

describe '#xml_from_should(_name, should)' do
describe '#xml_from_should(name, should)' do
test_data = [
{
desc: 'a full example',
name: {
path: 'path monitor',
route: 'example vr/test route 1',
},
attrs: {
route: 'example vr/test route 1',
path: 'path monitor',
Expand All @@ -60,6 +64,10 @@
},
{
desc: 'essentials example',
name: {
path: 'path monitor',
route: 'example vr/test route 1',
},
attrs: {
route: 'example vr/test route 1',
path: 'path monitor',
Expand Down Expand Up @@ -168,51 +176,53 @@
end
end

describe '#create(context, _name, should)' do
describe '#create(context, name, should)' do
context 'when called' do
let(:expected_path) do
'/config/devices/entry/network/virtual-router/entry[@name=\'foo\']/routing-table/ip/static-route/entry[@name=\'bar\']/path-monitor/monitor-destinations'
end
let(:should_values) do
let(:namevars) do
{
name: 'foo',
route: 'foo/bar',
title: 'foo/bar/path',
route: 'foo/bar',
path: 'path',
}
end
let(:mystruct) { {} }

it 'will call set_config' do
expect(typedef).to receive(:definition).and_return(mystruct).twice
expect(provider).to receive(:xml_from_should).with('foo', should_values)
expect(provider).to receive(:xml_from_should).with(namevars, anything)
expect(device).to receive(:set_config).with(expected_path, anything)
provider.create(context, 'name', should_values)
provider.create(context, namevars, anything)
end
end
end

describe '#update(context, _name, should)' do
describe '#update(context, name, should)' do
context 'when called' do
let(:expected_path) do
'/config/devices/entry/network/virtual-router/entry[@name=\'foo\']/routing-table/ip/static-route/entry[@name=\'bar\']/path-monitor/monitor-destinations'
end
let(:should_values) do
let(:namevars) do
{
name: 'foo',
route: 'foo/bar',
path: 'moo',
title: 'foo/bar/moo',
}
end
let(:mystruct) { {} }

it 'will call set_config' do
expect(typedef).to receive(:definition).and_return(mystruct).twice
expect(provider).to receive(:xml_from_should).with('foo', should_values)
expect(provider).to receive(:xml_from_should).with(namevars, anything)
expect(device).to receive(:set_config).with(expected_path, anything)
provider.update(context, 'name', should_values)
provider.update(context, namevars, anything)
end
end
end

describe '#delete(context, name, vr_name)' do
describe '#delete(context, name)' do
context 'when called' do
let(:expected_path) do
'/some_xpath/entry[@name=\'bar\']/routing-table/ip/static-route/entry[@name=\'bar\']/path-monitor/monitor-destinations/entry[@name=\'moo\']'
Expand All @@ -222,11 +232,18 @@
base_xpath: '/some_xpath',
}
end
let(:namevars) do
{
title: 'bar/bar/moo',
route: 'bar/bar',
path: 'moo',
}
end

it 'will call delete_config' do
expect(typedef).to receive(:definition).and_return(mystruct)
expect(device).to receive(:delete_config).with(expected_path)
provider.delete(context, 'bar/bar/moo')
provider.delete(context, namevars)
end
end
end
Expand Down
Loading

0 comments on commit 73296bb

Please sign in to comment.