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

VLAN creation fails if device name associated contains dots #257

Open
nstamoul opened this issue Feb 13, 2022 · 1 comment
Open

VLAN creation fails if device name associated contains dots #257

nstamoul opened this issue Feb 13, 2022 · 1 comment

Comments

@nstamoul
Copy link

nstamoul commented Feb 13, 2022

Environment

  • Python version: 3.8.10
  • Network Importer version: 3.0.3

VLAN creation fails if device name associated contains dots. The issue seems to be that the code does not correctly slugify the device name, rather does not slugify at all.

This results in an error when the POST is made to create the VLAN (see below)

{'name': 'device=cht.mpls.rtr01', 'slug': 'device__cht.mpls.rtr01'}
{'name': 'device=cht.mpls.rtr01', 'slug': 'device__cht.mpls.rtr01'}
Traceback (most recent call last):
  File "/opt/nautobot/bin/network-importer", line 8, in <module>
    sys.exit(main())
  File "/opt/nautobot/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/opt/nautobot/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/opt/nautobot/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/nautobot/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/nautobot/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/opt/nautobot/lib/python3.8/site-packages/network_importer/cli.py", line 108, in apply
    ni.sync()
  File "/opt/nautobot/lib/python3.8/site-packages/network_importer/main.py", line 142, in sync
    self.sot.sync_from(self.network, diff_class=NetworkImporterDiff)
  File "/opt/nautobot/lib/python3.8/site-packages/diffsync/__init__.py", line 525, in sync_from
    result = syncer.perform_sync()
  File "/opt/nautobot/lib/python3.8/site-packages/diffsync/helpers.py", line 326, in perform_sync
    changed |= self.sync_diff_element(element)
  File "/opt/nautobot/lib/python3.8/site-packages/diffsync/helpers.py", line 390, in sync_diff_element
    changed |= self.sync_diff_element(child, parent_model=dst_model)
  File "/opt/nautobot/lib/python3.8/site-packages/diffsync/helpers.py", line 366, in sync_diff_element
    changed, modified_model = self.sync_model(src_model=src_model, dst_model=dst_model, ids=ids, attrs=attrs)
  File "/opt/nautobot/lib/python3.8/site-packages/diffsync/helpers.py", line 419, in sync_model
    dst_model = dst_model.update(attrs=attrs)
  File "/opt/nautobot/lib/python3.8/site-packages/network_importer/adapters/nautobot_api/models.py", line 612, in update
    nb_params = self.translate_attrs_for_nautobot(attrs)
  File "/opt/nautobot/lib/python3.8/site-packages/network_importer/adapters/nautobot_api/models.py", line 522, in translate_attrs_for_nautobot
    tag_id = device.get_device_tag_id()
  File "/opt/nautobot/lib/python3.8/site-packages/network_importer/adapters/nautobot_api/models.py", line 53, in get_device_tag_id
    tag = self.diffsync.nautobot.extras.tags.create(name=f"device={self.name}", slug=f"device__{self.name}")
  File "/opt/nautobot/lib/python3.8/site-packages/pynautobot/core/endpoint.py", line 279, in create
    req = Request(base=self.url, token=self.token, http_session=self.api.http_session,).post(
  File "/opt/nautobot/lib/python3.8/site-packages/pynautobot/core/query.py", line 325, in post
    return self._make_call(verb="post", data=data)
  File "/opt/nautobot/lib/python3.8/site-packages/pynautobot/core/query.py", line 227, in _make_call
    raise RequestError(req)
pynautobot.core.query.RequestError: The request failed with code 400 Bad Request: {'slug': ['Enter a valid "slug" consisting of letters, numbers, underscores or hyphens.']}

I worked around the issue myself by installing python-slugify and doing the following modifications to the code of network_importer/adapters/nautobot_api/models.py

from slugify import slugify

tag = self.diffsync.nautobot.extras.tags.create(name=f"device={self.name}", slug=f"device__{self.name}") -> tag = self.diffsync.nautobot.extras.tags.create(name=f"device={self.name}", slug=f"device__{slugify(self.name)}")


Steps to Reproduce

  1. import a device containing dots in hostname
  2. try to import vlans

What did you expect to happen?

Expected Behavior

Correct VLAN creation in Nautobot's IPAM module

What happened instead?

Observed Behavior

Error during network-importer apply

@dd1245
Copy link

dd1245 commented Apr 4, 2022

Hi @nstamoul . I have this same issue, but am getting an error with the modified models.py file. Can you share you modified file please?

Thanks

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

No branches or pull requests

2 participants