Skip to content

Commit 58f7d9a

Browse files
committed
save state
1 parent 7872e7b commit 58f7d9a

3 files changed

Lines changed: 56 additions & 17 deletions

File tree

main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323

2424

2525
def main():
26-
eda = EDAClient(base_url="https://devbox.panda-cobra.ts.net")
27-
logger.info(f"Access Token: {eda.token}")
26+
with EDAClient(base_url="https://devbox.panda-cobra.ts.net") as eda:
27+
my_banner = banner("This is a test banner")
28+
eda.add_to_transaction_create(my_banner)
29+
resp = eda.commit_transaction()
2830

29-
my_banner = banner("This is a test banner")
30-
# print(my_banner.model_dump())
31-
eda.add_to_transaction_create(my_banner)
31+
print(resp)
3232

3333
# iface = Interface(
3434
# apiVersion="interfaces.eda.nokia.com/v1alpha1",

src/banner.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ def banner(motd_text: str) -> Banner:
1414
namespace="clab-vlan",
1515
labels={"app": "banner"},
1616
),
17-
spec=Spec(
18-
motd=motd_text,
19-
),
17+
spec=Spec(motd=motd_text, nodeSelector=["containerlab=managed"]),
2018
)
2119

2220
return banner

src/client.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from typing import Optional
1+
import logging
2+
from typing import Any, Optional
23

34
import httpx
4-
from pydantic import BaseModel, HttpUrl
5+
from pydantic import BaseModel
6+
from rich import print
57

68
from models.core import (
79
Transaction,
@@ -11,6 +13,8 @@
1113
TransactionValue,
1214
)
1315

16+
logger = logging.getLogger(__name__)
17+
1418
EDA_VERSION = "v24.12.1"
1519

1620
KC_REALM = "master"
@@ -47,7 +51,9 @@ def add_to_transaction_create(self, resource: BaseModel) -> None:
4751
"""Add resource to transaction"""
4852
# convert the resource instance of whatever actual type to a TransactionContent
4953
content = TransactionContent(
50-
**resource.model_dump(exclude_unset=True, exclude_defaults=True)
54+
**resource.model_dump(
55+
exclude_unset=True, exclude_none=True, exclude_defaults=True
56+
)
5157
)
5258

5359
if self.transaction is None:
@@ -67,12 +73,47 @@ def add_to_transaction_create(self, resource: BaseModel) -> None:
6773
)
6874
)
6975

70-
# def run_transaction(self) -> Any:
71-
# """Run transaction"""
72-
# # convert the transaction instance to a dict
73-
# transaction_dict = self.transaction.model_dump(
74-
# exclude_unset=True, exclude_defaults=True
75-
# )
76+
def commit_transaction(self) -> Any:
77+
"""Commit transaction"""
78+
79+
# convert the transaction instance to a dict
80+
if self.transaction is None:
81+
raise ValueError("No transaction to commit")
82+
83+
self.transaction.retain = True
84+
self.transaction.resultType = "normal"
85+
86+
content = self.transaction.model_dump_json(
87+
exclude_unset=True, exclude_none=True, exclude_defaults=True
88+
)
89+
90+
response = self.post(
91+
url=self.transaction_endpoint,
92+
content=content,
93+
)
94+
if response.status_code != 200:
95+
raise ValueError(response.text)
96+
97+
tx_id: int = response.json()["id"]
98+
logger.info(f"Transaction {tx_id} committed")
99+
100+
self.get_transaction(tx_id)
101+
102+
def get_transaction(self, tx_id: int) -> Any:
103+
"""Get transaction"""
104+
105+
params = {
106+
"waitForComplete": "true",
107+
# "failOnErrors": "true"
108+
}
109+
response = self.get(
110+
url=f"{self.transaction_endpoint}/details/{tx_id}",
111+
params=params,
112+
)
113+
if response.status_code != 200:
114+
raise ValueError(response.text)
115+
116+
logger.info(f"Transaction {tx_id} details:\n{response.json()}")
76117

77118

78119
def _get_client_secret(kc_url: str) -> str:

0 commit comments

Comments
 (0)