Skip to content

Commit

Permalink
ANPL-0000 Testing before performing action
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao2 committed Oct 5, 2023
1 parent 4184f45 commit 1a8ab9e
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions controlpanel/cli/management/commands/post_migration_clearup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

# Third-party
import json

from django.core.management.base import BaseCommand

# First-party/Local
Expand All @@ -18,6 +20,13 @@ class Command(BaseCommand):
EXCEPTION_APPS = ["gold-scorecard-form"]

def add_arguments(self, parser):
parser.add_argument(
"-n",
"--number",
type=int,
default=2,
help="input: The number of apps for testing",
)
parser.add_argument(
"-a", "--apply", action="store_true", help="Apply the actions"
)
Expand All @@ -29,12 +38,13 @@ def _remove_old_auth0_clients(self, app, auth0_instance):
self._log_info(f"No old client for {app.slug} - {app.repo_url}")
return

self._log_info(f"Old client information: {json.dumps(old_client_info)}")
self._log_info(f"Removing the old client for {app.slug} - {app.repo_url}")
if self.apply_action:
auth0_instance.clear_up_app(old_client_info)

def _update_db(self, app):
self._log_info(f"Removing the migration info and old clients for {app.slug} - {app.repo_url}")
self._log_info(f"Removing the migration info for {app.slug} - {app.repo_url}")
app.description = ""
if App.DEFAULT_AUTH_CATEGORY in (app.app_conf or {}).get(App.KEY_WORD_FOR_AUTH_SETTINGS, {}):
del app.app_conf[App.KEY_WORD_FOR_AUTH_SETTINGS][App.DEFAULT_AUTH_CATEGORY]
Expand Down Expand Up @@ -62,30 +72,30 @@ def _log_info(self, info):
f.write(info)
f.write("\n")

def _clear_up_resources(self, auth0_instance):
def _clear_up_resources(self, auth0_instance, testing_number: 0):
apps = App.objects.all()
counter = 1
for app in apps:
for cnt, app in enumerate(apps):
if (testing_number > 0) and (cnt > (testing_number-1)):
continue
if app.slug in self.EXCEPTION_APPS:
self._log_info(f"Ignore the application {app.slug}")
continue

try:
self._log_info(f"{counter}--Processing the application {app.slug}")
self._log_info(f"{cnt+1}--Processing the application {app.slug}")

self._remove_old_auth0_clients(app, auth0_instance)
self._update_db(app)
if "moj-analytical-services" in app.repo_url:
self._remove_application(app)
self._log_info(f"{counter}--Done with the application {app.slug}")
counter += 1
self._log_info(f"{cnt+1}--Done with the application {app.slug}")
except Exception as ex:
self._log_info(f"Failed to process {app.slug} due to error : {ex.__str__()}")

def handle(self, *args, **options):
self.stdout.write("start to scan the apps from database.")
auth0_instance = auth0.ExtendedAuth0()
self.apply_action = options.get('apply') or False
self._clear_up_resources(auth0_instance)
self._clear_up_resources(auth0_instance, options.get('number', 0))
self.stdout.write("Clean up action has completed.")

0 comments on commit 1a8ab9e

Please sign in to comment.