Skip to content

Commit

Permalink
Allow using regexp for model_map and group_map keys
Browse files Browse the repository at this point in the history
Use case is that the source returns string which has unrelated and variant data
but the actual model is included somewhere in this data

For example source could have space separated list of tags and one of these
tags could be model name. Eg 'procurve switch'.

Closes #3360
  • Loading branch information
ytti committed Jan 9, 2025
1 parent aa89265 commit ffc4aa0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
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 #3361 (@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
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

0 comments on commit ffc4aa0

Please sign in to comment.