-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from stfc/id_netbox_query
Remake ID Netbox Query
- Loading branch information
Showing
21 changed files
with
743 additions
and
483 deletions.
There are no files selected for viewing
84 changes: 40 additions & 44 deletions
84
.github/workflows/pynetbox.yaml → .github/workflows/pynetbox_codecov.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,40 @@ | ||
name: Pynetbox Data Uploader Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
paths: | ||
- "pynetbox_data_uploader/**" | ||
- ".github/workflows/pynetbox.yaml" | ||
|
||
jobs: | ||
lint_test_codecov: | ||
runs-on: ubuntu-20.04 | ||
strategy: | ||
matrix: | ||
python-version: ["3.8", "3.x"] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
cache: "pip" | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
cd pynetbox_data_uploader | ||
pip install -r requirements.txt | ||
- name: Analyse with pylint | ||
run: cd pynetbox_data_uploader && pylint . --rcfile=.pylintrc | ||
|
||
- name: Run tests and collect coverage | ||
run: cd pynetbox_data_uploader && python3 -m pytest --cov-report xml:coverage.xml --cov | ||
|
||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
token: ${{secrets.CODECOV_TOKEN}} | ||
files: ./pynetbox_data_uploader/coverage.xml | ||
|
||
name: Pynetbox Codecov | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
paths: | ||
- "pynetbox_data_uploader/**" | ||
- ".github/workflows/pynetbox.yaml" | ||
|
||
jobs: | ||
Codecov: | ||
runs-on: ubuntu-20.04 | ||
strategy: | ||
matrix: | ||
python-version: ["3.x"] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
cache: "pip" | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
cd pynetbox_data_uploader | ||
pip install -r requirements.txt | ||
- name: Run tests and collect coverage | ||
run: cd pynetbox_data_uploader && python3 -m pytest --cov-report xml:coverage.xml --cov | ||
|
||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
token: ${{secrets.CODECOV_TOKEN}} | ||
files: ./pynetbox_data_uploader/coverage.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
name: Pynetbox Pylint | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
paths: | ||
- "pynetbox_data_uploader/**" | ||
- ".github/workflows/pynetbox.yaml" | ||
|
||
jobs: | ||
Pylint: | ||
runs-on: ubuntu-20.04 | ||
strategy: | ||
matrix: | ||
python-version: ["3.x"] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
cache: "pip" | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
cd pynetbox_data_uploader | ||
pip install -r requirements.txt | ||
- name: Analyse with pylint | ||
run: cd pynetbox_data_uploader && pylint $(git ls-files '*.py') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
name: Pynetbox Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
paths: | ||
- "pynetbox_data_uploader/**" | ||
- ".github/workflows/pynetbox.yaml" | ||
|
||
jobs: | ||
Test: | ||
runs-on: ubuntu-20.04 | ||
strategy: | ||
matrix: | ||
python-version: ["3.x"] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
cache: "pip" | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
cd pynetbox_data_uploader | ||
pip install -r requirements.txt | ||
- name: Run tests and collect coverage | ||
run: cd pynetbox_data_uploader && python3 -m pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[report] | ||
|
||
exclude_lines = | ||
if __name__ == .__main__.: |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,41 @@ | ||
from operator import attrgetter | ||
from typing import Union, Dict | ||
from lib.enums.dcim_device_id import DeviceInfoID | ||
from lib.utils.device_dataclass import Device | ||
|
||
# pylint:disable = too-few-public-methods | ||
|
||
|
||
class NetboxGetID: | ||
""" | ||
This class retrieves field value ID's from Netbox. | ||
""" | ||
|
||
# pylint: disable = R0903,C0115 | ||
class NetboxGetId: | ||
def __init__(self, api): | ||
""" | ||
This method allows the Netbox Api Object to be accessible within the class. | ||
""" | ||
self.netbox = api | ||
|
||
def get_id( | ||
self, attr_string: str, netbox_value: str, site_value: str | ||
) -> Union[int, str]: | ||
""" | ||
This method uses Pynetbox Api .get() 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 = DeviceInfoID[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 isinstance(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) | ||
list_value = list(value) | ||
list_value = [item for item in list_value if item[0] == "id"] | ||
value = list_value[0][1] | ||
else: | ||
value = value.get(name=netbox_value).id | ||
return value | ||
|
||
def get_id_from_key(self, key: str, dictionary: Dict) -> Union[str, int]: | ||
def get_id(self, device: Device, attr: str) -> Device: | ||
""" | ||
This method calls the get_id method to retrieve the Netbox id of a value. | ||
:param key: The attribute to look for. | ||
:param dictionary: The device dictionary being referenced. | ||
:return: If an ID was needed and found it returns the ID. If an ID was not needed it returns the original value. | ||
This method queries Netbox for ID's of values. | ||
:param device: The device dataclass. | ||
:param attr: The attribute to query Netbox for. | ||
:return: Returns an updated copy of the device dataclass. | ||
""" | ||
if key.upper() in [prop.name for prop in DeviceInfoID]: | ||
value = self.get_id( | ||
attr_string=key, | ||
netbox_value=dictionary[key], | ||
site_value=dictionary["site"], | ||
) | ||
return value | ||
return dictionary[key] | ||
value = getattr(device, attr) | ||
netbox_id = "" | ||
if attr in ["status", "face", "airflow", "position", "name", "serial"]: | ||
return getattr(device, attr) | ||
match attr: | ||
case "tenant": | ||
netbox_id = self.netbox.tenancy.tenants.get(name=value).id | ||
case "device_role": | ||
netbox_id = self.netbox.dcim.device_roles.get(name=value).id | ||
case "manufacturer": | ||
netbox_id = self.netbox.dcim.manufacturers.get(name=value).id | ||
case "device_type": | ||
netbox_id = self.netbox.dcim.device_types.get(slug=value).id | ||
case "site": | ||
netbox_id = self.netbox.dcim.sites.get(name=value).id | ||
case "location": | ||
if isinstance(device.site, int): | ||
site_slug = self.netbox.dcim.sites.get(id=device.site).slug | ||
else: | ||
site_slug = device.site.replace(" ", "-").lower() | ||
netbox_id = self.netbox.dcim.locations.get( | ||
name=value, site=site_slug | ||
).id | ||
case "rack": | ||
netbox_id = self.netbox.dcim.racks.get(name=value).id | ||
return netbox_id |
Oops, something went wrong.