Skip to content

Commit

Permalink
Merge pull request #19 from device42/D42-16383
Browse files Browse the repository at this point in the history
D42-16730 - Ansible galaxy sync issue with custom fields
Removed notes from custom fields to continue support of Device42 16.12.00.
  • Loading branch information
cscaglioned42 committed Aug 18, 2020
2 parents 7a23ee1 + 3e56a81 commit 14a8c1f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -10,11 +10,10 @@ In the `contrib` directory you will find the legacy inventory scripts. Please fa
2. `contrib\inventory\d42_ansible_dynamic_inventory.py` can be used to dynamically by ansible to get hosts from Device42 based on certain filters.

-----------------------------
## Assumptions
- This script works with Device42 12.0.0 and above
### Requirements
- ansible 2.9+
- python 3.6.x+
- Device42 16.12.00+
- requests (you can install it with pip install requests or apt-get install python-requests)
- Ansible must have an available connection to your Device42 instance in order to collect devices for inventory

Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
@@ -1,6 +1,6 @@
namespace: "device42"
name: "d42"
version: "1.1.0"
version: "1.1.1"
readme: "README.md"
authors:
- "Will Tome (@willtome)"
Expand Down
24 changes: 13 additions & 11 deletions plugins/inventory/d42.py
Expand Up @@ -123,24 +123,28 @@ def get_doql_json(self, query):
debug = self.get_option('debug')

data = {'output_type': 'json', 'query': query}
unformatted_d42_inventory = []

try:
# while there should be no timeout, ansible seems to get stuck sending requests without timeouts
response = requests.post(base_url + "/services/data/v1.0/query/", data=data,
auth=(username, password), verify=ssl_check, timeout=30)

if debug:
status_code = response.status_code
print('Response Status: ' + str(status_code))
status_code = response.status_code

# json response to json object
unformatted_d42_inventory = response.json()
if status_code == 500:
print('an error was encountered on query API call to Device42')
print('Response Status: ' + str(status_code))
unformatted_d42_inventory = {}
else:
# csv response to json object
if debug:
print('Response Status: ' + str(status_code))
unformatted_d42_inventory = response.json()

except Exception as e:
if debug:
print(e)
return []
return {}

return unformatted_d42_inventory

Expand Down Expand Up @@ -260,7 +264,6 @@ def get_d42_inventory(self):
if device_id in d42_inventory:
d42_inventory[device_id]['custom_fields'].append(
{
'notes': custom_field_record.get('custom_field_notes'),
'key': custom_field_record.get('custom_field_key'),
'value': custom_field_record.get('custom_field_value')
}
Expand Down Expand Up @@ -487,10 +490,9 @@ def get_custom_fields(self):
view_device_v1.device_pk,
view_device_v1.name,
custom_field.key as custom_field_key,
custom_field.value as custom_field_value,
custom_field.notes as custom_field_notes
custom_field.value as custom_field_value
from view_device_v1
inner join (select device_fk, type, notes, key,
inner join (select device_fk, type, key,
(CASE
WHEN view_device_custom_fields_v1.type = 'Related Field'
THEN (CASE
Expand Down

0 comments on commit 14a8c1f

Please sign in to comment.