arista-eapi 0.10.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 - Arista::EAPI::Format::JSON_COMMANDS).size > 0 ? 'text' : 'json'
56
+ (commands.flatten - JSON_COMMANDS).size > 0 ? 'text' : 'json'
30
57
  end
31
58
  end
32
59
  end
@@ -1,16 +1,20 @@
1
- class Arista::EAPI::Parser
2
- def self.parse(command, body)
3
- parser_class = command.split.first.downcase.capitalize
4
- parser_method = command.gsub(/\s/, '_')
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
- parser = self.const_get(parser_class)
8
+ parser = self.const_get(parser_class)
7
9
 
8
- if parser.respond_to?(parser_method.to_sym)
9
- parser.send(parser_method, body)
10
- else
11
- body
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
- class Arista::EAPI::Parser::Show
2
- class << self
3
- def show_lldp_neighbors(body)
4
- lldp = {
5
- :last_update => nil,
6
- :ports => {}
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
- lldp[:last_update] = body.match(/^Last table change time\s+:\s(?<last_update>.*?)$/m)[:last_update]
10
- ports = body.match(/.*Neighbor Port ID\s*TTL\s(?<ports>.*)/m)[:ports]
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
- unless ports.nil?
13
- ports.split("\n").each do |line|
14
- name, neighbor_device_id, neighbor_port_id, ttl = line.split
15
- lldp[:ports][name] = {
16
- :neighbor_device_id => neighbor_device_id,
17
- :neighbor_port_id => neighbor_port_id,
18
- :ttl => ttl
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
@@ -1,30 +1,34 @@
1
- class Arista::EAPI::Request
2
- attr_accessor :switch, :commands
1
+ module Arista
2
+ module EAPI
3
+ class Request
4
+ attr_accessor :switch, :commands
3
5
 
4
- def initialize(switch, *commands)
5
- self.switch = switch
6
- self.commands = commands
7
- end
6
+ def initialize(switch, *commands)
7
+ self.switch = switch
8
+ self.commands = commands
9
+ end
8
10
 
9
- def payload
10
- @payload ||= JSON.generate({
11
- :jsonrpc => '2.0',
12
- :method => 'runCmds',
13
- :id => 1,
14
- :params => {
15
- :version => 1,
16
- :cmds => commands,
17
- :format => Arista::EAPI.format_for(commands)
18
- },
19
- })
20
- end
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
- def execute
23
- Arista::EAPI::Response.new(commands, RestClient.post(switch.url, payload))
24
- end
24
+ def execute
25
+ Arista::EAPI::Response.new(commands, RestClient.post(switch.url, payload))
26
+ end
25
27
 
26
- def self.execute!(switch, *commands)
27
- req = self.new(switch, *commands)
28
- req.execute
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
@@ -1,32 +1,36 @@
1
- class Arista::EAPI::Switch
2
- attr_accessor :hostname, :user, :password, :protocol, :url, :attributes
1
+ module Arista
2
+ module EAPI
3
+ class Switch
4
+ attr_accessor :hostname, :user, :password, :protocol, :url, :attributes
3
5
 
4
- def initialize(hostname, user, password, protocol = 'https')
5
- self.attributes = {}
6
- self.hostname = hostname
7
- self.user = user
8
- self.password = password
9
- self.protocol = protocol
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
- userpass = [ CGI.escape(user), CGI.escape(password) ].join(':')
12
- self.url = "#{protocol}://#{userpass}@#{hostname}/command-api"
13
- end
13
+ userpass = [ CGI.escape(user), CGI.escape(password) ].join(':')
14
+ self.url = "#{protocol}://#{userpass}@#{hostname}/command-api"
15
+ end
14
16
 
15
- def interfaces
16
- run('show interfaces')
17
- attributes[:interfaces]
18
- end
17
+ def interfaces
18
+ run('show interfaces')
19
+ attributes[:interfaces]
20
+ end
19
21
 
20
- def version
21
- run('show version').first
22
- end
22
+ def version
23
+ run('show version').first
24
+ end
23
25
 
24
- def update_attributes!(results)
25
- results.each { |result| attributes.merge!(result) if result.is_a?(Hash) }
26
- end
26
+ def update_attributes!(results)
27
+ results.each { |result| attributes.merge!(result) if result.is_a?(Hash) }
28
+ end
27
29
 
28
- def run(*commands)
29
- request = Arista::EAPI::Request.new(self, *commands)
30
- self.update_attributes!(request.execute.results)
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
@@ -1,5 +1,5 @@
1
1
  module Arista
2
2
  module EAPI
3
- VERSION = "0.10.0"
3
+ VERSION = "0.10.1"
4
4
  end
5
5
  end
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.0
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
@@ -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