ytti / oxidized

Oxidized is a network device configuration backup tool. It's a RANCID replacement!
Apache License 2.0
2.79k stars 922 forks source link

Extend model, crash oxidized #3118

Closed marmack95 closed 6 months ago

marmack95 commented 6 months ago

Hello, i'm trying to extend a model, but it does not work.

require 'oxidized/model/comware.rb'

class CustomHP
  using Refinements

  cmd 'save force' do |cfg|
    comment cfg
  end

end

That's the result:

2024-04-10 14:53:14 UTC
undefined method `cmd' for CustomHP:Class [NoMethodError]
--------------------------------------------------
/home/oxidized/.config/oxidized/model/edl_hp.rb:12:in `<class:CustomHP>'
/home/oxidized/.config/oxidized/model/edl_hp.rb:3:in `<top (required)>'
<internal:/usr/lib/ruby/vendor_ruby/rubygems/core_ext/kernel_require.rb>:85:in `require'
<internal:/usr/lib/ruby/vendor_ruby/rubygems/core_ext/kernel_require.rb>:85:in `require'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/manager.rb:9:in `load'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/manager.rb:60:in `loader'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/manager.rb:48:in `add_model'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/node.rb:179:in `resolve_model'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/node.rb:23:in `initialize'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/nodes.rb:22:in `new'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/nodes.rb:22:in `block (2 levels) in load'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/nodes.rb:17:in `each'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/nodes.rb:17:in `block in load'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/nodes.rb:133:in `synchronize'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/nodes.rb:133:in `with_lock'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/nodes.rb:11:in `load'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/nodes.rb:128:in `initialize'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/core.rb:14:in `new'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/core.rb:14:in `initialize'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/core.rb:4:in `new'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/core.rb:4:in `new'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/lib/oxidized/cli.rb:13:in `run'
/var/lib/gems/3.0.0/gems/oxidized-0.29.1/bin/oxidized:5:in `<top (required)>'
/usr/local/bin/oxidized:25:in `load'
/usr/local/bin/oxidized:25:in `<main>'

If i compare with documentation: https://github.com/ytti/oxidized/blob/master/docs/Creating-Models.md or this issue: https://github.com/ytti/oxidized/issues/3017 All seems ok, but it does not work.

I use docker version (tested with the last version and version 0.29.1 of april 2023)

marmack95 commented 6 months ago

The documentation is not correct. I found the solution by adding the name of the require class as someone do it here: https://github.com/ytti/oxidized/issues/2579.

require 'oxidized/model/comware.rb'

class CustomHP < Comware
...

It's works now.

ytti commented 6 months ago

Document is extending an existing class, you are creating a new class, subclassing existing class. These are two different things. This is technically not oxidized question at all, but rather ruby question.

Usually all model classes are subclasses of Oxidized::Model, this is why custom methods like cmd work. If we don't subclass existing class, and we define entirely new class, of course there will be no Oxidized specific methods available.