Skip to content

Commit

Permalink
Merge pull request #81 from hooligan-sa/support-yml-ext
Browse files Browse the repository at this point in the history
Adds support for .yml file extensions from the datasource
  • Loading branch information
smk4664 authored Oct 18, 2023
2 parents 68b0f87 + 53453fb commit 75dc6d2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
13 changes: 5 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,16 @@ jobs:
strategy:
fail-fast: true
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
db-backend: ["postgresql"]
nautobot-version: ["stable"]
python-version: ["3.8"]
db-backend: ["postgresql", "mysql"]
nautobot-version: ["1.6.0"]
include:
- python-version: "3.10"
db-backend: "postgresql"
nautobot-version: "1.5.0"
- python-version: "3.7"
db-backend: "mysql"
nautobot-version: "1.5.0"
- python-version: "3.10"
db-backend: "mysql"
nautobot-version: "stable"
nautobot-version: "1.5.0"
runs-on: "ubuntu-20.04"
env:
INVOKE_WELCOME_WIZARD_PYTHON_VER: "${{ matrix.python-version }}"
Expand Down Expand Up @@ -193,7 +190,7 @@ jobs:
- name: "Upload binaries to release"
uses: "svenstaro/upload-release-action@v2"
with:
repo_token: "${{ secrets.NTC_GITHUB_TOKEN }}"
repo_token: "${{ secrets.GH_NAUTOBOT_BOT_TOKEN }}"
file: "dist/*"
tag: "${{ github.ref }}"
overwrite: true
Expand Down
31 changes: 23 additions & 8 deletions welcome_wizard/datasources.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,39 @@
from welcome_wizard.models.importer import ManufacturerImport, DeviceTypeImport


def refresh_git_import_wizard(repository_record, job_result, delete=False):
"""Callback for GitRepository updates - refresh Device Types managed by it."""
if "welcome_wizard.import_wizard" not in repository_record.provided_contents or delete:
# TODO Handle delete.
return
def retrieve_device_types_from_filesystem(path):
"""Retrieve Manufacturers and Device Types from the file system.
Args:
path (str): Filesystem path to the repo holding the Device Types.
Returns:
tuple: a Set of Manufacturers and a dictionary of Device Types.
"""
manufacturers = set()
device_types = {}

# We have decided that a Git repository can provide YAML files in a
# We have decided that a Git repository can provide both YML and YAML files in a
# /animals/ directory at the repository root.
device_type_path = os.path.join(repository_record.filesystem_path, "device-types")
for filename in Path(device_type_path).rglob("*.yaml"):

device_type_path = os.path.join(path, "device-types")
files = (filename for filename in Path(device_type_path).rglob("*") if filename.suffix in [".yml", ".yaml"])
for filename in files:
with open(filename, encoding="utf8") as file:
data = yaml.safe_load(file)

manufacturers.add(data["manufacturer"])
device_types[filename.name] = data
return (manufacturers, device_types)


def refresh_git_import_wizard(repository_record, job_result, delete=False):
"""Callback for GitRepository updates - refresh Device Types managed by it."""
if "welcome_wizard.import_wizard" not in repository_record.provided_contents or delete:
# TODO Handle delete.
return

manufacturers, device_types = retrieve_device_types_from_filesystem(repository_record.filesystem_path)

for manufacturer in manufacturers:
# Create or update an ManufacturerImport record based on the provided data
Expand Down
8 changes: 8 additions & 0 deletions welcome_wizard/tests/test_datasources.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def populate_repo(path, _url):
{"manufacturer": "Cisco", "model": "Fake Model"},
file,
)
with open(os.path.join(path, "device-types", "Cisco", "fake2.yml"), "w", encoding="utf8") as file:
yaml.dump(
{"manufacturer": "Cisco", "model": "Fake Model 2"},
file,
)
return mock.DEFAULT

mock_git_repo.side_effect = populate_repo
Expand All @@ -118,6 +123,9 @@ def populate_repo(path, _url):
self.assertIsNotNone(device_type)
self.assertEqual(device_type.name, "Fake Model")
self.assertEqual(device_type.manufacturer, manufacturer_import)
device_type2 = DeviceTypeImport.objects.get(filename="fake2.yml")
self.assertIsNotNone(device_type2)
self.assertEqual(device_type2.name, "Fake Model 2")

# Delete the GitRepository (this is a noop)
self.repo.delete()

0 comments on commit 75dc6d2

Please sign in to comment.