Skip to content
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

Added the format_dict.py module that converts normal csv dictionaries to Netbox friendly dictionaries. #99

Merged
merged 5 commits into from
Oct 12, 2023

Conversation

khalford
Copy link
Member

@khalford khalford commented Oct 6, 2023

Pynetbox friendly dictionaries

@@ -1,19 +1,21 @@
import pandas as pd
import pandas
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import pandas as pd

@staticmethod
def csv_to_python(file_path: str) -> dict:
def __init__(self):
self.pd = pandas
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...^ then we don't need this

self.enums_id = DeviceInfoID
self.enums_no_id = DeviceInfoNoID

def iterate_dicts(self) -> list:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be List and Dict, as list != List (it's one of those Python gotchas that was only fixed in later versions which we partially have access to)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the return typing should be uppercase List and Dict, does this apply to argument typing? like:
def func(values: List):
pass
or
def func(values: list):
pass

Comment on lines 49 to 69
def get_id(self, attr_string: str, netbox_value: str, site_value: str) -> id:
"""
This method uses the Pynetbox Api .get() method to retrieve the ID of a string value from Netbox.
:param attr_string: The attribute string to get.
:param netbox_value: The value to search for in Netbox.
:param site_value: The value of the site key in the dictionary
:return: Returns the value/ID
"""
attr_string = attr_string.upper()
attr_to_look_for = getattr(self.enums_id, attr_string).value # Gets Enums value
value = attrgetter(attr_to_look_for)(self.netbox) # Gets netbox attr
if attr_string == "DEVICE_TYPE":
value = value.get(slug=netbox_value).id
elif attr_string == "LOCATION":
if type(site_value) == int:
site_name = self.netbox.dcim.sites.get(site_value).name
site_slug = site_name.replace(" ", "-").lower()
value = value.get(name=netbox_value, site=site_slug)
else:
value = value.get(name=netbox_value).id
return value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Format dict should only format dicts

This should be in a different class, such as NetboxData

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -1,24 +1,25 @@
from pynetbox import api
import pynetbox
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use import pynetbox as nb

"""
self.url = url
self.token = token
self.pnb = pynetbox
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use import pynetbox as nb we would not need to use self.pnb here


def api_object(self):
"""
This method returns the Pynetbox Api object.
:return: Returns the Api object
"""
return api(self.url, self.token)
obj = self.pnb.api(self.url, self.token)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would then become nb.api(self.url, self.token)

@khalford khalford merged commit 8a5f011 into master Oct 12, 2023
2 checks passed
@khalford khalford deleted the Netbox_Data_Uploader branch October 12, 2023 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants