Method: Cisco::Client::NXAPI#initialize

Defined in:
lib/cisco_node_utils/client/nxapi/client.rb

#initialize(**kwargs) ⇒ NXAPI

Constructor for Client. By default this connects to the local unix domain socket. If you need to connect to a remote device, you must provide the host/username/password parameters.



41
42
43
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
# File 'lib/cisco_node_utils/client/nxapi/client.rb', line 41

def initialize(**kwargs)
  # rubocop:disable Style/HashSyntax
  super(data_formats: [:nxapi_structured, :cli],
        platform:     :nexus,
        **kwargs)
  # rubocop:enable Style/HashSyntax
  # Default: connect to unix domain socket on localhost, if available
  if @host.nil?
    unless File.socket?(NXAPI_UDS)
      fail Cisco::ConnectionRefused, \
           "No host specified but no UDS found at #{NXAPI_UDS} either"
    end
    # net_http_unix provides NetX::HTTPUnix, a small subclass of Net::HTTP
    # which supports connection to local unix domain sockets. We need this
    # in order to run natively under NX-OS but it's not needed for off-box
    # unit testing where the base Net::HTTP will meet our needs.
    require 'net_http_unix'
    @http = NetX::HTTPUnix.new('unix://' + NXAPI_UDS)
  else
    # Remote connection. This is primarily expected
    # when running e.g. from a Unix server as part of Minitest.
    @http = Net::HTTP.new(@host)
  end
  # The default read time out is 60 seconds, which may be too short for
  # scaled configuration to apply. Change it to 300 seconds, which is
  # also used as the default config by firefox.
  @http.read_timeout = 300
  @address = @http.address

  # Make sure we can actually connect to the socket
  get(command: 'show hostname')
end