Class: Arista::EAPI::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/arista/eapi/switch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, user, password, protocol = 'https') ⇒ Switch

Returns a new instance of Switch.



6
7
8
9
10
11
12
13
14
15
# File 'lib/arista/eapi/switch.rb', line 6

def initialize(hostname, user, password, protocol = 'https')
  self.attributes = {}
  self.hostname = hostname
  self.user = user
  self.password = password
  self.protocol = protocol

  userpass = [ CGI.escape(user), CGI.escape(password) ].join(':')
  self.url = "#{protocol}://#{userpass}@#{hostname}/command-api"
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/arista/eapi/switch.rb', line 4

def attributes
  @attributes
end

#hostnameObject

Returns the value of attribute hostname.



4
5
6
# File 'lib/arista/eapi/switch.rb', line 4

def hostname
  @hostname
end

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/arista/eapi/switch.rb', line 4

def password
  @password
end

#protocolObject

Returns the value of attribute protocol.



4
5
6
# File 'lib/arista/eapi/switch.rb', line 4

def protocol
  @protocol
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'lib/arista/eapi/switch.rb', line 4

def url
  @url
end

#userObject

Returns the value of attribute user.



4
5
6
# File 'lib/arista/eapi/switch.rb', line 4

def user
  @user
end

Instance Method Details

#interfacesObject



17
18
19
20
# File 'lib/arista/eapi/switch.rb', line 17

def interfaces
  run!('show interfaces')
  attributes[:interfaces]
end

#run(commands, options = {}) ⇒ Object

Public: Run the

options - Options to pass to the eAPI call.

:format - The format for the eAPI response. Accepts the strings
          "json" or "text". (Defaults to json)


51
52
53
54
# File 'lib/arista/eapi/switch.rb', line 51

def run(commands, options={})
  request = Arista::EAPI::Request.new(self, commands, options)
  self.update_attributes!(request.execute.results)
end

#run!(command, options = {}) ⇒ Object

Public: Runs a single command and returns the first result.

options - Options to pass to the eAPI call.

:format - The format for the eAPI response. Accepts the strings
          "json" or "text". (Defaults to json)

Examples

switch.run!('show version')
#=> {:model_name=>"DCS-7048T-A-R", :internal_version=>"4.12.0-1244071.EOS4120", :system_mac_address=>"00:1c:73:16:c2:c8", :serial_number=>"redacted", :mem_total=>4009152, :bootup_timestamp=>1368735672.690161, :mem_free=>1848284, :version=>"4.12.0", :architecture=>"i386", :internal_build_id=>"c25ec8ea-cb8f-40a8-af0b-d11eaa94d57c", :hardware_revision=>"01.04"}


41
42
43
# File 'lib/arista/eapi/switch.rb', line 41

def run!(command, options={})
  run([command], options).first
end

#update_attributes!(results) ⇒ Object



26
27
28
# File 'lib/arista/eapi/switch.rb', line 26

def update_attributes!(results)
  results.each { |result| attributes.merge!(result) if result.is_a?(Hash) }
end

#versionObject



22
23
24
# File 'lib/arista/eapi/switch.rb', line 22

def version
  run!('show version')
end