Automation Scripts

Reply
Highlighted

easy.broker('CustomFields') not working as expected- any thoughts?

Guru
Posts: 61
1270     0

Hello,

I am trying to extract the real switch model from 'show inventory' using the sample API code from netmri_easy.

 

When running it, i am getting the following error:

 

*** Error / Traceback (most recent call last): 
   File "", line 155, in  
   File "/usr/lib/python3.4/site-packages/infoblox_netmri/api/broker/v3_2_0/custom_fields_broker.py", line 167, in create_field 
     return self.api_request(self._get_method_fullname("create_field"), kwargs) 
   File "/usr/lib/python3.4/site-packages/infoblox_netmri/api/broker/broker.py", line 24, in api_request 
     params 
   File "/usr/lib/python3.4/site-packages/infoblox_netmri/client.py", line 227, in api_request 
     return self._make_request(url=url, method="post", data=data) 
   File "/usr/lib/python3.4/site-packages/infoblox_netmri/client.py", line 103, in _make_request 
     return self._send_request(url, method, data, extra_headers) 
   File "/usr/lib/python3.4/site-packages/infoblox_netmri/client.py", line 129, in _send_request 
     raise HTTPError(res.json(), response=res) 
 requests.exceptions.HTTPError: {'message': 'The action failed because the request was not valid.', 'fields': {'model': ['is required'], 'name': ['is required'], 'type': ['is required']}, 'error': 'general/validation-failed'} 
  ***
 swc1.4103# 
 swc1.4103#terminal no monitor 
 swc1.4103#terminal no editing 
 swc1.4103#terminal length 0 
 swc1.4103# 

 

The script i am trying is as follows:

# BEGIN-SCRIPT-BLOCK
#
# Script-Filter:
#     $vendor eq "Cisco"
#
# END-SCRIPT-BLOCK

from infoblox_netmri.easy import NetMRIEasy
import re
 
# This values will be provided by NetMRI before execution
defaults = {
   "api_url": api_url,
   "http_username": http_username,
   "http_password": http_password,
   "job_id": job_id,
   "device_id": device_id,
   "batch_id": batch_id
}

 # Create NetMRI context manager. It will close session after execution
with NetMRIEasy(**defaults) as easy:
     field_name = "RealModel"
     #custom field creation
     broker = easy.broker('CustomFields')
     broker.create_field(
        model='Device',
        name=field_name,
        type='string'
     )
     # get device
     device = easy.get_device()
     inventory = easy.send_command('show inventory')
     rmodel = re.search('(?<=PID: ).*$', inventory).group()

     if rmodel:
         # Custom field creation
         # all custom fields should start with 'custom_'
         device_broker = easy.broker('Device')
         field_name = "custom_{}".format(field_name)
         params = {
            'DeviceID': device.DeviceID,
            field_name: rmodel
         }
         result = device_broker.update(**params)
         print(result.custom_RealModel)

Anyone have any ideas where this is going wrong?

 

Thanks, Daniel

 

 

Re: easy.broker('CustomFields') not working as expected- any thoughts?

[ Edited ]
Guru
Posts: 61
1271     0

As an FYI- I get the same error running the sample as-is, with no modification of the regex search or field name.

 

Re: easy.broker('CustomFields') not working as expected- any thoughts?

Adviser
Posts: 373
1271     0

I'll look at it today Smiley Happy

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: easy.broker('CustomFields') not working as expected- any thoughts?

Adviser
Posts: 373
1271     0

I got it to work by removing:

         print(result)

I have looked into that yet Smiley Happy

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: easy.broker('CustomFields') not working as expected- any thoughts?

Guru
Posts: 61
1271     0

I commented out the print(result) line, but still got the error.

 

If i comment out the field creation portion, since the field is already in the system, i get the following error about deviceIds.

 

 

 

requests.exceptions.HTTPError: {'error': 'general/validation-failed', 'message': 'The action failed because the request was not valid.', 'fields': {'DeviceID': ['is required']}} 
  ***

Re: easy.broker('CustomFields') not working as expected- any thoughts?

Adviser
Posts: 373
1271     0

It's funny, I ran it 3x now on the same box and no issue at all, I wonder if you need to reset your sandbox or something else going on

 

# BEGIN-SCRIPT-BLOCK
#
# Script-Filter:
#     $vendor eq "Cisco"
#
# END-SCRIPT-BLOCK

from infoblox_netmri.easy import NetMRIEasy
import re
 
# This values will be provided by NetMRI before execution
defaults = {
   "api_url": api_url,
   "http_username": http_username,
   "http_password": http_password,
   "job_id": job_id,
   "device_id": device_id,
   "batch_id": batch_id
}

 # Create NetMRI context manager. It will close session after execution
with NetMRIEasy(**defaults) as easy:
     field_name = "RealModel"
     #custom field creation
     broker = easy.broker('CustomFields')
     broker.create_field(
        model='Device',
        name=field_name,
        type='string'
     )
     # get device
     device = easy.get_device()
     inventory = easy.send_command('show inventory')
     rmodel = re.search('(?<=PID: ).*$', inventory).group()

     if rmodel:
         # Custom field creation
         # all custom fields should start with 'custom_'
         device_broker = easy.broker('Device')
         field_name = "custom_{}".format(field_name)
         params = {
            'DeviceID': device.DeviceID,
            field_name: rmodel
         }
         result = device_broker.update(**params)
         print(result)
Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: easy.broker('CustomFields') not working as expected- any thoughts?

Guru
Posts: 61
1271     0

Yea, this is strange.   I reset the sandbox but am still getting the error.

I'm not seeing anything that could be missing, and if you are running the same code with no issues, something must be broken internally.

 

 

Re: easy.broker('CustomFields') not working as expected- any thoughts?

Adviser
Posts: 373
1271     0

I'm running "7.3.1.90807"

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: easy.broker('CustomFields') not working as expected- any thoughts?

Guru
Posts: 61
1271     0

I am still on 7.2.1.87960.

Showing results for 
Search instead for 
Do you mean 

Recommended for You