class JunosSpace::Platform::Device

Public Instance Methods

add(data) click to toggle source

add(data)

Add a new device to the Junos Space database, with the given information in 'data'. 'data' is a Hash table with the following structure:

`host => IP address or hostname of device`, `snmp => Use SNMP during discovery: 'community string' or 'false'`, `user => SSH username for device`, `pass => SSH password for device`

Returns a Hash with 'status' as the key, and the job ID as the value.

# File lib/junos-space-api/platform/device.rb, line 64
def add(data)
  result = {}
  
  begin
    rand = Random.rand(999999)
    server = JunosSpace.base_uri.split('@')[1]
    queue_xml = "<queue name='d#{rand}'><durable>false</durable></queue>"
    RestClient.post("#{JunosSpace.base_uri}/api/hornet-q/queues", queue_xml, @@queue_headers)
    queue_url = "https://#{server}/api/hornet-q/queues/jms.queue.d#{rand}"
    
    xml = "<discover-devices>"
    
    if IPAddress.valid?(data['host'])
      xml += "<ipAddressDiscoveryTarget><ipAddress>#{data['host']}</ipAddress></ipAddressDiscoveryTarget>"
    else
      xml += "<hostNameDiscoveryTarget><hostName>#{data['host']}</hostName></hostNameDiscoveryTarget>"
    end
    
    if data['snmp'] != 'false'
      xml += "<useSnmp>true</useSnmp><snmpV1Setting><communityName>#{data['snmp']}</communityName></snmpV1Setting>"
    else
      xml += "<manageDiscoveredSystemsFlag>true</manageDiscoveredSystemsFlag><sshCredential>" +
             "<userName>#{data['user']}</userName><password>#{data['pass']}</password></sshCredential>" +
             "</discover-devices>"
    end
    
    res = RestClient.post("#{JunosSpace.base_uri}#{@@discover_uri}?queue-url=#{queue_url}", xml, @@device_headers)
    doc = Nokogiri::XML::Document.parse(res)
    
    if res.code == 202
      result['status'] = doc.xpath('//task/id').text
    end
    
    return result
  rescue RestClient::Unauthorized
    result['status'] = '401 Error - Auth failure (bad username/password).'
    
    return result
  end
end
list() click to toggle source

list

Returns a Hash of all of the devices in the Junos Space database. The device name is the key, and value is an array of the following: device id, family, Junos version, platform, serial number, connection status, IP address, and managed status.

# File lib/junos-space-api/platform/device.rb, line 23
def list
  result = {}
  
  begin
    res = RestClient.get("#{JunosSpace.base_uri}#{@@devices_uri}")
    doc = Nokogiri::XML::Document.parse(res)

    doc.xpath('//device').each do |device|
      stats = []
      stats << device.xpath('@key').text
      stats << device.xpath('deviceFamily').text
      stats << device.xpath('OSVersion').text
      stats << device.xpath('platform').text
      stats << device.xpath('serialNumber').text
      stats << device.xpath('connectionStatus').text
      stats << device.xpath('ipAddr').text
      stats << device.xpath('managedStatus').text
      
      result[device.xpath('name').text] = stats
    end
    
    return result
  rescue RestClient::Unauthorized
    result['status'] = '401 Error - Auth failure (bad username/password).'
    
    return result
  end
end