class PaloAlto::XML::Op

Public Instance Methods

escape_xpath_tag(tag) click to toggle source
# File lib/palo_alto/op.rb, line 35
def escape_xpath_tag(tag)
  if tag.to_s.include?('-') # https://stackoverflow.com/questions/48628259/nokogiri-how-to-name-a-node-comment
    tag
  else
    tag.to_s + "_"
  end
end
execute(obj, additional_payload = {}) click to toggle source
# File lib/palo_alto/op.rb, line 11
def execute(obj, additional_payload = {})

  cmd = to_xml(obj)

  if obj=='commit' || obj.keys.first.to_sym == :commit
    type='commit'
    action='panorama'
  elsif obj=='commit-all' || obj.keys.first.to_sym == :'commit-all'
    type='commit'
    action='all'
  else
    type='op'
    action='panorama'
  end

  payload = {
    type:   type,
    action: action,
    cmd:   cmd
  }.merge(additional_payload)

  XML.execute(payload)
end
to_xml(obj) click to toggle source
# File lib/palo_alto/op.rb, line 107
def to_xml(obj)
  builder = Nokogiri::XML::Builder.new{|xml|
    xml_builder(xml, @@ops, obj)
  }
  builder.doc.root.to_xml
end
xml_builder(xml, ops, obj) click to toggle source
# File lib/palo_alto/op.rb, line 43
def xml_builder(xml, ops, obj)
  if obj.is_a?(String)
    section = obj
    data = nil
  elsif obj.is_a?(Hash)
    section = obj.keys.first
    data = obj[section]
  else
    raise obj.pretty_inspect
  end

  unless ops.has_key?(section.to_s)
    err = "Error #{section.to_s} does not exist. Valid: " + ops.keys.pretty_inspect
    raise err
  end

  ops_tree = ops[section.to_s]

  section = escape_xpath_tag(section)

  case ops_tree[:obj]
  when :element
    xml.public_send(section, data)
  when :array
    xml.public_send(section) {
      data.each{|el|
        key = ops_tree.keys.first
        xml.public_send(escape_xpath_tag(key), el)
      }
    }
  when :sequence
    if data==nil
      xml.send(section)
    elsif data.is_a?(Hash)
      xml.send(section){
        xml_builder(xml, ops_tree, data)
      }
    else # array

      if data.is_a?(Array)
        attr = data.find { |child| child.is_a?(Hash) && ops_tree[child.keys.first.to_s][:obj]==:'attr-req' }
        data.delete(attr)
      else
        attr = {}
      end

      xml.public_send(section, attr){
        data.each{|child|
          xml_builder(xml, ops_tree, child)
        }
      }
    end
  when :union
    k,v=obj.first
    xml.send("#{k}_"){
      xml_builder(xml, ops_tree, v)
    }
  else
    raise ops_tree[:obj].pretty_inspect
  end
  xml
end