From 4bcb58c8fcea5e8a4428f9af0431928bd86302b0 Mon Sep 17 00:00:00 2001 From: meganerd Date: Mon, 25 Sep 2023 03:02:11 -0400 Subject: [PATCH] Update init_job for branch `next-2.0` dispatch.user feature for user permission mapping Add get_jobs class method which returns table of jobs, which can be filtered Update queries to use restrist filter for user --- changes/224.added | 1 + nautobot_chatops/workers/nautobot.py | 45 ++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/changes/224.added b/changes/224.added index 2548ff0f..edf1f6f3 100644 --- a/changes/224.added +++ b/changes/224.added @@ -1 +1,2 @@ Add init_job Nautobot subcommand, which starts a Nautobot job by job name. +Add get_jobs Nautobot subcommand, which gets all Nautobot jobs or a filtered list of jobs. diff --git a/nautobot_chatops/workers/nautobot.py b/nautobot_chatops/workers/nautobot.py index f9fc8b1d..595d9a22 100644 --- a/nautobot_chatops/workers/nautobot.py +++ b/nautobot_chatops/workers/nautobot.py @@ -1023,18 +1023,53 @@ def get_circuit_providers(dispatcher, *args): return CommandStatusChoices.STATUS_SUCCEEDED +@subcommand_of("nautobot") +def get_jobs(dispatcher, job_filters: str = ""): # We can use a Literal["enabled", "installed", "runnable"] here instead + """Get jobs from Nautobot. + + Args: + job_filters (str): + """ + # Check for filters in user supplied input + filters = ["enabled", "installed", "runnable"] + if any([key in job_filters for key in filters]): + filter_args = {key: job_filters[key] for key in filters if key in job_filters} + jobs = Job.objects.restrict(dispatch.user, "view").filter(**filter_args) # enabled=True, installed=True, runnable=True + else: + jobs = Job.objects.restrict(dispatch.user, "view").all() + + header = ["Name", "ID"] + rows = [ + ( + str(job.name), + str(job.id), + ) + for job in jobs + ] + + dispatcher.send_large_table(header, rows) + + return CommandStatusChoices.STATUS_SUCCEEDED + + @subcommand_of("nautobot") def init_job(dispatcher, job_name): """Initiate a job in Nautobot by job name.""" - # Replace this with user mapping - job_username = "meganerd" - # Get instance of the user who will run the job user = get_user_model() - user_instance = user.objects.get(username=job_username) + try: + user_instance = user.objects.get(username=dispatch.user) + except user.DoesNotExist: # Unsure if we need to check this case? + dispatcher.send_error(f"User {dispatch.user} not found") + return (CommandStatusChoices.STATUS_FAILED, f'User "{dispatch.user}" not found') # Get the job model instance using job name - job_model = Job.objects.get(name=job_name) + try: + job_model = Job.objects.restrict(dispatch.user, "view").get(name=job_name) + except Job.DoesNotExist: + dispatcher.send_error(f"Job {job_name} not found") + return (CommandStatusChoices.STATUS_FAILED, f'Job "{job_name}" not found') + job_class_path = job_model.class_path # Create an instance of job result