"""
Setup DB with example data for tests
"""

from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User, Group
from django.core.management.base import BaseCommand

from api import models


class Command(BaseCommand):
    help = 'Setup DB with example data for tests'

    def handle(self, *args, **options):
        print('----    Creating Users    ----')
        User.objects.get_or_create(username='thomastest', password=make_password('imatestin'))
        thomas = User.objects.get(username='thomastest')

        User.objects.get_or_create(username='norbert', password=make_password('netzwerk'))
        norbert = User.objects.get(username='norbert')

        User.objects.get_or_create(username='stefan', password=make_password('helldesk'))
        stefan = User.objects.get(username='stefan')

        superuser = Group.objects.get(name='superuser')
        superuser.user_set.add(thomas)

        netadmin = Group.objects.get(name='netadmin')
        netadmin.user_set.add(norbert)

        support = Group.objects.get(name='support')
        support.user_set.add(stefan)

        print('----    Creating Inventory    ----')
        models.Inventory.objects.create(name='Example', hosts_file='web_nornir/nornir_config/example_config/hosts.yaml',
                                        groups_file='web_nornir/nornir_config/example_config/groups.yaml', type=1)
        models.Inventory.objects.create(name='INS Lab', hosts_file='web_nornir/nornir_config/inslab_config/hosts.yaml',
                                        groups_file='web_nornir/nornir_config/inslab_config/groups.yaml', type=1)

        print('----    Creating Job Templates    ----')
        models.JobTemplate.objects.create(name='hello_world', description='This prints a hello world',
                                          file_name='hello_world.py', created_by_id=1)
        models.JobTemplate.objects.create(name='Get CDP Neighbors', description='Lists all CDP neighbors',
                                          file_name='get_cdp_neighbors.py', created_by_id=1)
        models.JobTemplate.objects.create(name='Get Interfaces',
                                          description='Gets brief information about all interfaces, sh ip int br',
                                          file_name='get_interfaces.py', created_by_id=1)
        models.JobTemplate.objects.create(name='Ping Device',
                                          description='Pings a chosen network device and reports if reachable',
                                          file_name='ping.py', variables=['target'], created_by_id=1)
        models.JobTemplate.objects.create(name='Get Configuration', description='Gets all configuration from device',
                                          file_name='get_configuration.py', created_by_id=1)

        print('----    Creating Tasks    ----')
        models.Task.objects.create(name='Get Hello World', created_by_id=1, template_id=1, inventory_id=1)
        models.Task.objects.create(name='Get CDP neighbors of INS lab', created_by_id=2, template_id=2, inventory_id=2)
        models.Task.objects.create(name='Get interfaces of INS lab', created_by_id=2, template_id=3, inventory_id=2)

        print('----    ALL DONE!!    ----')

[(18, 'django.contrib.auth.models.User.objects.get', 'User.objects.get', ([], {'username': '"""thomastest"""'}), False, 'from django.contrib.auth.models import User, Group\n'), (21, 'django.contrib.auth.models.User.objects.get', 'User.objects.get', ([], {'username': '"""norbert"""'}), False, 'from django.contrib.auth.models import User, Group\n'), (24, 'django.contrib.auth.models.User.objects.get', 'User.objects.get', ([], {'username': '"""stefan"""'}), False, 'from django.contrib.auth.models import User, Group\n'), (26, 'django.contrib.auth.models.Group.objects.get', 'Group.objects.get', ([], {'name': '"""superuser"""'}), False, 'from django.contrib.auth.models import User, Group\n'), (29, 'django.contrib.auth.models.Group.objects.get', 'Group.objects.get', ([], {'name': '"""netadmin"""'}), False, 'from django.contrib.auth.models import User, Group\n'), (32, 'django.contrib.auth.models.Group.objects.get', 'Group.objects.get', ([], {'name': '"""support"""'}), False, 'from django.contrib.auth.models import User, Group\n'), (36, 'api.models.Inventory.objects.create', 'models.Inventory.objects.create', ([], {'name': '"""Example"""', 'hosts_file': '"""web_nornir/nornir_config/example_config/hosts.yaml"""', 'groups_file': '"""web_nornir/nornir_config/example_config/groups.yaml"""', 'type': '(1)'}), False, 'from api import models\n'), (38, 'api.models.Inventory.objects.create', 'models.Inventory.objects.create', ([], {'name': '"""INS Lab"""', 'hosts_file': '"""web_nornir/nornir_config/inslab_config/hosts.yaml"""', 'groups_file': '"""web_nornir/nornir_config/inslab_config/groups.yaml"""', 'type': '(1)'}), False, 'from api import models\n'), (42, 'api.models.JobTemplate.objects.create', 'models.JobTemplate.objects.create', ([], {'name': '"""hello_world"""', 'description': '"""This prints a hello world"""', 'file_name': '"""hello_world.py"""', 'created_by_id': '(1)'}), False, 'from api import models\n'), (44, 'api.models.JobTemplate.objects.create', 'models.JobTemplate.objects.create', ([], {'name': '"""Get CDP Neighbors"""', 'description': '"""Lists all CDP neighbors"""', 'file_name': '"""get_cdp_neighbors.py"""', 'created_by_id': '(1)'}), False, 'from api import models\n'), (46, 'api.models.JobTemplate.objects.create', 'models.JobTemplate.objects.create', ([], {'name': '"""Get Interfaces"""', 'description': '"""Gets brief information about all interfaces, sh ip int br"""', 'file_name': '"""get_interfaces.py"""', 'created_by_id': '(1)'}), False, 'from api import models\n'), (49, 'api.models.JobTemplate.objects.create', 'models.JobTemplate.objects.create', ([], {'name': '"""Ping Device"""', 'description': '"""Pings a chosen network device and reports if reachable"""', 'file_name': '"""ping.py"""', 'variables': "['target']", 'created_by_id': '(1)'}), False, 'from api import models\n'), (52, 'api.models.JobTemplate.objects.create', 'models.JobTemplate.objects.create', ([], {'name': '"""Get Configuration"""', 'description': '"""Gets all configuration from device"""', 'file_name': '"""get_configuration.py"""', 'created_by_id': '(1)'}), False, 'from api import models\n'), (56, 'api.models.Task.objects.create', 'models.Task.objects.create', ([], {'name': '"""Get Hello World"""', 'created_by_id': '(1)', 'template_id': '(1)', 'inventory_id': '(1)'}), False, 'from api import models\n'), (57, 'api.models.Task.objects.create', 'models.Task.objects.create', ([], {'name': '"""Get CDP neighbors of INS lab"""', 'created_by_id': '(2)', 'template_id': '(2)', 'inventory_id': '(2)'}), False, 'from api import models\n'), (58, 'api.models.Task.objects.create', 'models.Task.objects.create', ([], {'name': '"""Get interfaces of INS lab"""', 'created_by_id': '(2)', 'template_id': '(3)', 'inventory_id': '(2)'}), False, 'from api import models\n'), (17, 'django.contrib.auth.hashers.make_password', 'make_password', (['"""imatestin"""'], {}), False, 'from django.contrib.auth.hashers import make_password\n'), (20, 'django.contrib.auth.hashers.make_password', 'make_password', (['"""netzwerk"""'], {}), False, 'from django.contrib.auth.hashers import make_password\n'), (23, 'django.contrib.auth.hashers.make_password', 'make_password', (['"""helldesk"""'], {}), False, 'from django.contrib.auth.hashers import make_password\n')]