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

32 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-25 08:49 +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 

9network_app = typer.Typer() 

10logger = structlog.get_logger(__name__) 

11 

12 

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

14def create_network(name: str, cidr: str): 

15 try: 

16 access_token = get_access_token() 

17 except Exception: 

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

19 exit(1) 

20 

21 context = get_context_config() 

22 res = httpx.post( 

23 f"{context['endpoint']}/api/v1/networks", 

24 content=schemas.NetworkCreate(name=name, cidr=cidr).model_dump_json(), 

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

26 ) 

27 

28 if res.status_code >= 400: 

29 print("could not create network:", res.text) 

30 exit(1) 

31 

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

33 

34 

35@network_app.command(name="list") 

36def list_networks(): 

37 try: 

38 access_token = get_access_token() 

39 except Exception: 

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

41 exit(1) 

42 

43 context = get_context_config() 

44 res = httpx.get( 

45 f"{context['endpoint']}/api/v1/networks", 

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

47 ) 

48 res.raise_for_status() 

49 print(res.json())