-
Notifications
You must be signed in to change notification settings - Fork 17
/
panos_provider.rb
85 lines (73 loc) · 2.86 KB
/
panos_provider.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require 'puppet/resource_api/simple_provider'
# A base provider for all PANOS providers
class Puppet::Provider::PanosProvider < Puppet::ResourceApi::SimpleProvider
def initialize # rubocop:disable Lint/MissingSuper
require 'rexml/document'
require 'rexml/xpath'
require 'builder'
end
def get(context)
config = context.transport.get_config(context.type.definition[:base_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
defined?(munge) ? munge(result) : result
end
end
def create(context, name, should)
validate_should(should) if defined? validate_should
context.transport.set_config(context.type.definition[:base_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
context.transport.edit_config(context.type.definition[:base_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
def delete(context, name)
context.transport.delete_config(context.type.definition[:base_xpath] + "/entry[@name='#{name}']")
end
def match(entry, attr, attr_name)
return 'present' if attr_name == :ensure
if attr.key? :xpath
text_match(entry, attr)
elsif attr.key? :xpath_array
array_match(entry, attr)
end
end
# PANOS uses Yes/No, convert to bool if the type expects it
def string_to_bool(value)
return false if value.nil? || value.casecmp('no').zero?
return true if value.casecmp('yes').zero?
value # if it doesn't match anything
end
# Puppet type uses bool, convert to Yes/No to suit PANOS system
def bool_to_string(value)
return 'yes' if value == true
return 'no' if value == false
value # if it doesn't match anything
end
def build_tags(builder, should)
return unless should[:tags]
builder.tag do
should[:tags].each do |tag|
builder.member(tag)
end
end
end
private
def array_match(entry, attr)
result = REXML::XPath.match(entry, attr[:xpath_array]).map(&:to_s)
# Allow empty array values to return nil if the value is Optional
(result.empty? && attr[:type] =~ %r{^Optional.*}) ? nil : result
end
def text_match(entry, attr)
result = REXML::XPath.match(entry, attr[:xpath]).first
# don't convert nil values to empty strings
return result if result.is_a? String
return nil if result.is_a?(Array) && result.empty?
result.nil? ? nil : result.value
end
end