Module: Nexpose::DataTable

Defined in:
lib/nexpose/data_table.rb

Overview

Data table functions which extract data from the Nexpose UI.

The functions in this file are utility functions for accessing data in the same manner as the Nexpose UI. These functions are not designed for external use, but to aid exposing data through other methods in the gem.

Class Method Summary collapse

Class Method Details

._clean_data_table!(arr, id) ⇒ Object

Clean up the ‘type-safe’ IDs returned by many table requests. This is a destructive operation, changing the values in the underlying hash.

Example usage:

# For data like: {"assetID"=>{"ID"=>2818}, "assetIP"=>"10.4.16.1", ...}
_clean_data_table!(data, 'assetID')

Parameters:

  • arr (Array[Hash])

    Array of hashes representing a data table.

  • id (String)

    Key value of a type-safe ID to clean up.



113
114
115
116
117
# File 'lib/nexpose/data_table.rb', line 113

def _clean_data_table!(arr, id)
  arr.reduce([]) do |acc, hash|
    acc << _clean_id!(hash, id)
  end
end

._clean_id!(hash, id) ⇒ Object

Convert a type-safe ID into a regular ID inside a hash.

Parameters:

  • hash (Hash)

    Hash map containing a type-safe ID as one key.

  • id (String)

    Key value of a type-safe ID to clean up.



124
125
126
127
128
129
130
131
132
# File 'lib/nexpose/data_table.rb', line 124

def _clean_id!(hash, id)
  hash.each_pair do |key, value|
    if key == id
      hash[key] = value['ID']
    else
      hash[key] = value
    end
  end
end

._dyn_headers(response) ⇒ Object

Parse headers out of a dyntable response.



76
77
78
79
80
81
82
# File 'lib/nexpose/data_table.rb', line 76

def _dyn_headers(response)
  headers = []
  response.elements.each('DynTable/MetaData/Column') do |header|
    headers << header.attributes['name']
  end
  headers
end

._dyn_record(row) ⇒ Object

Parse records out of the row of a dyntable.



94
95
96
97
98
99
100
# File 'lib/nexpose/data_table.rb', line 94

def _dyn_record(row)
  record = []
  row.elements.each('td') do |value|
    record << (value.text ? value.text.to_s : '')
  end
  record
end

._dyn_rows(response) ⇒ Object

Parse rows out of a dyntable into an array of values.



85
86
87
88
89
90
91
# File 'lib/nexpose/data_table.rb', line 85

def _dyn_rows(response)
  rows = []
  response.elements.each('DynTable/Data/tr') do |row|
    rows << _dyn_record(row)
  end
  rows
end

._get_dyn_table(console, address, payload = nil) ⇒ Array[Hash]

Helper method to get a Dyntable into a consumable Ruby object.

Example usage:

DataTable._get_dyn_table(@console, '/data/asset/os/dyntable.xml?tableID=OSSynopsisTable')

Parameters:

  • console (Connection)

    API connection to a Nexpose console.

  • address (String)

    Tag address with parameters relative to host:port

Returns:

  • (Array[Hash])

    array of hashes representing the requested table.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nexpose/data_table.rb', line 62

def _get_dyn_table(console, address, payload = nil)
  if payload
    response = AJAX.post(console, address, payload)
  else
    response = AJAX.get(console, address)
  end
  response = REXML::Document.new(response)

  headers = _dyn_headers(response)
  rows = _dyn_rows(response)
  rows.map { |row| Hash[headers.zip(row)] }
end

._get_json_table(console, address, parameters, page_size = 500, records = nil) ⇒ Array[Hash]

Helper method to get the YUI tables into a consumable Ruby object.

Example usage:

DataTable._get_json_table(@console,
                          '/data/asset/site',
                          { 'sort' => 'assetName',
                            'table-id' => 'site-assets',
                            'siteID' => site_id })

Parameters:

  • console (Connection)

    API connection to a Nexpose console.

  • address (String)

    Controller address relative to host:port

  • parameters (Hash)

    Parameters that need to be sent to the controller

  • pagination (Integer)

    size

  • number (Integer)

    of records to return, gets all if not specified The following attributes need to be provided:

    'sort' Column to sort by
    'table-id' The ID of the table to get from this controller
    

Returns:

  • (Array[Hash])

    An array of hashes representing the requested table.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nexpose/data_table.rb', line 31

def _get_json_table(console, address, parameters, page_size = 500, records = nil)
  parameters['dir'] = 'DESC'
  parameters['startIndex'] = -1
  parameters['results'] = -1

  post = AJAX.form_post(console, address, parameters)
  data = JSON.parse(post)
  total = records || data['totalRecords']
  return [] if total == 0

  rows = []
  parameters['results'] = page_size
  while rows.length < total
    parameters['startIndex'] = rows.length

    data = JSON.parse(AJAX.form_post(console, address, parameters))
    rows.concat data['records']
  end
  rows
end