1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/peeringdb_server/management/commands/pdb_generate_test_data.py
Matt Griswold 77282c4b66 Prep 2.29 (#1024)
* django3, py39, lgtm, linting (#715)

* IX-F Importer: ticket status change when posting re-occuring conflict to existing resolved ticket (#920)

* fix recaptcha requirement for user creation in django-admin (#715)

* IX-F Importer: fix command output buffering #967

* Drop dot1q_support field #903

* fix test (#967)

* Add name, city, country to ixfac (GET operation) #166

* additional tests fir #166

* Allow IXP to trigger ix-f importer for their exchange #779

* add docker compose for dev

* add selinux labels for mountpoints

* fixes #1013: The process to permanently remove old soft-deleted network contacts pdb_delete_pocs raises a false ProtectedAction

* fix api test

* relock poetry

* remove django_namespace_perms from installed apps

* fix user permissios ui

* remove remaining references to django namespace perms

* linting

* copy tox.ini

* comment flake8 check until we figure out why it ignores configs from tox.ini

* black format

* poetry lock

Co-authored-by: Stefan Pratter <stefan@20c.com>
2021-08-18 08:21:22 -05:00

116 lines
3.8 KiB
Python

import reversion
from django.conf import settings
from django.contrib.auth.models import Group
from django.core.management.base import BaseCommand
from django.db import transaction
from peeringdb_server import models
from peeringdb_server.mock import Mock
class Command(BaseCommand):
help = "Will create test data. This will wipe all data locally, so use with caution. This command is NOT to be run on production or beta environments."
def add_arguments(self, parser):
parser.add_argument(
"--commit", action="store_true", help="will commit the changes"
)
parser.add_argument("--limit", type=int, default=2)
def log(self, msg):
if self.commit:
self.stdout.write(msg)
else:
self.stdout.write(f"[pretend] {msg}")
def handle(self, *args, **options):
self.commit = options.get("commit")
self.limit = options.get("limit")
if settings.RELEASE_ENV in ["prod", "beta"]:
self.log("This command is only allowed to run on dev / test instances")
return
self.mock = Mock()
self.generate()
def wipe(self):
if not self.commit:
return
# we wipe all data by simply deleting all organizations
# since everything in the end is a child of an organization
# it will wipe all peeringdb data
models.Organization.objects.all().delete()
# delete all org specific user groups
Group.objects.filter(name__startswith="org.").delete()
@reversion.create_revision()
@transaction.atomic()
def generate(self):
self.entities = {k: [] for k in list(models.REFTAG_MAP.keys())}
queue = [
"org",
"net",
"ix",
"fac",
"ixpfx",
"ixfac",
"netixlan",
"netfac",
"poc",
]
self.log("Wiping current data ...")
self.wipe()
self.log(
"Making {} of each - Use the --limit option to increase or decrease (5 max)".format(
self.limit
)
)
if not self.commit:
return
for i in range(0, self.limit):
for reftag in queue:
params = {}
# create apropriate relations to previously
# create objects
if reftag in ["ixpfx", "netixlan"]:
params.update(ixlan=self.entities["ixlan"][i])
if reftag in ["poc", "netfac", "netixlan"]:
params.update(network=self.entities["net"][i])
if reftag in ["netfac", "ixfac"]:
params.update(facility=self.entities["fac"][i])
if reftag in ["ixlan", "ixfac"]:
params.update(ix=self.entities["ix"][i])
if reftag in ["ix", "net", "fac"]:
params.update(org=self.entities["org"][i])
# create object
entity = self.mock.create(reftag, **params)
self.entities[reftag].append(entity)
# for prefixes we also want to create one for the IPv6
# protocol
if reftag == "ixpfx":
params.update(protocol="IPv6")
entity = self.mock.create(reftag, **params)
self.entities[reftag].append(entity)
elif reftag == "ix":
self.entities["ixlan"].append(entity.ixlan)
self.entities["net"].append(self.mock.create("net"))
self.entities["ix"].append(self.mock.create("ix"))
self.entities["fac"].append(self.mock.create("fac"))
self.entities["org"].append(self.mock.create("org"))
for reftag, entities in list(self.entities.items()):
self.log(f"Created {len(entities)} {reftag}s")