Coverage for src/meshadmin/cli/commands/template.py: 100%

41 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 11:46 +0200

1import httpx 

2import structlog 

3import typer 

4from rich import print, print_json 

5 

6from meshadmin.cli.utils import get_access_token, get_context_config 

7from meshadmin.common import schemas 

8 

9template_app = typer.Typer() 

10logger = structlog.get_logger(__name__) 

11 

12 

13@template_app.command(name="create") 

14def create_template( 

15 name: str, network_name: str, is_lighthouse: bool, is_relay: bool, use_relay: bool 

16): 

17 try: 

18 access_token = get_access_token() 

19 except Exception: 

20 logger.exception("failed to get access token") 

21 exit(1) 

22 

23 context = get_context_config() 

24 res = httpx.post( 

25 f"{context['endpoint']}/api/v1/templates", 

26 content=schemas.TemplateCreate( 

27 name=name, 

28 network_name=network_name, 

29 is_lighthouse=is_lighthouse, 

30 is_relay=is_relay, 

31 use_relay=use_relay, 

32 ).model_dump_json(), 

33 headers={"Authorization": f"Bearer {access_token}"}, 

34 ) 

35 res.raise_for_status() 

36 print_json(res.content.decode("utf-8")) 

37 

38 

39@template_app.command() 

40def get_token(name: str): 

41 try: 

42 access_token = get_access_token() 

43 except Exception: 

44 logger.exception("failed to get access token") 

45 exit(1) 

46 

47 context = get_context_config() 

48 res = httpx.get( 

49 f"{context['endpoint']}/api/v1/templates/{name}/token", 

50 headers={"Authorization": f"Bearer {access_token}"}, 

51 ) 

52 res.raise_for_status() 

53 print_json(res.content.decode("utf-8")) 

54 

55 

56@template_app.command(name="delete") 

57def delete_template(name: str): 

58 try: 

59 access_token = get_access_token() 

60 except Exception: 

61 logger.exception("failed to get access token") 

62 exit(1) 

63 

64 context = get_context_config() 

65 res = httpx.delete( 

66 f"{context['endpoint']}/api/v1/templates/{name}", 

67 headers={"Authorization": f"Bearer {access_token}"}, 

68 ) 

69 res.raise_for_status() 

70 print(res.json())