Skip to content

Commit

Permalink
Merge pull request #118 from stfc/id_netbox_query
Browse files Browse the repository at this point in the history
Remake ID Netbox Query
  • Loading branch information
khalford authored Nov 9, 2023
2 parents 55d883c + 7fd3116 commit 765cf92
Show file tree
Hide file tree
Showing 21 changed files with 743 additions and 483 deletions.
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
34 changes: 34 additions & 0 deletions .github/workflows/pynetbox_pylint.yaml
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')
34 changes: 34 additions & 0 deletions .github/workflows/pynetbox_tests.yaml
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
4 changes: 4 additions & 0 deletions pynetbox_data_uploader/.coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[report]

exclude_lines =
if __name__ == .__main__.:
Empty file.
16 changes: 0 additions & 16 deletions pynetbox_data_uploader/lib/enums/dcim_device_id.py

This file was deleted.

14 changes: 0 additions & 14 deletions pynetbox_data_uploader/lib/enums/dcim_device_no_id.py

This file was deleted.

87 changes: 34 additions & 53 deletions pynetbox_data_uploader/lib/netbox_api/netbox_get_id.py
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
Loading

0 comments on commit 765cf92

Please sign in to comment.