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
Add netbox pillar #46559
Add netbox pillar #46559
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need some imports from __future__
here. Also, please update the logging here so that you pass the args to the log handler instead of performing string replacement manually? Our logging handler normalizes str/unicode mismatches on Python 2, so you should just do your logging like this:
log.debug('foo = %s', foo)
Doing the logging the way you have it now will result in the string replacement happening before our logging handler has a chance to normalize any str/unicode mismatches.
salt/pillar/netbox.py
Outdated
If any additional options for the proxy setup are needed they should also be | ||
configured in pillar_roots. | ||
''' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the following here:
from __future__ import absolute_import, print_function, unicode_literals
214bd2d
to
c396c51
Compare
@terminalmage Thanks. We've made the requested changes. Also squashed the commits to clean up a mess I made in merging. (Sorry about that.) |
salt/pillar/netbox.py
Outdated
if len(devices) == 1: | ||
ret['netbox'] = devices[0] | ||
elif len(devices) > 1: | ||
log.error('More than one device found for "%s"' % minion_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This log entry still needs to be changed per my earlier comments.
Fixed.
… On Mar 16, 2018, at 9:59 AM, Erik Johnson ***@***.***> wrote:
@terminalmage requested changes on this pull request.
In salt/pillar/netbox.py <#46559 (comment)>:
> + params={'name': minion_id, },
+ headers={'Authorization': 'Token ' + api_token},
+ )
+
+ # Check status code for API call
+ if device_results.status_code != requests.codes.ok:
+ log.warn('API query failed for "%s", status code: %d',
+ minion_id, device_results.status_code)
+
+ # Assign results from API call to "netbox" key
+ try:
+ devices = device_results.json()['results']
+ if len(devices) == 1:
+ ret['netbox'] = devices[0]
+ elif len(devices) > 1:
+ log.error('More than one device found for "%s"' % minion_id)
This log entry still needs to be changed per my earlier comments.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#46559 (review)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAJVWEvULD2o36F8lZ0k8_qqPNN8i1Emks5te8UjgaJpZM4SsZld>.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @ksofa2. I think we should rename this module appropriate to its scope, and document what it does exactly (the docstring doesn't currently reflect that).
salt/pillar/netbox.py
Outdated
'host': str(ipaddress.IPv4Interface( | ||
ret['netbox']['primary_ip4']['address']).ip), | ||
'driver': platform_results.json()['napalm_driver'], | ||
'proxytype': 'napalm', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be hardcoded like that. Maybe at least have it as an option or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, but given the structures we get back from NetBox, I'd like to leave it hardcoded and update the documentation so say that only napalm proxtypes are supported.
I added a code change to only produce the proxy
key if the napalm_driver is defined in the device's platform entry.
.. code-block:: yaml | ||
|
||
ext_pillar: | ||
- netbox: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would argue that this Pillar module should not be named as netbox
, as its scope is very, very specific to retrieving data from NetBox and populating the proxy
key into the Pillar. While this is very useful and nice, maybe we should emphasise the scope and rename it, say netbox_proxy
or something similar.
To clarify: a netbox
external Pillar I would expect to provide me all the NetBox data related to that device (i.e, from the dcim/devices/<device ID>/
endpoint).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's creating the proxy
key if requested, but this isn't just doing that. It also is returning the device info in the netbox
key. It's a raw dump of what it gets back from the API call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, okay, I misread that - please disregard.
salt/pillar/netbox.py
Outdated
|
||
# Pull settings from kwargs | ||
api_url = kwargs['api_url'].rstrip('/') | ||
api_token = kwargs['api_token'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the token should not be mandatory, as this Pillar module only fetches data (the token is required only when writing data into NetBox) - see https://docs.saltstack.com/en/develop/ref/modules/all/salt.modules.netbox.html as an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't have any further comments on this, just to ask you to rebase and squash (but make sure to leave aside commits such as ed6cdb7 or 2c6d44a which are unrelated to this addition). So I will defer to @terminalmage for more input.
Thanks for your contribution @ksofa2.
e87283d
to
bacfc01
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Co-authored-by: Kris Steinhoff <steinhof@umich.edu> Co-authored-by: Nicholas Grundler <grundler@umich.edu>
bacfc01
to
93a2caa
Compare
Thanks @mirceaulinic and @terminalmage! I've squashed and rebased onto develop. Please let me know if there's anything else I need to do. |
What does this PR do?
Add a pillar module that adds data to the Pillar structure from a NetBox API. Optionally creates the
proxy
structure.Tests written?
No
Commits signed with GPG?
No