Class: JunosSpace::SD::Device

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

Constant Summary collapse

@@device_uri =
'/api/juniper/sd/device-management/devices'

Instance Method Summary collapse

Instance Method Details

#info(name) ⇒ Object

info(name)

Returns a Hash of resultrmation about the given device ‘name’. For zones, it returns Hash where the zone name is the key, and an array of interfaces that are in said zone as the value.

For interfaces, it returns a Hash where the interface name is the key, and the IP address of that interface is the value.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
104
105
106
107
108
109
110
111
112
113
# File 'lib/junos-space-api/sd/device.rb', line 44

def info(name)
  result = {}
  devices = self.list
  
  begin
    res = RestClient.get("#{JunosSpace.base_uri}#{@@device_uri}/#{devices[name]}")
    doc = Nokogiri::XML::Document.parse(res)
    
    if !devices[name]
      result['status'] = 'No device(s) found.'
    else
      doc.xpath('//device').each do |device|
        result["management-status"] = device.xpath('management-status').text,
        result["name"] = device.xpath('name').text,
        result["platform"] = device.xpath('platform').text,
        result["ip"] = device.xpath('device-ip').text,
        result["config-status"] = device.xpath('configuration-status').text,
        result["connection-status"] = device.xpath('connection-status').text,
        result["family"] = device.xpath('device-family').text,
        result["version"] = device.xpath('software-release').text,
        result["id"] = device.xpath('id').text
        
        zone_uri = device.xpath('Zone/@href').text
        int_uri = device.xpath('Interfaces/@href').text

        zone_res = RestClient.get("#{JunosSpace.base_uri}#{zone_uri}")
        zone_doc = Nokogiri::XML::Document.parse(zone_res)
        int_res = RestClient.get("#{JunosSpace.base_uri}#{int_uri}")
        int_doc = Nokogiri::XML::Document.parse(int_res)
        
        zones = []
        zone_doc.xpath('//zone').each do |zone|
          zone_ints = []
          zone_result = {}
          name = zone.xpath('name').text
          
          zone.xpath('./interfaces').each do |int|
            int.xpath('./interface').each do |interface|
              zone_ints << interface.text
            end
          end
          
          zone_result[name] = zone_ints
          zones << zone_result
        end
        
        result["zones"] = zones

        interfaces = []
        int_doc.xpath('//interface').each do |int|
          int_result = {}
          name = int.xpath('name').text
          ip = int.xpath('ip-addr').text
          mask = int.xpath('ip-netmask').text
          int_result[name] = "#{ip}/#{mask}"
          
          interfaces << int_result
        end
        
        result["interfaces"] = interfaces
      end
    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 device objects in Space. The name of the device is the key, and the ID is the value.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/junos-space-api/sd/device.rb', line 13

def list
  result = {}
  
  begin
    res = RestClient.get("#{JunosSpace.base_uri}#{@@device_uri}")
    doc = Nokogiri::XML::Document.parse(res)

    doc.xpath('//device').each do |device|
      name = device.xpath('name').text
      id = device.xpath('id').text
      
      result[name] = id
    end
    
    return result
  rescue RestClient::Unauthorized
    result['status'] = '401 Error - Auth failure (bad username/password).'
    
    return result
  end
end