Class: Cisco::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/util/node.rb

Overview

class Cisco::Node Pseudo-singleton representing the network node (switch/router) that is running this code. The singleton is lazily instantiated, meaning that it doesn’t exist until some client requests it (with Node.instance(…))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_class) ⇒ Node

Returns a new instance of Node.



64
65
66
# File 'lib/util/node.rb', line 64

def initialize(client_class)
  @client = Cisco::Client.create(client_class)
end

Instance Attribute Details

#clientObject (readonly)

Here and below are implementation details and private APIs that most providers shouldn’t need to know about or use.



36
37
38
# File 'lib/util/node.rb', line 36

def client
  @client
end

Class Method Details

.instance(client_class = nil) ⇒ Object

Return a node instance that wraps a client of the specified class.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/util/node.rb', line 39

def self.instance(client_class=nil)
  return @instance_hash[client_class] ||= new(client_class) unless client_class.nil?

  # just return one of the cached instances
  return @instance_hash[hash.keys[0]] unless @instance_hash.empty?

  # no nodes currently cached, so create one
  debug 'Attempting to create a client (type not specified)...'
  env_names = Cisco::Util::Environment.environment_names

  Cisco::Client.clients.each do |c|
    client_env_name = Cisco::Client.environment_name(c)
    if env_names.include?(client_env_name)
      debug "Environment configuration found for client #{c}"
      return instance(c)
    end
  end

  error 'No clients configured'
end

.instance_exists(client_class) ⇒ Object



60
61
62
# File 'lib/util/node.rb', line 60

def self.instance_exists(client_class)
  !@instance_hash[client_class].nil?
end

Instance Method Details

#bootString

Returns such as “bootflash:///n3000-uk9-kickstart.6.0.2.U5.0.941.bin”.

Returns:

  • (String)

    such as “bootflash:///n3000-uk9-kickstart.6.0.2.U5.0.941.bin”



195
196
197
# File 'lib/util/node.rb', line 195

def boot
  @client.get_boot
end

#delete_yang(yang) ⇒ Object

Delete the specified JSON YANG config from the device.



119
120
121
# File 'lib/util/node.rb', line 119

def delete_yang(yang)
  @client.set(values: yang, mode: :delete_config)
end

#domain_nameString

Returns such as “example.com”.

Returns:

  • (String)

    such as “example.com”



169
170
171
# File 'lib/util/node.rb', line 169

def domain_name
  @client.domain_name
end

#get(**kwargs) ⇒ Object

Send a show command to the device.

Raises:



84
85
86
# File 'lib/util/node.rb', line 84

def get(**kwargs)
  @client.get(**kwargs)
end

#get_netconf(xpath) ⇒ Object

Retrieve config from the device for the specified path using netconf.



101
102
103
# File 'lib/util/node.rb', line 101

def get_netconf(xpath)
  @client.get(command: xpath)
end

#get_yang(yang_path) ⇒ Object

Retrieve JSON YANG config from the device for the specified path.



124
125
126
# File 'lib/util/node.rb', line 124

def get_yang(yang_path)
  @client.get(command: yang_path)
end

#get_yang_oper(yang_path) ⇒ Object

Retrieve JSON YANG operational data for the specified path.



129
130
131
# File 'lib/util/node.rb', line 129

def get_yang_oper(yang_path)
  @client.get(command: yang_path, mode: :get_oper)
end

#host_nameString

Returns such as “bxb-oa-n3k-7”.

Returns:

  • (String)

    such as “bxb-oa-n3k-7”



164
165
166
# File 'lib/util/node.rb', line 164

def host_name
  @client.host_name
end

#inspectObject



72
73
74
# File 'lib/util/node.rb', line 72

def inspect
  "Node: client:'#{client.inspect}'"
end

#last_reset_reasonString

Returns such as “Reset Requested by CLI command reload”.

Returns:

  • (String)

    such as “Reset Requested by CLI command reload”



184
185
186
# File 'lib/util/node.rb', line 184

def last_reset_reason
  @client.get_last_reset_reason
end

#last_reset_timeString

Returns timestamp of last reset time.

Returns:

  • (String)

    timestamp of last reset time



179
180
181
# File 'lib/util/node.rb', line 179

def last_reset_time
  @client.get_last_reset_time
end

#merge_netconf(config) ⇒ Object

Merge the specified config with the running config on the device. using netconf



90
91
92
# File 'lib/util/node.rb', line 90

def merge_netconf(config)
  @client.set(values: config, mode: :merge)
end

#merge_yang(yang) ⇒ Object

Merge the specified JSON YANG config with the running config on the device.



107
108
109
# File 'lib/util/node.rb', line 107

def merge_yang(yang)
  @client.set(values: yang, mode: :merge_config)
end

#osString

Returns such as “Cisco Nexus Operating System (NX-OS) Software”.

Returns:

  • (String)

    such as “Cisco Nexus Operating System (NX-OS) Software”



134
135
136
# File 'lib/util/node.rb', line 134

def os
  @client.os
end

#os_versionString

Returns such as “6.0(2)U5(1) [build 6.0(2)U5(0.941)]”.

Returns:

  • (String)

    such as “6.0(2)U5(1) [build 6.0(2)U5(0.941)]”



139
140
141
# File 'lib/util/node.rb', line 139

def os_version
  @client.os_version
end

#product_descriptionString

Returns such as “Nexus 3048 Chassis”.

Returns:

  • (String)

    such as “Nexus 3048 Chassis”



144
145
146
# File 'lib/util/node.rb', line 144

def product_description
  @client.product_description
end

#product_idString

Returns such as “N3K-C3048TP-1GE”.

Returns:

  • (String)

    such as “N3K-C3048TP-1GE”



149
150
151
# File 'lib/util/node.rb', line 149

def product_id
  @client.product_id
end

#product_serial_numberString

Returns such as “FOC1722R0ET”.

Returns:

  • (String)

    such as “FOC1722R0ET”



159
160
161
# File 'lib/util/node.rb', line 159

def product_serial_number
  @client.product_serial_number
end

#product_version_idString

Returns such as “V01”.

Returns:

  • (String)

    such as “V01”



154
155
156
# File 'lib/util/node.rb', line 154

def product_version_id
  @client.product_version_id
end

#replace_netconf(config) ⇒ Object

Replace the running config on the device with the specified config using netconf client.



96
97
98
# File 'lib/util/node.rb', line 96

def replace_netconf(config)
  @client.set(values: config, mode: :replace)
end

#replace_yang(yang) ⇒ Object

Replace the running config on the device with the specified JSON YANG config.



113
114
115
116
# File 'lib/util/node.rb', line 113

def replace_yang(yang)
  @client.set(values: yang,
              mode:   :replace_config)
end

#set(**kwargs) ⇒ Object

Send a config command to the device.

Raises:



78
79
80
# File 'lib/util/node.rb', line 78

def set(**kwargs)
  @client.set(**kwargs)
end

#systemString

Returns such as “bootflash:///n3000-uk9.6.0.2.U5.0.941.bin”.

Returns:

  • (String)

    such as “bootflash:///n3000-uk9.6.0.2.U5.0.941.bin”



201
202
203
# File 'lib/util/node.rb', line 201

def system
  @client.system
end

#system_cpu_utilizationFloat

Returns combined user/kernel CPU utilization.

Returns:

  • (Float)

    combined user/kernel CPU utilization



189
190
191
# File 'lib/util/node.rb', line 189

def system_cpu_utilization
  @client.system_cpu_utilization
end

#system_uptimeInteger

Returns System uptime, in seconds.

Returns:

  • (Integer)

    System uptime, in seconds



174
175
176
# File 'lib/util/node.rb', line 174

def system_uptime
  @client.system_uptime
end

#to_sObject



68
69
70
# File 'lib/util/node.rb', line 68

def to_s
  client.to_s
end