Module: Rbeapi::Client

Defined in:
lib/rbeapi/client.rb

Overview

Rbeapi::Client

Defined Under Namespace

Classes: Config, Node

Constant Summary collapse

DEFAULT_TRANSPORT =
'https'.freeze
TRANSPORTS =
{ 'http' => 'Rbeapi::Eapilib::HttpEapiConnection',
  'https' => 'Rbeapi::Eapilib::HttpsEapiConnection',
  'http_local' => 'Rbeapi::Eapilib::HttpLocalEapiConenction',
  'socket' => 'Rbeapi::Eapilib::SocketEapiConnection' }
.freeze

Class Method Summary collapse

Class Method Details

.configConfig

Returns the currently loaded config object. This function will create a new instance of the config object if one doesn’t already exist.

Returns:

  • (Config)

    Returns an instance of Config used for working with the eapi.conf file.



60
61
62
63
64
# File 'lib/rbeapi/client.rb', line 60

def config
  return @config if @config
  @config = Config.new
  @config
end

.config_for(name) ⇒ Hash?

Returns the configuration options for the named connection from the loaded configuration. The configuration name is specified as the string right of the colon in the section name.

Parameters:

  • name (String)

    The connection name to return from the loaded configuration.

Returns:

  • (Hash, nil)

    This method will return the configuration hash for the named configuration if found. If the name is not found, then nil is returned.



89
90
91
# File 'lib/rbeapi/client.rb', line 89

def config_for(name)
  config.get_connection(name)
end

.connect(opts = {}) ⇒ Rbeapi::Connection

Builds a connection object to a remote node using the specified options and return an instance of Rbeapi::Connection. All configuration options can be passed via the :opts param.

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a message with.

Options Hash (opts):

  • host (String)

    The IP address or hostname of the remote eAPI endpoint.

  • username (String)

    The username to use to authenticate the eAPI connection with.

  • password (String)

    The password to use to authenticate the eAPI connection with.

  • enablepwd (String)

    The enable password (if defined) to pass to the remote node to enter privilege mode.

  • use_ssl (String)

    Specifies whether or not to use the HTTP or HTTPS protocol.

  • port (String)

    The port to connect to. If not specified The port is automatically determined based on the protocol used (443 for https, 80 for http).

Returns:

  • (Rbeapi::Connection)

    Returns an instance of Rbeapi::Connection using the specified configuration options.



144
145
146
147
# File 'lib/rbeapi/client.rb', line 144

def connect(opts = {})
  transport = opts.fetch(:transport, DEFAULT_TRANSPORT)
  make_connection(transport, opts)
end

.connect_to(name) ⇒ Rbeapi::Node?

Retrieves the node config from the loaded configuration file and returns a Rbeapi::Node instance for working with the remote node.

Parameters:

  • name (String)

    The named configuration to use for creating the connection to the remote node.

Returns:

  • (Rbeapi::Node, nil)

    Returns an instance of Rbeapi::Node. If the named configuration is not found then nil is returned.



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rbeapi/client.rb', line 102

def connect_to(name)
  config_entry = config_for(name)
  return nil unless config_entry
  config = config_entry.dup
  config['host'] = name if config['host'] == '*'
  config = Rbeapi::Utils.transform_keys_to_symbols(config)
  connection = connect config
  Node.new(connection)
  node = Node.new(connection)
  enablepwd = config.fetch(:enablepwd, nil)
  node.enable_authentication(enablepwd) if enablepwd
  node
end

.load_config(conf) ⇒ Object

load_config overrides the default conf file loaded in the config instances using the supplied conf argument as the conf file. This method will clear out an previously loaded configuration and replace all entries with the contents of the supplied file.

Parameters:

  • conf (String)

    The full path to the conf file to load into the config instance.



74
75
76
# File 'lib/rbeapi/client.rb', line 74

def load_config(conf)
  config.read(conf)
end

.make_connection(transport, opts = {}) ⇒ Rbeapi::EapiConnection

Creates a connection instance that can either be used directly or passed to a Node instance.

Parameters:

  • transport (String)

    The name of the transport to create.

  • opts (Hash) (defaults to: {})

    The options used to create the transport.

Returns:

  • (Rbeapi::EapiConnection)

    A instance of a connection object.



158
159
160
161
162
# File 'lib/rbeapi/client.rb', line 158

def make_connection(transport, opts = {})
  klass = TRANSPORTS.fetch(transport)
  cls = Rbeapi::Utils.class_from_string(klass)
  cls.new(opts)
end