Coverage for harbor_cli/commands/api/scanall.py: 60%
48 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-09 12:09 +0100
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-09 12:09 +0100
1from __future__ import annotations
3from typing import Optional
5import typer
6from harborapi.models import Schedule
7from harborapi.models import ScheduleObj
9from ...logs import logger
10from ...output.console import exit_err
11from ...output.render import render_result
12from ...state import state
13from ...utils.args import create_updated_model
14from ...utils.args import model_params_from_ctx
15from ...utils.commands import inject_help
17# Create a command group
18app = typer.Typer(
19 name="scan-all",
20 help="Scanning of all artifacts.",
21 no_args_is_help=True,
22)
23schedule_cmd = typer.Typer(
24 name="schedule",
25 help="Manage 'Scan All' schedule.",
26 no_args_is_help=True,
27)
28app.add_typer(schedule_cmd)
31# HarborAsyncClient.get_scan_all_metrics()
32@app.command("metrics")
33def get_scanall_metrics(ctx: typer.Context) -> None:
34 """Get metrics for all 'Scan All' jobs."""
35 metrics = state.run(
36 state.client.get_scan_all_metrics(), "Fetching metrics for 'Scan All' jobs..."
37 )
38 render_result(metrics, ctx)
41def get_scanall_schedule() -> Schedule:
42 return state.run(
43 state.client.get_scan_all_schedule(), "Fetching 'Scan All' schedule..."
44 )
47# HarborAsyncClient.get_scan_all_schedule()
48@schedule_cmd.command("get")
49def get_scanall_schedule_cmd(ctx: typer.Context) -> None:
50 """Get the current 'Scan All' schedule."""
51 schedule = get_scanall_schedule()
52 render_result(schedule, ctx)
55@schedule_cmd.command("create")
56@inject_help(Schedule)
57@inject_help(ScheduleObj)
58def create_scanall_schedule(
59 ctx: typer.Context,
60 type: Optional[str] = typer.Option(None),
61 cron: Optional[str] = typer.Option(None),
62) -> None:
63 params = model_params_from_ctx(ctx, ScheduleObj)
64 schedule = Schedule(schedule=ScheduleObj(**params))
65 state.run(
66 state.client.create_scan_all_schedule(schedule),
67 "Creating 'Scan All' schedule...",
68 )
69 logger.info(f"'Scan All' schedule created.")
72@schedule_cmd.command("update")
73@inject_help(Schedule)
74@inject_help(ScheduleObj)
75def update_scanall_schedule(
76 ctx: typer.Context,
77 type: Optional[str] = typer.Option(None),
78 cron: Optional[str] = typer.Option(None),
79 # TODO: 'parameters' field for Schedule
80) -> None:
81 schedule = get_scanall_schedule()
82 if not schedule.schedule:
83 exit_err("No existing 'Scan All' schedule found.")
84 schedule_obj = create_updated_model(schedule.schedule, ScheduleObj, ctx)
85 schedule.schedule = schedule_obj
86 state.run(
87 state.client.update_scan_all_schedule(schedule),
88 "Updating 'Scan All' schedule...",
89 )
90 logger.info(f"'Scan All' schedule created.")
93# HarborAsyncClient.stop_scan_all_job()
94@app.command("stop")
95def stop_scanall_job(ctx: typer.Context) -> None:
96 """Stop the currently running 'Scan All' job."""
97 state.run(state.client.stop_scan_all_job(), "Stopping 'Scan All' job...")
98 logger.info(f"'Scan All' job stopped.")