From 2435cbcf124cf9d61bc7bd24650e3ee0d17da1a9 Mon Sep 17 00:00:00 2001 From: Gary Snider <75227981+gsnider2195@users.noreply.github.com> Date: Thu, 12 Sep 2024 15:37:38 -0700 Subject: [PATCH] rename management command --- docs/dev/dev_environment.md | 13 ++-- ...test_data.py => generate_dlm_test_data.py} | 60 ++++++++----------- 2 files changed, 33 insertions(+), 40 deletions(-) rename nautobot_device_lifecycle_mgmt/management/commands/{generate_app_test_data.py => generate_dlm_test_data.py} (62%) diff --git a/docs/dev/dev_environment.md b/docs/dev/dev_environment.md index b5deca46..d0ed1f10 100644 --- a/docs/dev/dev_environment.md +++ b/docs/dev/dev_environment.md @@ -485,14 +485,15 @@ This command can only guess the schema, so it's up to the developer to manually ### Test Data Generation -To quickly generate test data for developing against this app, you can use the following command: - -!!! danger - The `--flush` flag will completely empty your database and replace it with test data. This command should never be run in a production environment. +To quickly generate test data for developing against this app, you can use the following commands: ```bash -nautobot-server generate_app_test_data --flush +nautobot-server generate_test_data --flush +nautobot-server generate_dlm_test_data nautobot-server createsuperuser ``` -This uses the [`generate_test_data`](https://docs.nautobot.com/projects/core/en/stable/user-guide/administration/tools/nautobot-server/#generate_test_data) management command from Nautobot core to generate the Statuses, Platforms, Device Types, Devices, etc. Nautobot version 2.2.0 is the minimum version required for devices to be generated. +!!! danger + The `--flush` flag will completely empty your database and replace it with test data. This command should never be run in a production environment. + +This uses the [`generate_test_data`](https://docs.nautobot.com/projects/core/en/stable/user-guide/administration/tools/nautobot-server/#generate_test_data) management command from Nautobot core to generate the Statuses, Platforms, Device Types, Devices, etc. Nautobot version 2.2.0 is the minimum version required for devices to be generated. If using an older version of Nautobot, you'll need to create devices manually after running `nautobot-server generate_test_data`. diff --git a/nautobot_device_lifecycle_mgmt/management/commands/generate_app_test_data.py b/nautobot_device_lifecycle_mgmt/management/commands/generate_dlm_test_data.py similarity index 62% rename from nautobot_device_lifecycle_mgmt/management/commands/generate_app_test_data.py rename to nautobot_device_lifecycle_mgmt/management/commands/generate_dlm_test_data.py index 8085e9ac..5a3c58c1 100644 --- a/nautobot_device_lifecycle_mgmt/management/commands/generate_app_test_data.py +++ b/nautobot_device_lifecycle_mgmt/management/commands/generate_dlm_test_data.py @@ -30,90 +30,85 @@ class Command(BaseCommand): help = __doc__ def add_arguments(self, parser): # noqa: D102 - parser.add_argument( - "--flush", - action="store_true", - help="Flush any existing data from the database before generating new data.", - ) parser.add_argument( "--database", default=DEFAULT_DB_ALIAS, help='The database to generate the test data in. Defaults to the "default" database.', ) - def _generate_static_data(self): - devices = get_random_instances(Device, minimum=2, maximum=4) - device_types = get_random_instances(DeviceType, minimum=2, maximum=4) - inventory_items = get_random_instances(InventoryItem, minimum=2, maximum=4) - platforms = get_random_instances(Platform, minimum=2, maximum=4) + def _generate_static_data(self, db): + devices = get_random_instances(Device.objects.using(db), minimum=2, maximum=4) + device_types = get_random_instances(DeviceType.objects.using(db), minimum=2, maximum=4) + inventory_items = get_random_instances(InventoryItem.objects.using(db), minimum=2, maximum=4) + platforms = get_random_instances(Platform.objects.using(db), minimum=2, maximum=4) # create HardwareLCM for device_type in device_types: - HardwareLCM.objects.create(device_type=device_type, end_of_sale="2022-03-14") + HardwareLCM.objects.using(db).create(device_type=device_type, end_of_sale="2022-03-14") for inventory_item in inventory_items: - HardwareLCM.objects.create(inventory_item=inventory_item, end_of_support="2020-05-04") + HardwareLCM.objects.using(db).create(inventory_item=inventory_item, end_of_support="2020-05-04") # create SoftwareLCM for platform in platforms: - SoftwareLCM.objects.create( + SoftwareLCM.objects.using(db).create( device_platform=platform, version=f"Test SoftwareLCM for {platform.name}", ) # create SoftwareImageLCM - for software in SoftwareLCM.objects.all(): - SoftwareImageLCM.objects.create( + for software in SoftwareLCM.objects.using(db).all(): + SoftwareImageLCM.objects.using(db).create( software=software, image_file_name=f"{software.device_platform.name}_vxyz.bin", ) # create ValidatedSoftwareLCM - for software in SoftwareLCM.objects.all(): - ValidatedSoftwareLCM.objects.create( + for software in SoftwareLCM.objects.using(db).all(): + ValidatedSoftwareLCM.objects.using(db).create( software=software, start="2020-01-01", end="2020-12-31", ) # create DeviceSoftwareValidationResult - software_choices = list(SoftwareLCM.objects.all()) + software_choices = list(SoftwareLCM.objects.using(db).all()) for device in devices: - DeviceSoftwareValidationResult.objects.create( + DeviceSoftwareValidationResult.objects.using(db).create( device=device, software=random.choice(software_choices), # noqa: S311 ) # create InventoryItemSoftwareValidationResult for inventory_item in inventory_items: - InventoryItemSoftwareValidationResult.objects.create( + InventoryItemSoftwareValidationResult.objects.using(db).create( inventory_item=inventory_item, software=random.choice(software_choices), # noqa: S311 ) # create ProviderLCM for i in range(1, 9): - ProviderLCM.objects.create( + ProviderLCM.objects.using(db).create( name=f"Test Provider {i}", description=f"Description for Provider {i}", ) # create ContractLCM - for provider in ProviderLCM.objects.all(): - ContractLCM.objects.create( + for provider in ProviderLCM.objects.using(db).all(): + ContractLCM.objects.using(db).create( provider=provider, name=f"Test Contract for {provider.name}", ) # create ContactLCM - for contract in ContractLCM.objects.all(): - ContactLCM.objects.create( + for contract in ContractLCM.objects.using(db).all(): + ContactLCM.objects.using(db).create( contract=contract, name=f"Test Contact for {contract.name}", ) # create CVELCM for i in range(1, 5): - cve = CVELCM.objects.create( + cve = CVELCM.objects.using(db).create( name=f"Test CVELCM {i}", published_date="2020-01-01", link="http://cve.example.org/{i}/details/", @@ -122,16 +117,13 @@ def _generate_static_data(self): cve.affected_softwares.set(get_random_instances(SoftwareLCM, minimum=1)) # create VulnerabilityLCM - VulnerabilityLCM.objects.create(cve=CVELCM.objects.get(name="Test CVELCM 1")) - VulnerabilityLCM.objects.create(software=SoftwareLCM.objects.first()) - VulnerabilityLCM.objects.create(device=Device.objects.first()) - VulnerabilityLCM.objects.create(inventory_item=InventoryItem.objects.first()) + VulnerabilityLCM.objects.using(db).create(cve=CVELCM.objects.using(db).get(name="Test CVELCM 1")) + VulnerabilityLCM.objects.using(db).create(software=SoftwareLCM.objects.using(db).first()) + VulnerabilityLCM.objects.using(db).create(device=Device.objects.using(db).first()) + VulnerabilityLCM.objects.using(db).create(inventory_item=InventoryItem.objects.using(db).first()) def handle(self, *args, **options): """Entry point to the management command.""" - # Call nautobot core's generate_test_data command to generate data for core models - call_command("generate_test_data", flush=options["flush"]) - - self._generate_static_data() + self._generate_static_data(db=options["database"]) self.stdout.write(self.style.SUCCESS(f"Database {options['database']} populated with app data successfully!"))