Class: Cisco::Client::NXAPI

Inherits:
Cisco::Client show all
Defined in:
lib/cisco_node_utils/client/nxapi.rb,
lib/cisco_node_utils/client/nxapi/client.rb

Overview

Class representing an HTTP client connecting to a NXAPI server.

Constant Summary collapse

NXAPI_UDS =

Location of unix domain socket for NXAPI localhost

'/tmp/nginx_local/nginx_1_be_nxapi.sock'
NXAPI_REMOTE_URI_PATH =

NXAPI listens for remote connections to “http://<switch IP>/ins” NXAPI listens for local connections to “http://<UDS>/ins_local”

'/ins'
NXAPI_UDS_URI_PATH =
'/ins_local'
NXAPI_VERSION =

Latest supported version is 1.0

'1.0'

Instance Attribute Summary

Attributes inherited from Cisco::Client

#cache_auto, #data_formats, #platform

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Cisco::Client

#cache_auto?, #cache_enable=, #cache_enable?, clients, create, filter_cli, filter_data, find_subconfig, handle_errors, #inspect, #munge_to_array, munge_to_array, register_client, silence_warnings, #supports?, to_regexp, #to_s

Constructor Details

#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

Class Method Details

.validate_args(**kwargs) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cisco_node_utils/client/nxapi/client.rb', line 74

def self.validate_args(**kwargs)
  super
  if kwargs[:host].nil?
    # Connection to UDS - no username or password either
    fail ArgumentError unless kwargs[:username].nil? && kwargs[:password].nil?
  else
    # Connection to remote system - username and password are required
    fail TypeError, 'username is required' if kwargs[:username].nil?
    fail TypeError, 'password is required' if kwargs[:password].nil?
  end
end

Instance Method Details

#cache_flushObject

Clear the cache of CLI output results.

If cache_auto is true (default) then this will be performed automatically whenever a set() is called, but providers may also call this to explicitly force the cache to be cleared.



91
92
93
94
95
96
97
# File 'lib/cisco_node_utils/client/nxapi/client.rb', line 91

def cache_flush
  @cache_hash = {
    'cli_conf'       => {},
    'cli_show'       => {},
    'cli_show_ascii' => {},
  }
end

#get(data_format: :cli, command: nil, context: nil, value: nil) ⇒ String, Hash

Get the given state from the device.

Unlike set() this will not clear the CLI cache; multiple calls with the same parameters may return cached data rather than querying the device repeatedly.

Parameters:

  • data_format (defaults to: :cli)

    one of Cisco::DATA_FORMATS. Default is :cli

  • command (String) (defaults to: nil)

    the show command to execute

  • context (String, Array<String>) (defaults to: nil)

    Context to refine the results

  • value (String) (defaults to: nil)

    Specific key to look up

Returns:

  • (String, Hash)

Raises:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cisco_node_utils/client/nxapi/client.rb', line 134

def get(data_format: :cli,
        command:     nil,
        context:     nil,
        value:       nil)
  context = munge_to_array(context)
  super
  if data_format == :cli
    output = req('cli_show_ascii', command)
    return self.class.filter_cli(cli_output: output,
                                 context:    context,
                                 value:      value)
  elsif data_format == :nxapi_structured
    output = req('cli_show', command)
    return self.class.filter_data(data: output,
                                  keys: context + munge_to_array(value))
  else
    fail TypeError
  end
end

#set(data_format: :cli, context: nil, values: nil) ⇒ Object

Configure the given CLI command(s) on the device.

Parameters:

  • data_format (defaults to: :cli)

    one of Cisco::DATA_FORMATS. Default is :cli

  • context (String, Array<String>) (defaults to: nil)

    Zero or more configuration commands used to enter the desired CLI sub-mode

  • values (String, Array<String>) (defaults to: nil)

    One or more commands to enter within the CLI sub-mode.

Raises:



108
109
110
111
112
113
114
115
116
117
# File 'lib/cisco_node_utils/client/nxapi/client.rb', line 108

def set(data_format: :cli,
        context: nil,
        values: nil)
  # we don't currently support nxapi_structured for configuration
  fail Cisco::RequestNotSupported if data_format == :nxapi_structured
  context = munge_to_array(context)
  values = munge_to_array(values)
  super
  req('cli_conf', context + values)
end