Reply
Highlighted

NetMRI Perl API how to pass the config file parameter to Policy test method

[ Edited ]
VictorO
Techie
Posts: 8
4348     0

Hi,

 

I am currently working with NetMRI Perl API and I am having a little issue with the Policy test method. He is the description of the method :

==$broker->test( \%inputs )==

--INPUTS--
_ id: Required Integer
The internal Network Automation identifier for the policy to test.

_ DeviceID: Optional Integer
The internal Network Automation identifier for the device against which to test the policy. Any specific device attributes in this device record will be overridden if they are passed as input parameters. For example, if you specify a DeviceID for a Cisco Catalyst that is running IOS 12.2, but then also include the parameter DeviceVersion with value 12.3, the policy will be tested using the 12.3 value, along with the all other values from the device. This applies to attributes and configuration file text or revision numbers.

_ ConfigRevisionID: Optional Integer
The internal Network Automation identifier for the configuration file revision (ConfigRevision) to use when testing the policy. If passed, this will override any current running configuration of the device for purposes of this test.

_ config_file: Optional String
Configuration file content to use in the policy test. If passed, this overrides both the ConfigRevisionID and the running config of any passed DeviceID.

 

Here is how I am using it in my script :

my @devicePolicies = $netmri->broker->Policy->test({
                    "id" => $policyID,
                    "config_file" => $configfolderpath."\\".$folder."\\".$configFile
                });

 

Knowing that $configfolderpath."\\".$folder."\\".$configFile refers to the absolute path of a specific config file. However when I execute my script I receive the following error :

