mirror of
https://github.com/openziti/zrok.git
synced 2025-06-20 17:58:50 +02:00
make overview a class with data types instead of a function that returns JSON
This commit is contained in:
parent
9ee3db852a
commit
701f897678
@ -1,8 +1,26 @@
|
||||
from zrok.environment.root import Root
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List
|
||||
|
||||
import urllib3
|
||||
from zrok.environment.root import Root
|
||||
|
||||
from zrok_api.models.environment import Environment
|
||||
from zrok_api.models.share import Share
|
||||
|
||||
|
||||
def Overview(root: Root) -> str:
|
||||
@dataclass
|
||||
class EnvironmentAndShares:
|
||||
environment: Environment
|
||||
shares: List[Share] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Overview:
|
||||
environments: List[EnvironmentAndShares] = field(default_factory=list)
|
||||
|
||||
@classmethod
|
||||
def create(cls, root: Root) -> 'Overview':
|
||||
if not root.IsEnabled():
|
||||
raise Exception("environment is not enabled; enable with 'zrok enable' first!")
|
||||
|
||||
@ -17,4 +35,41 @@ def Overview(root: Root) -> str:
|
||||
})
|
||||
except Exception as e:
|
||||
raise Exception("unable to get account overview", e)
|
||||
return response.data.decode('utf-8')
|
||||
|
||||
json_data = json.loads(response.data.decode('utf-8'))
|
||||
overview = cls()
|
||||
|
||||
for env_data in json_data.get('environments', []):
|
||||
env_dict = env_data['environment']
|
||||
# Map the JSON keys to the Environment class parameters
|
||||
environment = Environment(
|
||||
description=env_dict.get('description'),
|
||||
host=env_dict.get('host'),
|
||||
address=env_dict.get('address'),
|
||||
z_id=env_dict.get('zId'),
|
||||
activity=env_dict.get('activity'),
|
||||
limited=env_dict.get('limited'),
|
||||
created_at=env_dict.get('createdAt'),
|
||||
updated_at=env_dict.get('updatedAt')
|
||||
)
|
||||
# Map the JSON keys to the Share class parameters
|
||||
shares = []
|
||||
for share_data in env_data.get('shares', []):
|
||||
share = Share(
|
||||
token=share_data.get('token'),
|
||||
z_id=share_data.get('zId'),
|
||||
share_mode=share_data.get('shareMode'),
|
||||
backend_mode=share_data.get('backendMode'),
|
||||
frontend_selection=share_data.get('frontendSelection'),
|
||||
frontend_endpoint=share_data.get('frontendEndpoint'),
|
||||
backend_proxy_endpoint=share_data.get('backendProxyEndpoint'),
|
||||
reserved=share_data.get('reserved'),
|
||||
activity=share_data.get('activity'),
|
||||
limited=share_data.get('limited'),
|
||||
created_at=share_data.get('createdAt'),
|
||||
updated_at=share_data.get('updatedAt')
|
||||
)
|
||||
shares.append(share)
|
||||
overview.environments.append(EnvironmentAndShares(environment=environment, shares=shares))
|
||||
|
||||
return overview
|
||||
|
Loading…
x
Reference in New Issue
Block a user