Class: JunosSpace::Platform::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/junos-space-api/platform/device.rb

Constant Summary collapse

@@devices_uri =
'/api/space/device-management/devices'
@@discover_uri =
'/api/space/device-management/discover-devices'
@@device_headers =
{
  :content_type => 'application/vnd.net.juniper.space.device-management.discover-devices+xml;version=2;charset=UTF-8'
}
@@queue_headers =
{
  :content_type => 'application/hornetq.jms.queue+xml'
}

Instance Method Summary collapse

Instance Method Details

#add(data) ⇒ Object

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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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

#listObject

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.



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
# 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