Class: Puppet::Transport::Panos

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/transport/panos.rb

Overview

The main connection class to a PAN-OS API endpoint

Defined Under Namespace

Classes: API

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_context, connection_info) ⇒ Panos

attr_reader :config



17
18
19
# File 'lib/puppet/transport/panos.rb', line 17

def initialize(_context, connection_info)
  @connection_info = self.class.validate_connection_info(connection_info)
end

Class Method Details

.validate_connection_info(connection_info) ⇒ Object

Raises:

  • (Puppet::ResourceError)


10
11
12
13
# File 'lib/puppet/transport/panos.rb', line 10

def self.validate_connection_info(connection_info)
  raise Puppet::ResourceError, 'Could not find "user"/"password" or "apikey" in the configuration' unless (connection_info.key?(:user) && connection_info.key?(:password)) || connection_info.key?(:apikey) # rubocop:disable Metrics/LineLength
  connection_info
end

Instance Method Details

#apikeyObject



144
145
146
# File 'lib/puppet/transport/panos.rb', line 144

def apikey
  api.apikey
end

#commitObject



138
139
140
141
142
# File 'lib/puppet/transport/panos.rb', line 138

def commit
  Puppet.debug('Committing outstanding changes')
  # https://<firewall>/api/?type=commit&cmd=<commit></commit>
  api.job_request('commit', cmd: '<commit></commit>')
end

#delete_config(xpath) ⇒ Object



84
85
86
87
88
# File 'lib/puppet/transport/panos.rb', line 84

def delete_config(xpath)
  Puppet.debug("Deleting #{xpath}")
  # https://<firewall>/api/?key=apikey&type=config&action=delete&xpath=xpath-value
  api.request('config', action: 'delete', xpath: xpath)
end

#edit_config(xpath, document) ⇒ Object



78
79
80
81
82
# File 'lib/puppet/transport/panos.rb', line 78

def edit_config(xpath, document)
  Puppet.debug("Updating #{xpath}")
  # https://<firewall>/api/?key=apikey&type=config&action=edit&xpath=xpath-value&element=element-value
  api.request('config', action: 'edit', xpath: xpath, element: document)
end

#facts(context) ⇒ Object



21
22
23
# File 'lib/puppet/transport/panos.rb', line 21

def facts(context)
  @facts ||= parse_device_facts(fetch_device_facts(context))
end

#fetch_device_facts(context) ⇒ Object



25
26
27
28
29
# File 'lib/puppet/transport/panos.rb', line 25

def fetch_device_facts(context)
  context.debug('Retrieving PANOS Device Facts')
  # https://<firewall>/api/?type=op&cmd=<show><system><info></info></system></show>
  api.request('op', cmd: '<show><system><info/></system></show>')
end

#get_config(xpath) ⇒ Object



66
67
68
69
70
# File 'lib/puppet/transport/panos.rb', line 66

def get_config(xpath)
  Puppet.debug("Retrieving #{xpath}")
  # https://<firewall>/api/?key=apikey&type=config&action=get&xpath=<path-to-config-node>
  api.request('config', action: 'get', xpath: xpath)
end

#import(file_path, category) ⇒ Object



107
108
109
110
111
112
# File 'lib/puppet/transport/panos.rb', line 107

def import(file_path, category)
  Puppet.debug("Importing #{category}")
  # https://<firewall>/api/?key=apikey&type=import&category=category
  # POST: File(file_path)
  api.upload('import', file_path, category: category)
end

#load_config(file_name) ⇒ Object



114
115
116
117
118
# File 'lib/puppet/transport/panos.rb', line 114

def load_config(file_name)
  Puppet.debug('Loading Config')
  # https://<firewall>/api/?type=op&cmd=<load><config><from>file_name</from></config></load>
  api.request('op', cmd: "<load><config><from>#{file_name}</from></config></load>")
end

#move(xpath, name, dst) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/puppet/transport/panos.rb', line 90

def move(xpath, name, dst)
  if dst.empty?
    # perform a check to see if we are already top. PANOS throws an exception if the item is already there
    sibling = get_config(xpath + "/entry[@name='#{name}']/preceding-sibling::entry[1]/@name")
    # https://<firewall>/api/?key=apikey&type=config&action=move&xpath=xpath-value&where=top
    if sibling.elements['/response/result'].attributes.key?('count') &&
       (sibling.elements['/response/result'].attributes['count'].to_i > 0)
      Puppet.debug("moving #{name}")
      api.request('config', action: 'move', xpath: xpath + "/entry[@name='#{name}']", where: 'top')
    end
  else
    Puppet.debug("moving #{name}")
    # https://<firewall>/api/?key=apikey&type=config&action=move&xpath=xpath-value&where=after&dst=<dst>
    api.request('config', action: 'move', xpath: xpath + "/entry[@name='#{name}']", where: 'after', dst: dst)
  end
end

#outstanding_changes?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/puppet/transport/panos.rb', line 126

def outstanding_changes?
  # /api/?type=op&cmd=<check><pending-changes></pending-changes></check>
  result = api.request('op', cmd: '<check><pending-changes></pending-changes></check>')
  result.elements['/response/result'].text == 'yes'
end

#parse_device_facts(response) ⇒ Object



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
# File 'lib/puppet/transport/panos.rb', line 31

def parse_device_facts(response)
  # rubocop:disable Style/StringLiterals
  facts = { "networking" => {} }
  # rubocop:enable Style/StringLiterals

  hostname = response.elements['/response/result/system/hostname'].text
  ip = response.elements['/response/result/system/ip-address'].text
  ip6 = response.elements['/response/result/system/ipv6-address'].text
  mac = response.elements['/response/result/system/mac-address'].text
  model = response.elements['/response/result/system/model'].text
  netmask = response.elements['/response/result/system/netmask'].text
  serial = response.elements['/response/result/system/serial'].text
  uptime = response.elements['/response/result/system/uptime'].text
  version = response.elements['/response/result/system/sw-version'].text
  vsys = response.elements['/response/result/system/multi-vsys'].text

  if hostname
    facts['hostname'] = hostname
    facts['networking']['hostname'] = hostname
  end
  facts['networking']['ip'] = ip if ip
  facts['networking']['ip6'] = ip6 if ip6
  facts['networking']['mac'] = mac if mac
  facts['networking']['netmask'] = netmask if netmask
  facts['operatingsystem'] = model if model
  facts['operatingsystemrelease'] = version if version
  facts['multi-vsys'] = vsys if vsys
  facts['serialnumber'] = serial if serial
  if uptime
    facts['uptime'] = uptime
    facts['system_uptime'] = { 'uptime' => uptime }
  end
  facts
end

#set_config(xpath, document) ⇒ Object



72
73
74
75
76
# File 'lib/puppet/transport/panos.rb', line 72

def set_config(xpath, document)
  Puppet.debug("Writing to #{xpath}")
  # https://<firewall>/api/?key=apikey&type=config&action=set&xpath=xpath-value&element=element-value
  api.request('config', action: 'set', xpath: xpath, element: document)
end

#show_configObject



120
121
122
123
124
# File 'lib/puppet/transport/panos.rb', line 120

def show_config
  Puppet.debug('Retrieving Config')
  # https://<firewall>/api/?type=op&cmd=<show><config><running></running></config></show>
  api.request('op', cmd: '<show><config><running></running></config></show>')
end

#validateObject



132
133
134
135
136
# File 'lib/puppet/transport/panos.rb', line 132

def validate
  Puppet.debug('Validating configuration')
  # https://<firewall>/api/?type=op&cmd=<validate><full></full></validate>
  api.job_request('op', cmd: '<validate><full></full></validate>')
end