Skip to content

Commit 41f1ece

Browse files
committed
added a banner and api client
1 parent 515e275 commit 41f1ece

10 files changed

Lines changed: 125 additions & 26 deletions

File tree

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ Install `datamodel-codegen`:
66
uv tool install 'datamodel-code-generator[http]'
77
```
88

9-
To generate a mode:
9+
To generate a model:
1010

11-
```
11+
```bash
12+
# interfaces
1213
datamodel-codegen --url https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/interfaces.eda.nokia.com/v1alpha1/interfaces.json \
1314
--output-model-type pydantic_v2.BaseModel \
1415
--use-annotated \
1516
--enum-field-as-literal all \
1617
--output models
18+
19+
# siteinfo
20+
datamodel-codegen --url https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/siteinfo.eda.nokia.com/v1alpha1/siteinfo.json \
21+
--output-model-type pydantic_v2.BaseModel \
22+
--use-annotated \
23+
--enum-field-as-literal all \
24+
--output models
1725
```
1826

1927
See [command options](https://koxudaxi.github.io/datamodel-code-generator/#all-command-options) for more details.

main.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import asyncio
2-
31
from models.com.nokia.eda.interfaces.v1alpha1 import (
42
Interface,
53
Member,
64
Metadata,
75
SpecModel,
86
)
9-
from src import auth
7+
from src.client import Client
108

119

1210
def main():
13-
client_secret = auth.get_client_secret()
14-
access_token = auth.get_access_token(client_secret)
15-
print(f"Access Token: {access_token}")
11+
c = Client(base_url="https://devbox.panda-cobra.ts.net")
12+
c.auth()
13+
print(f"Access Token: {c.token}")
1614

1715
# iface = Interface(
1816
# apiVersion="interfaces.eda.nokia.com/v1alpha1",

models/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# generated by datamodel-codegen:
2-
# filename: https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/interfaces.eda.nokia.com/v1alpha1/interfaces.json
3-
# timestamp: 2025-01-19T10:10:02+00:00
2+
# filename: https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/siteinfo.eda.nokia.com/v1alpha1/siteinfo.json
3+
# timestamp: 2025-01-19T11:30:04+00:00
44

55
from __future__ import annotations
66

models/com/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# generated by datamodel-codegen:
2-
# filename: https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/interfaces.eda.nokia.com/v1alpha1/interfaces.json
3-
# timestamp: 2025-01-19T10:10:02+00:00
2+
# filename: https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/siteinfo.eda.nokia.com/v1alpha1/siteinfo.json
3+
# timestamp: 2025-01-19T11:30:04+00:00

models/com/nokia/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# generated by datamodel-codegen:
2-
# filename: https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/interfaces.eda.nokia.com/v1alpha1/interfaces.json
3-
# timestamp: 2025-01-19T10:10:02+00:00
2+
# filename: https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/siteinfo.eda.nokia.com/v1alpha1/siteinfo.json
3+
# timestamp: 2025-01-19T11:30:04+00:00

models/com/nokia/eda/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# generated by datamodel-codegen:
2-
# filename: https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/interfaces.eda.nokia.com/v1alpha1/interfaces.json
3-
# timestamp: 2025-01-19T10:10:02+00:00
2+
# filename: https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/siteinfo.eda.nokia.com/v1alpha1/siteinfo.json
3+
# timestamp: 2025-01-19T11:30:04+00:00
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# generated by datamodel-codegen:
2+
# filename: https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/siteinfo.eda.nokia.com/v1alpha1/siteinfo.json
3+
# timestamp: 2025-01-19T11:30:04+00:00
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# generated by datamodel-codegen:
2+
# filename: https://raw.githubusercontent.com/eda-labs/openapi/refs/heads/main/apps/siteinfo.eda.nokia.com/v1alpha1/siteinfo.json
3+
# timestamp: 2025-01-19T11:30:04+00:00
4+
5+
from __future__ import annotations
6+
7+
from typing import List, Optional
8+
9+
from pydantic import BaseModel, Field
10+
from typing_extensions import Annotated
11+
12+
from ..... import Metadata
13+
14+
15+
class Spec(BaseModel):
16+
loginBanner: Annotated[
17+
Optional[str],
18+
Field(
19+
description='This is the login banner displayed before a user has logged into the Node.',
20+
title='Login Banner',
21+
),
22+
] = None
23+
motd: Annotated[
24+
Optional[str],
25+
Field(
26+
description='This is the MOTD banner displayed after a user has logged into the Node.',
27+
title='MOTD',
28+
),
29+
] = None
30+
nodeSelector: Annotated[
31+
Optional[List[str]],
32+
Field(
33+
description='Labe selector to select nodes on which to configure the banners.',
34+
title='Node Selector',
35+
),
36+
] = None
37+
nodes: Annotated[
38+
Optional[List[str]],
39+
Field(
40+
description='List of nodes on which to configure the banners.',
41+
title='Nodes',
42+
),
43+
] = None
44+
45+
46+
class Status(BaseModel):
47+
nodes: Annotated[
48+
Optional[List[str]],
49+
Field(
50+
description='List of nodes this banner has been applied to', title='Nodes'
51+
),
52+
] = None
53+
54+
55+
class Banner(BaseModel):
56+
apiVersion: str
57+
kind: str
58+
metadata: Metadata
59+
spec: Annotated[
60+
Spec,
61+
Field(
62+
description='BannerSpec allows the configuration of login and MOTD (Message of the Day) banners on selected nodes. The banners can be applied to specific nodes or selected using label selectors.',
63+
title='Specification',
64+
),
65+
]
66+
status: Annotated[
67+
Optional[Status],
68+
Field(
69+
description='BannerStatus defines the observed state of Banner',
70+
title='Status',
71+
),
72+
] = None
73+
74+
75+
class BannerList(BaseModel):
76+
apiVersion: str
77+
items: Optional[List[Banner]] = None
78+
kind: str

src/banner.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def set_banner(banner):
2+
"""
3+
Set the banner for the SSH session.
4+
"""

src/auth.py renamed to src/client.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import httpx
22

3-
# Keycloak configuration
4-
EDA_API_URL = "https://devbox.panda-cobra.ts.net"
53
EDA_VERSION = "v24.12.1"
6-
KC_KEYCLOAK_URL = f"{EDA_API_URL}/core/httpproxy/v1/keycloak/"
4+
75
KC_REALM = "master"
86
KC_CLIENT_ID = "admin-cli"
97
KC_USERNAME = "admin"
@@ -12,9 +10,20 @@
1210
API_CLIENT_ID = "eda"
1311

1412

15-
def get_client_secret() -> str:
13+
class Client:
14+
def __init__(self, base_url: str):
15+
self.base_url = base_url
16+
self.kc_url = f"{self.base_url}/core/httpproxy/v1/keycloak/"
17+
self.token = ""
18+
19+
def auth(self) -> None:
20+
"""Authenticate and get access token"""
21+
self.token = _get_access_token(self)
22+
23+
24+
def _get_client_secret(kc_url: str) -> str:
1625
with httpx.Client(verify=False) as client:
17-
token_url = f"{KC_KEYCLOAK_URL}/realms/{KC_REALM}/protocol/openid-connect/token"
26+
token_url = f"{kc_url}/realms/{KC_REALM}/protocol/openid-connect/token"
1827
token_data = {
1928
"grant_type": "password",
2029
"client_id": KC_CLIENT_ID,
@@ -29,7 +38,7 @@ def get_client_secret() -> str:
2938
access_token = token_response.json()["access_token"]
3039

3140
# Step 2: Fetch the `eda` client ID and secret
32-
admin_api_url = f"{KC_KEYCLOAK_URL}/admin/realms/{EDA_REALM}/clients"
41+
admin_api_url = f"{kc_url}/admin/realms/{EDA_REALM}/clients"
3342
auth_headers = {
3443
"Authorization": f"Bearer {access_token}",
3544
"Content-Type": "application/json",
@@ -58,10 +67,9 @@ def get_client_secret() -> str:
5867
return client_secret
5968

6069

61-
def get_access_token(client_secret: str) -> str:
62-
token_endpoint = (
63-
f"{KC_KEYCLOAK_URL}/realms/{EDA_REALM}/protocol/openid-connect/token"
64-
)
70+
def _get_access_token(self: Client) -> str:
71+
client_secret = _get_client_secret(self.kc_url)
72+
token_endpoint = f"{self.kc_url}/realms/{EDA_REALM}/protocol/openid-connect/token"
6573

6674
token_data = {
6775
"client_id": API_CLIENT_ID,

0 commit comments

Comments
 (0)