Class: Puppet::Provider::PanosArbitraryCommands::PanosArbitraryCommands

Inherits:
ResourceApi::SimpleProvider
  • Object
show all
Defined in:
lib/puppet/provider/panos_arbitrary_commands/panos_arbitrary_commands.rb

Overview

provider to handle arbitrary configuration commands against a PANOS device using the Resource API.

Instance Method Summary collapse

Constructor Details

#initializePanosArbitraryCommands

Returns a new instance of PanosArbitraryCommands.



5
6
7
8
# File 'lib/puppet/provider/panos_arbitrary_commands/panos_arbitrary_commands.rb', line 5

def initialize
  require 'rexml/xpath'
  require 'builder'
end

Instance Method Details

#canonicalize(_context, resources) ⇒ Object

to ensure that the manifest xml matches the API format

e.g.

<entry admin='admin'>
  <foo>bar</bar>
</entry>

becomes:

<entry><foo>bar</bar></entry>


18
19
20
21
22
23
24
# File 'lib/puppet/provider/panos_arbitrary_commands/panos_arbitrary_commands.rb', line 18

def canonicalize(_context, resources)
  resources.each do |resource|
    resource[:xml] = str_from_xml(resource[:xml])
  end

  resources
end

#create(context, xpath, should) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/puppet/provider/panos_arbitrary_commands/panos_arbitrary_commands.rb', line 44

def create(context, xpath, should)
  begin
    should = REXML::Document.new should[:xml]
  rescue REXML::ParseException => parse_exception
    raise Puppet::ResourceError, parse_exception.message
  end

  context.transport.set_config('/config/' + xpath, should)
end

#delete(context, xpath) ⇒ Object



64
65
66
# File 'lib/puppet/provider/panos_arbitrary_commands/panos_arbitrary_commands.rb', line 64

def delete(context, xpath)
  context.transport.delete_config('/config/' + xpath)
end

#get(context, xpaths = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppet/provider/panos_arbitrary_commands/panos_arbitrary_commands.rb', line 26

def get(context, xpaths = nil)
  return [] if xpaths.nil?
  results = []
  config = context.transport.get_config('/config/' + xpaths.first) unless xpaths.first.nil?
  if xpaths.first
    config.elements.collect('/response/result') do |entry| # rubocop:disable Style/CollectionMethods
      xml = str_from_xml(entry.to_s)

      results << {
        xpath:  xpaths.first,
        ensure: 'present',
        xml:    xml,
      }
    end
  end
  results
end

#str_from_xml(xml) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/puppet/provider/panos_arbitrary_commands/panos_arbitrary_commands.rb', line 68

def str_from_xml(xml)
  xml.to_s
     .gsub(%r{<result.*?[^>)]>}, '') # cleaning out the start of the result tags
     .gsub(%r{</result>}, '') # cleaning the closing result tags
     .gsub(%r{admin=(?:'|")[a-zA-Z0-9]*(?:'|")}, '') # removing the `admin` attributes
     .gsub(%r{time=(?:'|")\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}(?:'|")}, '') # removing the `time` attributes
     .gsub(%r{dirtyId=(?:'|")\d*(?:'|")}, '') # removing the `dirtyId` attributes
     .gsub(%r{\n(\s*[^<])?}, '') # removing new lines and extra spaces
     .tr("'", '"') # replacing the \' with "
     .gsub(%r{\s{2,}}, ' ') # removing extra spaces
     .gsub(%r{\s*(/)?>}, '\1>') # cleaning the extra spaces before a close `>` and self close `/>`
end

#update(context, xpath, should) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/puppet/provider/panos_arbitrary_commands/panos_arbitrary_commands.rb', line 54

def update(context, xpath, should)
  begin
    should = REXML::Document.new should[:xml]
  rescue REXML::ParseException => parse_exception
    raise Puppet::ResourceError, parse_exception.message
  end

  context.transport.edit_config('/config/' + xpath, should)
end