Skip to content

Commit

Permalink
[change] Moved DeviceNode migration to create_device_nodes command #92
Browse files Browse the repository at this point in the history
Closes #92
  • Loading branch information
NoumbissiValere committed Sep 18, 2020
1 parent 83388e2 commit c7edafe
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
19 changes: 17 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ by default the data is added to the first found organization, eg::

**Note**: you can follow the `tutorial to migrate database from django-netjsongraph <https://github.com/openwisp/django-netjsongraph/blob/master/README.rst>`_.

``create_device_nodes``
^^^^^^^^^^^^^^^^^^^^^^^

This management command is used to create initial ``DeviceNode`` relationship when the
integration with `openwisp_controller <#integration-with-openwisp-controller-and-openwisp-monitoring>`_
is installed in a pre-existing system with some devices in it.

.. code-block:: shell
./manage.py create_device_nodes
Logging
-------

Expand Down Expand Up @@ -374,6 +385,10 @@ In order to use this module simply add
'rest_framework',
]
If you are enabling this integration on a pre-existing system, use the
`create_device_nodes <#create-device-nodes>`_ management command to create
the relationship between devices and nodes.

Settings
--------

Expand Down Expand Up @@ -479,8 +494,8 @@ of the url, this will enable you to point all the API urls to
your openwisp-network-topology API server's domain,
example value: ``https://mytopology.myapp.com``.

OPENWISP_NETWORK_TOPOLOGY_API_AUTH_REQUIRED
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
``OPENWISP_NETWORK_TOPOLOGY_API_AUTH_REQUIRED``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

+--------------+---------------+
| **type**: | ``boolean`` |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import swapper
from django.core.management.base import BaseCommand


class BaseCreateDeviceNodeCommand(BaseCommand):
help = 'Create initial DeviceNode objects'

def handle(self, *args, **kwargs):
Node = swapper.load_model('topology', 'Node')
DeviceNode = swapper.load_model('topology_device', 'DeviceNode')
queryset = Node.objects.select_related('topology').filter(
topology__parser='netdiff.OpenvpnParser'
)
for node in queryset.iterator():
DeviceNode.auto_create(node)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import BaseCreateDeviceNodeCommand


class Command(BaseCreateDeviceNodeCommand):
pass
11 changes: 11 additions & 0 deletions openwisp_network_topology/integrations/device/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import django
import swapper
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.db import transaction
from django.test import TransactionTestCase
from django.urls import reverse
Expand All @@ -29,6 +30,7 @@
Vpn = swapper.load_model('config', 'Template')
Organization = swapper.load_model('openwisp_users', 'Organization')
Cert = swapper.load_model('pki', 'Cert')
VpnClient = swapper.load_model('config', 'VpnClient')


class Base(
Expand Down Expand Up @@ -237,6 +239,15 @@ def test_topology_json(self):
json = topology.json(dict=True)
self.assertEqual(json['nodes'][0]['label'], device.name)

def test_create_device_nodes_command(self):
topology, _, cert = self._create_test_env(parser='netdiff.OpenvpnParser')
properties = {'common_name': cert.common_name}
n = self._create_node(topology=topology, properties=properties)
DeviceNode.objects.all().delete()
call_command('create_device_nodes')
qs = DeviceNode.objects.filter(node=n)
self.assertEqual(qs.count(), 1)


class TestMonitoringIntegration(Base, TransactionTestCase):
@mock.patch('openwisp_monitoring.monitoring.apps.MonitoringConfig.create_database')
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from openwisp_network_topology.integrations.device.management.commands import (
BaseCreateDeviceNodeCommand,
)


class Command(BaseCreateDeviceNodeCommand):
pass

0 comments on commit c7edafe

Please sign in to comment.