Coverage for harbor_cli/commands/api/quotas.py: 60%

33 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-09 12:09 +0100

1from __future__ import annotations 

2 

3from typing import List 

4from typing import Optional 

5 

6import typer 

7from harborapi.models.models import Quota 

8from harborapi.models.models import QuotaUpdateReq 

9 

10from ...logs import logger 

11from ...output.render import render_result 

12from ...state import state 

13from ...utils.args import parse_commalist 

14from ...utils.args import parse_key_value_args 

15from ...utils.commands import inject_resource_options 

16 

17# Create a command group 

18app = typer.Typer( 

19 name="quota", 

20 help="Quota management", 

21 no_args_is_help=True, 

22) 

23 

24 

25def get_quota(quota_id: int) -> Quota: 

26 """Fetch a quota.""" 

27 return state.run(state.client.get_quota(quota_id), "Fetching quota...") 

28 

29 

30@app.command("get", no_args_is_help=True) 

31def get_quota_cmd( 

32 ctx: typer.Context, 

33 quota_id: int = typer.Argument( 

34 ..., 

35 help="ID of quota to get.", 

36 ), 

37) -> None: 

38 """Fetch a quota.""" 

39 quota = get_quota(quota_id) 

40 render_result(quota, ctx) 

41 

42 

43@app.command("update", no_args_is_help=True) 

44def update_quota( 

45 ctx: typer.Context, 

46 quota_id: int = typer.Argument(..., help="ID of quota to update."), 

47 properties: List[str] = typer.Argument( 

48 ..., 

49 callback=parse_commalist, 

50 help=( 

51 "Quota properties to update in the format [green]'property=value'[/green]." 

52 " Multiple properties can be provided separated by spaces or commas. " 

53 "[red]NOTE:[/red] It is likely the property should always be [green]'storage'[/green] and the value an integer representing the quota size in bytes." 

54 ), 

55 metavar="PROP=VALUE, ...", 

56 ) 

57 # status omitted 

58) -> None: 

59 """Update a quota.""" 

60 props = parse_key_value_args(properties) 

61 if not props: 

62 raise typer.BadParameter("No properties provided.") 

63 

64 # quota = get_quota(quota_id) 

65 # FIXME: how to use existing quota? 

66 

67 req = QuotaUpdateReq(hard=props) 

68 state.run(state.client.update_quota(quota_id, req), f"Updating quota...") 

69 

70 # TODO: render quotas before and after update 

71 render_result(req, ctx) # is this a good idea? 

72 logger.info("Quota updated successfully.") 

73 

74 

75@app.command("list") 

76@inject_resource_options() 

77def list_quotas( 

78 ctx: typer.Context, 

79 reference: Optional[str] = typer.Option( 

80 None, "--reference", help="Reference type of quotas to list." 

81 ), 

82 reference_id: Optional[str] = typer.Option( 

83 None, "--reference-id", help="Reference ID of quotas to list." 

84 ), 

85 sort: Optional[str] = typer.Option( 

86 None, 

87 "--sort", 

88 help=( 

89 "Sort order of quotas to list. Valid values include: " 

90 "[green]'hard.resource_name'[/green], " 

91 "[green]'-hard.resource_name'[/green], " 

92 "[green]'used.resource_name'[/green], " 

93 "[green]'-used.resource_name'[/green]." 

94 ), 

95 ), 

96 page: int = ..., # type: ignore 

97 page_size: int = ..., # type: ignore 

98 limit: Optional[int] = ..., # type: ignore 

99) -> None: 

100 """List registries.""" 

101 registries = state.run( 

102 state.client.get_quotas( 

103 reference=reference, 

104 reference_id=reference_id, 

105 sort=sort, 

106 page=page, 

107 page_size=page_size, 

108 limit=limit, 

109 ), 

110 "Fetching quotas...", 

111 ) 

112 render_result(registries, ctx)