Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using regexp for model_map and group_map keys #3362

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Added
- junos: add unit test (@systeembeheerder)
- apc_aos: support for scp (@robertcheramy)
- config: allow model_map and group_map keys to be regexp. Fixes #3360 (@ytti)

### Changed
- sonicos: accept policy message. Fixes #3339 (@Steve-M-C, @robertcheramy)
Expand Down
1 change: 1 addition & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ source:
model_map:
cisco: ios
juniper: junos
!ruby/regexp /procurve/: procurve
```

## Advanced Group Configuration
Expand Down
2 changes: 1 addition & 1 deletion lib/oxidized/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.load(cmd_opts = {})
cfgfile = cmd_opts[:config_file] || 'config'
# configuration file with full path as a class instance variable
@configfile = File.join(usrdir, cfgfile)
asetus = Asetus.new(name: 'oxidized', load: false, key_to_s: true, usrdir: usrdir, cfgfile: cfgfile)
asetus = Asetus.new(name: 'oxidized', load: false, usrdir: usrdir, cfgfile: cfgfile)
Oxidized.asetus = asetus

asetus.default.username = 'username'
Expand Down
12 changes: 10 additions & 2 deletions lib/oxidized/source/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ def initialize
@group_map = Oxidized.config.group_map || {}
end

def map_value(map_hash, original_value)
map_hash.each do |key, new_value|
mthd = key.instance_of?(Regexp) ? :match : :eql?
return new_value if original_value.send(mthd, key)
end
original_value
end

def map_model(model)
@model_map.has_key?(model) ? @model_map[model] : model
map_value(@model_map, model)
end

def map_group(group)
@group_map.has_key?(group) ? @group_map[group] : group
map_value(@group_map, group)
end

def node_var_interpolate(var)
Expand Down
32 changes: 32 additions & 0 deletions spec/source/source_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative '../spec_helper'
require 'oxidized/source/source'

describe Oxidized::Source do
describe '#map_model' do
before(:each) do
Asetus.any_instance.expects(:load)
Asetus.any_instance.expects(:create).returns(false)

# Set :home_dir to make sure the OXIDIZED_HOME environment variable is not used
Oxidized::Config.load({ home_dir: '/cfg_path/' })
yaml = %(
juniper: junos
!ruby/regexp /procurve/: procurve
)
Oxidized.config.model_map = YAML.unsafe_load(yaml)
@source = Oxidized::Source::Source.new
end

it 'returns map value for existing string key' do
_(@source.map_model('juniper')).must_equal 'junos'
end

it 'returns its argument for non-existing string key' do
_(@source.map_model('ios')).must_equal 'ios'
end

it 'returns map value for existing regexp key' do
_(@source.map_model('foo procurve1234 bar')).must_equal 'procurve'
end
end
end
Loading