SERVER general/unexpected-error undefined method `add_mock_config' for #<Device:0x0000000dced918> https://10.254.53.24/api/3/policies/test.json at health_check.pl line 230

 

I am running out of idea to guess how it works... Does anyone know how I am supposed to be using this method please?

 

Re: NetMRI Perl API pass config file paramter to Policy test method

Adviser
Posts: 357
4349     0
That is a bug. You should never see general/unexpected-error back from the NetMRI.

Try loading the file in as a string and sending it - right now it’s only sending the file name.

Also, try passing a DeviceID AND the config file content. That may work around the bug you ran into, since it will hit a different code path.

John

Re: NetMRI Perl API pass config file paramter to Policy test method

[ Edited ]
VictorO
Techie
Posts: 8
4349     0

Thank you John for you help.

 

However I have 2 concerns about your advice.

 

First I would like to only test the policy against the configuration file so I should not be using a DeviceID.

Moreover that would have no effect since the documentation says this :

config_file: Optional String
Configuration file content to use in the policy test. If passed, this overrides both the ConfigRevisionID and the running config of any passed DeviceID.

 

Second it seems that it still does not work since I still get the same error when trying the following :

my @devicePolicies = $netmri->broker->Policy->test({
"id" => $policyID,
"config_file" => "echo off"
});

 

I would like to add that I also tried with a DeviceID but it is still not working.

 

Am I doing something wrong?

Re: NetMRI Perl API pass config file paramter to Policy test method

[ Edited ]
VictorO
Techie
Posts: 8
4349     0

I tried to load all the configuration file in a String by doing the following and after ensuring myself that the variable does contain the configuration file content but it is still not working :

my $configFileContent;
open(my $fileHandle, '<', $configfolderpath."\\".$folder."\\".$configFile) or die "cannot open file ".$configfolderpath."\\".$folder."\\".$configFile;
{
local $/;
$configFileContent = <$fileHandle>;
}
close($fileHandle);
my @devicePolicies = $netmri->broker->Policy->test({
"id" => $policyID,
"config_file" => $configFileContent
});

Re: NetMRI Perl API pass config file paramter to Policy test method

[ Edited ]
VictorO
Techie
Posts: 8
4349     0

I ran few tests and noticed something very strange. Indeed when I pass a string containing only one or more whitespace characters (space or \n) I don't get the error but if I pass a string containing alphanumeric character I get that error even if there is only one.

Re: NetMRI Perl API pass config file paramter to Policy test method

Adviser
Posts: 357
4349     0
That’s because the spaces get stripped out, so it considers it an empty string and ignores it.

The reason I suggest the DeviceID is that internally, it will load that device, then take the passed in config (or other parameters), and override the Device’s config when running the policy. That is, if you had a policy that used device attributes as well as a config, you can use a real device as the basis for the test, but pass in the config so it will run the test as if that device had that config.

Internally, if you do NOT pass a DeviceID, it will just instantiate a device object in memory and then attach the passed config to it, versus loading one from the database. So I hoped that it might *workaround* the bug. But if it doesn’t then it’s not relevant.

This is a bug, you should open a support case. Is this happening in the UI? I thought it used the same API call.

John

Re: NetMRI Perl API pass config file paramter to Policy test method

VictorO
Techie
Posts: 8
4349     0

Thanks for your explanations it is very clear.

 

Unfortunately even with the DeviceID parameter I still get the same error message.

 

In the UI I don't have this issue, everything is working perfectly so I assume it does not use the same API call.

Re: NetMRI Perl API pass config file paramter to Policy test method

VictorO
Techie
Posts: 8
4349     0

By the way do you have any idea how I can pass device attributes with this method because the documentation doen't specifies it at all. Could you please give me an example?

Re: NetMRI Perl API pass config file paramter to Policy test method

Adviser
Posts: 357
4349     0
Would you consider just running it against a specific policy rule, rather than a complete policy?

I just looked at the policy test code, and it’s not really handling this properly. Instead, if you use the policy_rules test code, you should be able to do all of this.

In that case, any parameter starting with $ will be treated as if it were device attribute. if you wanted to change the device version, you would pass $DeviceVersion=something. Of course the name has to match a particular attribute, too.

John

Re: NetMRI Perl API pass config file paramter to Policy test method

[ Edited ]
VictorO
Techie
Posts: 8
4349     0

I finally get the function to work properly.

 

However when I try to pass device attributes as you are telling me to do it doesn't seem to work. It is like if they were just ignored since the policy result return a skip.

 

Here are the different ways I did :

my @devicePolicies = $netmri->broker->Policy->test({
                    "id" => $policyID,
                    "config_file" => $configFileContent,
                    '$DeviceVendor' => "xxxxxx",
                    '$DeviceModel' => "xxxxx"
                });

 

my @devicePolicies = $netmri->broker->Policy->test({
                    "id" => $policyID,
                    "config_file" => $configFileContent,
                    '$device_vendor' => "xxxxxx",
                    '$device_model' => "xxxxx"
                });

 

my @devicePolicies = $netmri->broker->Policy->test({
                    "id" => $policyID,
                    "config_file" => $configFileContent,
                    '$vendor' => "xxxxxx",
                    '$model' => "xxxxx"
                });

 

my @devicePolicies = $netmri->broker->Policy->test({
                    "id" => $policyID,
                    "config_file" => $configFileContent,
                    'device vendor' => "xxxxxx",
                    'device model' => "xxxxx"
                });

 

my @devicePolicies = $netmri->broker->Policy->test({
                    "id" => $policyID,
                    "config_file" => $configFileContent,
                    'Device Vendor' => "xxxxxx",
                    'Device Model' => "xxxxx"
                });

 

my @devicePolicies = $netmri->broker->Policy->test({
                    "id" => $policyID,
                    "config_file" => $configFileContent,
                    'Vendor' => "xxxxxx",
                    'Model' => "xxxxx"
                });

 

In the pop-up window, device attributes have the following name "Device Vendor" and "Device Model".

Re: NetMRI Perl API pass config file paramter to Policy test method

VictorO
Techie
Posts: 8
4349     0

After few tests it appears that the solution to use $ sign for a device attribute does work with the PolicyRule class test() function but not with the Policy class test() function...

 

Have you noticed this behaviour?

Re: NetMRI Perl API pass config file paramter to Policy test method

Adviser
Posts: 357
4349     0

Yes, that's a bug. That's what I was trying to say in my last message. You can only do this by testing an individual rule right now.

Showing results for 
Search instead for 
Do you mean 

Recommended for You