1 import httpx

2 import structlog

3 import typer

4 from rich import print, print_json

5

6 from meshadmin.cli.utils import get_access_token, get_context_config

7 from meshadmin.common import schemas

8

9 network_app = typer.Typer()

10 logger = structlog.get_logger(__name__)

11

12

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

14 def 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")

36 def 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())