arista-eapi 0.10.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/arista/eapi.rb +28 -1
- data/lib/arista/eapi/parser.rb +15 -11
- data/lib/arista/eapi/parser/show.rb +23 -19
- data/lib/arista/eapi/request.rb +28 -24
- data/lib/arista/eapi/switch.rb +28 -24
- data/lib/arista/eapi/version.rb +1 -1
- metadata +1 -2
- data/lib/arista/eapi/format.rb +0 -27
data/lib/arista/eapi.rb
CHANGED
@@ -13,6 +13,33 @@ require 'arista/eapi/version'
|
|
13
13
|
|
14
14
|
module Arista
|
15
15
|
module EAPI
|
16
|
+
JSON_COMMANDS = [
|
17
|
+
'show aliases',
|
18
|
+
'show interfaces',
|
19
|
+
'show interfaces counters',
|
20
|
+
'show interfaces counters discards',
|
21
|
+
'show interfaces status',
|
22
|
+
'show interfaces switchport vlan mapping',
|
23
|
+
'show ip interface',
|
24
|
+
'show ipv6 interface',
|
25
|
+
'show mac address-table',
|
26
|
+
'show mac address-table aging-time',
|
27
|
+
'show management api http-commands',
|
28
|
+
'show management api http-commands certificate',
|
29
|
+
'show monitor server-failure history',
|
30
|
+
'show monitor server-failure servers',
|
31
|
+
'show monitor session',
|
32
|
+
'show privilege',
|
33
|
+
'show sflow',
|
34
|
+
'show sflow interfaces',
|
35
|
+
'show spanning-tree topology status',
|
36
|
+
'show version',
|
37
|
+
'show version license',
|
38
|
+
'show vlan',
|
39
|
+
'show vlan trunk group',
|
40
|
+
]
|
41
|
+
|
42
|
+
|
16
43
|
# Public: Determine the eAPI supported format based on the commands.
|
17
44
|
#
|
18
45
|
# options - List of commands to check format options for.
|
@@ -26,7 +53,7 @@ module Arista
|
|
26
53
|
#
|
27
54
|
# Returns json if all commands support JSON output, otherwise returns text.
|
28
55
|
def self.format_for(*commands)
|
29
|
-
(commands.flatten -
|
56
|
+
(commands.flatten - JSON_COMMANDS).size > 0 ? 'text' : 'json'
|
30
57
|
end
|
31
58
|
end
|
32
59
|
end
|
data/lib/arista/eapi/parser.rb
CHANGED
@@ -1,16 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module Arista
|
2
|
+
module EAPI
|
3
|
+
class Parser
|
4
|
+
def self.parse(command, body)
|
5
|
+
parser_class = command.split.first.downcase.capitalize
|
6
|
+
parser_method = command.gsub(/\s/, '_')
|
5
7
|
|
6
|
-
|
8
|
+
parser = self.const_get(parser_class)
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
if parser.respond_to?(parser_method.to_sym)
|
11
|
+
parser.send(parser_method, body)
|
12
|
+
else
|
13
|
+
body
|
14
|
+
end
|
15
|
+
rescue NameError
|
16
|
+
body
|
17
|
+
end
|
12
18
|
end
|
13
|
-
rescue NameError
|
14
|
-
body
|
15
19
|
end
|
16
20
|
end
|
@@ -1,26 +1,30 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module Arista
|
2
|
+
module EAPI
|
3
|
+
module Parser
|
4
|
+
class Show
|
5
|
+
def self.show_lldp_neighbors(body)
|
6
|
+
lldp = {
|
7
|
+
:last_update => nil,
|
8
|
+
:ports => {}
|
9
|
+
}
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
+
lldp[:last_update] = body.match(/^Last table change time\s+:\s(?<last_update>.*?)$/m)[:last_update]
|
12
|
+
ports = body.match(/.*Neighbor Port ID\s*TTL\s(?<ports>.*)/m)[:ports]
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
14
|
+
unless ports.nil?
|
15
|
+
ports.split("\n").each do |line|
|
16
|
+
name, neighbor_device_id, neighbor_port_id, ttl = line.split
|
17
|
+
lldp[:ports][name] = {
|
18
|
+
:neighbor_device_id => neighbor_device_id,
|
19
|
+
:neighbor_port_id => neighbor_port_id,
|
20
|
+
:ttl => ttl
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
{ :lldp_neighbors => lldp }
|
20
26
|
end
|
21
27
|
end
|
22
|
-
|
23
|
-
{ :lldp_neighbors => lldp }
|
24
28
|
end
|
25
29
|
end
|
26
30
|
end
|
data/lib/arista/eapi/request.rb
CHANGED
@@ -1,30 +1,34 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Arista
|
2
|
+
module EAPI
|
3
|
+
class Request
|
4
|
+
attr_accessor :switch, :commands
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def initialize(switch, *commands)
|
7
|
+
self.switch = switch
|
8
|
+
self.commands = commands
|
9
|
+
end
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
11
|
+
def payload
|
12
|
+
@payload ||= JSON.generate({
|
13
|
+
:jsonrpc => '2.0',
|
14
|
+
:method => 'runCmds',
|
15
|
+
:id => 1,
|
16
|
+
:params => {
|
17
|
+
:version => 1,
|
18
|
+
:cmds => commands,
|
19
|
+
:format => Arista::EAPI.format_for(commands)
|
20
|
+
},
|
21
|
+
})
|
22
|
+
end
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
24
|
+
def execute
|
25
|
+
Arista::EAPI::Response.new(commands, RestClient.post(switch.url, payload))
|
26
|
+
end
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
28
|
+
def self.execute!(switch, *commands)
|
29
|
+
req = self.new(switch, *commands)
|
30
|
+
req.execute
|
31
|
+
end
|
32
|
+
end
|
29
33
|
end
|
30
34
|
end
|
data/lib/arista/eapi/switch.rb
CHANGED
@@ -1,32 +1,36 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Arista
|
2
|
+
module EAPI
|
3
|
+
class Switch
|
4
|
+
attr_accessor :hostname, :user, :password, :protocol, :url, :attributes
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def initialize(hostname, user, password, protocol = 'https')
|
7
|
+
self.attributes = {}
|
8
|
+
self.hostname = hostname
|
9
|
+
self.user = user
|
10
|
+
self.password = password
|
11
|
+
self.protocol = protocol
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
userpass = [ CGI.escape(user), CGI.escape(password) ].join(':')
|
14
|
+
self.url = "#{protocol}://#{userpass}@#{hostname}/command-api"
|
15
|
+
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
def interfaces
|
18
|
+
run('show interfaces')
|
19
|
+
attributes[:interfaces]
|
20
|
+
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
def version
|
23
|
+
run('show version').first
|
24
|
+
end
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
|
26
|
+
def update_attributes!(results)
|
27
|
+
results.each { |result| attributes.merge!(result) if result.is_a?(Hash) }
|
28
|
+
end
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
def run(*commands)
|
31
|
+
request = Arista::EAPI::Request.new(self, *commands)
|
32
|
+
self.update_attributes!(request.execute.results)
|
33
|
+
end
|
34
|
+
end
|
31
35
|
end
|
32
36
|
end
|
data/lib/arista/eapi/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arista-eapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.10.
|
4
|
+
version: 0.10.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -90,7 +90,6 @@ files:
|
|
90
90
|
- arista-eapi.gemspec
|
91
91
|
- examples/save_config.rb
|
92
92
|
- lib/arista/eapi.rb
|
93
|
-
- lib/arista/eapi/format.rb
|
94
93
|
- lib/arista/eapi/parser.rb
|
95
94
|
- lib/arista/eapi/parser/show.rb
|
96
95
|
- lib/arista/eapi/request.rb
|
data/lib/arista/eapi/format.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
class Arista::EAPI::Format
|
2
|
-
JSON_COMMANDS = [
|
3
|
-
'show aliases',
|
4
|
-
'show interfaces',
|
5
|
-
'show interfaces counters',
|
6
|
-
'show interfaces counters discards',
|
7
|
-
'show interfaces status',
|
8
|
-
'show interfaces switchport vlan mapping',
|
9
|
-
'show ip interface',
|
10
|
-
'show ipv6 interface',
|
11
|
-
'show mac address-table',
|
12
|
-
'show mac address-table aging-time',
|
13
|
-
'show management api http-commands',
|
14
|
-
'show management api http-commands certificate',
|
15
|
-
'show monitor server-failure history',
|
16
|
-
'show monitor server-failure servers',
|
17
|
-
'show monitor session',
|
18
|
-
'show privilege',
|
19
|
-
'show sflow',
|
20
|
-
'show sflow interfaces',
|
21
|
-
'show spanning-tree topology status',
|
22
|
-
'show version',
|
23
|
-
'show version license',
|
24
|
-
'show vlan',
|
25
|
-
'show vlan trunk group',
|
26
|
-
]
|
27
|
-
end
|