Skip to content

Commit

Permalink
(MODULES-10385) - panos_address custom vsys
Browse files Browse the repository at this point in the history
- Retrieves vsys of address
- Can set vsys for address
  • Loading branch information
David Swan committed Mar 19, 2020
1 parent 49f1c85 commit 58349ba
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
9 changes: 6 additions & 3 deletions .fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# See https://github.com/puppetlabs/puppetlabs_spec_helper#using-fixtures for details
---
fixtures:
forge_modules:
resource_api: 'puppetlabs/resource_api'
puppetserver_gem: "puppetlabs/puppetserver_gem"
# forge_modules:
# resource_api: 'puppetlabs/resource_api'
# puppetserver_gem: "puppetlabs/puppetserver_gem"
repositories:
resource_api: "https://github.com/puppetlabs/puppet-resource_api.git"
puppetserver_gem: 'https://github.com/puppetlabs/puppetlabs-puppetserver_gem.git'
4 changes: 2 additions & 2 deletions lib/puppet/provider/panos_address/panos_address.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative '../panos_provider'
require_relative '../panos_vsys_base'

# Implementation for the panos_address type using the Resource API.
class Puppet::Provider::PanosAddress::PanosAddress < Puppet::Provider::PanosProvider
class Puppet::Provider::PanosAddress::PanosAddress < Puppet::Provider::PanosVsysBase
def validate_should(should)
required = [should[:ip_netmask], should[:ip_range], should[:fqdn]].compact.size
if required > 1 # rubocop:disable Style/GuardClause
Expand Down
57 changes: 57 additions & 0 deletions lib/puppet/provider/panos_vsys_base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require_relative 'panos_provider'

# A base provider for all PANOS providers
class Puppet::Provider::PanosVsysBase < Puppet::Provider::PanosProvider
def get(context)
virtual_systems = retrieve_vsys(context)
results = []
virtual_systems.each do |vsys|
xpath = context.type.definition[:base_xpath].gsub!('vsys/entry', "vsys/entry[@name='#{vsys}']")
config = context.transport.get_config(xpath + '/entry')
config.elements.collect('/response/result/entry') do |entry| # rubocop:disable Style/CollectionMethods
result = {}
context.type.attributes.each do |attr_name, attr|
result[attr_name] = match(entry, attr, attr_name)
end
result[:vsys] = vsys
results << result
end
end
results
end

def retrieve_vsys(context)
config = context.transport.get_config(context.type.definition[:vsys_xpath] + '/entry')
results = []
config.elements.collect('/response/result/entry') do |entry| # rubocop:disable Style/CollectionMethods
result = {}
context.type.attributes.each do |attr_name, attr|
result[attr_name] = match(entry, attr, attr_name)
end
results << result[:name]
end
results
end

def create(context, name, should)
validate_should(should) if defined? validate_should
xpath = if should[:vsys] != 'vsys'
context.type.definition[:base_xpath].gsub!('vsys/entry', "vsys/entry[@name='#{should[:vsys]}']")
else
context.type.definition[:base_xpath]
end
context.transport.set_config(xpath, xml_from_should(name, should))
context.transport.move(context.type.definition[:base_xpath], name, should[:insert_after]) unless should[:insert_after].nil?
end

def update(context, name, should)
validate_should(should) if defined? validate_should
xpath = if should[:vsys] != 'vsys'
context.type.definition[:base_xpath].gsub!('vsys/entry', "vsys/entry[@name='#{should[:vsys]}']")
else
context.type.definition[:base_xpath]
end
context.transport.edit_config(xpath + "/entry[@name='#{name}']", xml_from_should(name, should))
context.transport.move(context.type.definition[:base_xpath], name, should[:insert_after]) unless should[:insert_after].nil?
end
end
7 changes: 7 additions & 0 deletions lib/puppet/type/panos_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
docs: <<-EOS,
This type provides Puppet with the capabilities to manage "address" objects on Palo Alto devices.
EOS
vsys_xpath: '/config/devices/entry/vsys',
base_xpath: '/config/devices/entry/vsys/entry/address',
features: ['remote_resource'],
attributes: {
Expand All @@ -14,6 +15,12 @@
behaviour: :namevar,
xpath: 'string(@name)',
},
vsys: {
type: 'Pattern[/^[a-zA-z0-9\-_\s\.]{1,63}$/]',
desc: 'The vsys of the addresses xpath.',
# behaviour: :namevar,
default: 'vsys',
},
ensure: {
type: 'Enum[present, absent]',
desc: 'Whether this resource should be present or absent on the target system.',
Expand Down

0 comments on commit 58349ba

Please sign in to comment.