Coverage for harbor_cli/commands/api/gc.py: 61%
52 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.models import Schedule
7from harborapi.models.models import ScheduleObj
8from harborapi.models.models import Type as ScheduleType
10from ...logs import logger
11from ...output.console import exit_err
12from ...output.render import render_result
13from ...state import state
14from ...utils.args import create_updated_model
15from ...utils.commands import inject_help
16from ...utils.commands import inject_resource_options
18# Create a command group
19app = typer.Typer(
20 name="gc",
21 help="Garbage Collection scheduling and information",
22 no_args_is_help=True,
23)
25schedule_cmd = typer.Typer(
26 name="schedule", help="Garbage collection scheduling", no_args_is_help=True
27)
28app.add_typer(schedule_cmd)
31# HarborAsyncClient.get_gc_schedule()
32@schedule_cmd.command("get")
33def get_gc_schedule(ctx: typer.Context) -> None:
34 """Get garbage collection schedule."""
35 schedule = state.run(
36 state.client.get_gc_schedule(), f"Fetching Garbage Collection schedule..."
37 )
38 render_result(schedule, ctx)
41@schedule_cmd.command("create")
42@inject_help(ScheduleObj)
43@inject_help(Schedule)
44def create_gc_schedule(
45 ctx: typer.Context,
46 type: Optional[ScheduleType] = typer.Option(
47 None,
48 "--type",
49 ),
50 cron: Optional[str] = typer.Option(
51 None,
52 "--cron",
53 ),
54) -> None:
55 """Create a new Garbage Collection schedule."""
56 schedule_obj = ScheduleObj(
57 type=type,
58 cron=cron,
59 )
60 # TODO: investigate which parameters the `parameters` field takes
61 schedule = Schedule(schedule=schedule_obj)
62 state.run(
63 state.client.create_gc_schedule(schedule),
64 "Creating Garbage Collection schedule...",
65 )
66 logger.info(f"Garbage Collection schedule created.")
69@schedule_cmd.command("update")
70@inject_help(ScheduleObj)
71@inject_help(Schedule)
72def update_gc_schedule(
73 ctx: typer.Context,
74 type: Optional[ScheduleType] = typer.Option(
75 None,
76 "--type",
77 ),
78 cron: Optional[str] = typer.Option(
79 None,
80 "--cron",
81 ),
82 # NOTE: should we add ScheduleObj.next_scheduled_time as an option?
83 # it doesn't seem like something that should be set manually?
84 # TODO: add delete untagged artifacts option
85) -> None:
86 schedule = state.run(state.client.get_gc_schedule(), "Fetching current schedule...")
87 if schedule.schedule is None:
88 exit_err(
89 "No existing schedule to update. Use `harbor gc schedule create` to create a new schedule."
90 )
91 # The actual schedule is stored in the `schedule` field of the `Schedule` model
92 new_schedule = create_updated_model(schedule.schedule, ScheduleObj, ctx)
93 schedule.schedule = new_schedule
94 # TODO: investigate which parameters the `parameters` field takes
95 # And whether or not that is something we can/want to update
96 state.run(
97 state.client.update_gc_schedule(schedule),
98 f"Updating Garbage Collection schedule...",
99 )
100 logger.info(f"Garbage Collection schedule updated.")
103# HarborAsyncClient.get_gc_jobs()
104@app.command("jobs", no_args_is_help=True)
105@inject_resource_options()
106def get_gc_jobs(
107 ctx: typer.Context,
108 query: Optional[str],
109 sort: Optional[str],
110 page: int,
111 page_size: int,
112) -> None:
113 """Get garbage collection jobs."""
114 jobs = state.run(
115 state.client.get_gc_jobs(
116 query=query,
117 sort=sort,
118 page=page,
119 page_size=page_size,
120 ),
121 f"Fetching Garbage Collection jobs...",
122 )
123 render_result(jobs, ctx)
126# HarborAsyncClient.get_gc_job()
127@app.command("job", no_args_is_help=True)
128def get_gc_job(
129 ctx: typer.Context,
130 job_id: int = typer.Argument(
131 ..., help="The ID of the Garbage Collection job to fetch."
132 ),
133) -> None:
134 """Get garbage collection job by its ID."""
135 job = state.run(
136 state.client.get_gc_job(job_id), f"Fetching Garbage Collection jobs..."
137 )
138 render_result(job, ctx)
141# HarborAsyncClient.get_gc_log()
142@app.command("log", no_args_is_help=True)
143def get_gc_log(
144 ctx: typer.Context,
145 job_id: int = typer.Argument(
146 ..., help="The ID of the Garbage Collection job to fetch logs for."
147 ),
148) -> None:
149 """Get garbage collection job by its ID."""
150 log_lines = state.run(
151 state.client.get_gc_log(job_id, as_list=True),
152 "Fetching Garbage Collection logs...",
153 )
154 render_result(log_lines, ctx)