Skip to content

Commit

Permalink
Merge pull request #13 from cloudblue/support_ui_extensions
Browse files Browse the repository at this point in the history
LITE-23828, LITE-23829, LITE-23830
  • Loading branch information
Francesco Faraone authored May 30, 2022
2 parents aa29758 + 7bf70dd commit cdfca05
Show file tree
Hide file tree
Showing 15 changed files with 791 additions and 115 deletions.
7 changes: 7 additions & 0 deletions connect/eaas/core/decorators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from fastapi_utils.cbv import cbv
from fastapi_utils.inferring_router import InferringRouter

from connect.eaas.core.constants import (
EVENT_INFO_ATTR_NAME,
SCHEDULABLE_INFO_ATTR_NAME,
Expand Down Expand Up @@ -40,3 +43,7 @@ def wrapper(cls):
setattr(cls, VARIABLES_INFO_ATTR_NAME, variables)
return cls
return wrapper


router = InferringRouter()
web_app = cbv
38 changes: 28 additions & 10 deletions connect/eaas/core/extension.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import json
import os

import pkg_resources

Expand All @@ -10,12 +11,7 @@
)


class Extension:
def __init__(self, client, logger, config):
self.client = client
self.logger = logger
self.config = config

class ExtensionBase:
@classmethod
def get_descriptor(cls): # pragma: no cover
return json.load(
Expand All @@ -25,6 +21,17 @@ def get_descriptor(cls): # pragma: no cover
),
)

@classmethod
def get_variables(cls):
return getattr(cls, VARIABLES_INFO_ATTR_NAME, [])


class Extension(ExtensionBase):
def __init__(self, client, logger, config):
self.client = client
self.logger = logger
self.config = config

@classmethod
def get_events(cls):
return cls._get_methods_info(EVENT_INFO_ATTR_NAME)
Expand All @@ -33,10 +40,6 @@ def get_events(cls):
def get_schedulables(cls):
return cls._get_methods_info(SCHEDULABLE_INFO_ATTR_NAME)

@classmethod
def get_variables(cls):
return getattr(cls, VARIABLES_INFO_ATTR_NAME, [])

@classmethod
def _get_methods_info(cls, attr_name):
info = []
Expand All @@ -47,3 +50,18 @@ def _get_methods_info(cls, attr_name):
if hasattr(value, attr_name):
info.append(getattr(value, attr_name))
return info


class UIExtension(ExtensionBase):

@classmethod
def get_static_root(cls):
static_root = os.path.abspath(
os.path.join(
os.path.dirname(inspect.getfile(cls)),
'static_root',
),
)
if os.path.exists(static_root) and os.path.isdir(static_root):
return static_root
return None
Empty file.
51 changes: 51 additions & 0 deletions connect/eaas/core/inject/asynchronous.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os

from fastapi import Depends, Header

from connect.client import AsyncConnectClient


def get_installation_client(
x_connect_installation_api_key: str = Header(),
x_connect_api_gateway_url: str = Header(),
x_connect_user_agent: str = Header(),
):
return AsyncConnectClient(
x_connect_installation_api_key,
endpoint=x_connect_api_gateway_url,
use_specs=False,
default_headers={'User-Agent': x_connect_user_agent},
)


def get_extension_client(
x_connect_api_gateway_url: str = Header(),
x_connect_user_agent: str = Header(),
):
return AsyncConnectClient(
os.getenv('API_KEY'),
endpoint=x_connect_api_gateway_url,
use_specs=False,
default_headers={'User-Agent': x_connect_user_agent},
)


async def get_installation(
client: AsyncConnectClient = Depends(get_installation_client),
x_connect_extension_id: str = Header(),
x_connect_installation_id: str = Header(),
):
extension = client('devops').services[x_connect_extension_id]
return await extension.installations[x_connect_installation_id].get()


async def get_environment(
client: AsyncConnectClient = Depends(get_installation_client),
x_connect_extension_id: str = Header(),
x_connect_environment_id: str = Header(),
):
extension = client('devops').services[x_connect_extension_id]
return [
variable
async for variable in extension.environments[x_connect_environment_id].variables.all()
]
49 changes: 49 additions & 0 deletions connect/eaas/core/inject/synchronous.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os

from fastapi import Depends, Header


from connect.client import ConnectClient


def get_installation_client(
x_connect_installation_api_key: str = Header(),
x_connect_api_gateway_url: str = Header(),
x_connect_user_agent: str = Header(),
):
return ConnectClient(
x_connect_installation_api_key,
endpoint=x_connect_api_gateway_url,
use_specs=False,
default_headers={'User-Agent': x_connect_user_agent},
)


def get_extension_client(
x_connect_api_gateway_url: str = Header(),
x_connect_user_agent: str = Header(),
):
return ConnectClient(
os.getenv('API_KEY'),
endpoint=x_connect_api_gateway_url,
use_specs=False,
default_headers={'User-Agent': x_connect_user_agent},
)


def get_installation(
client: ConnectClient = Depends(get_installation_client),
x_connect_extension_id: str = Header(),
x_connect_installation_id: str = Header(),
):
extension = client('devops').services[x_connect_extension_id]
return extension.installations[x_connect_installation_id].get()


def get_environment(
client: ConnectClient = Depends(get_installation_client),
x_connect_extension_id: str = Header(),
x_connect_environment_id: str = Header(),
):
extension = client('devops').services[x_connect_extension_id]
return list(extension.environments[x_connect_environment_id].variables.all())
53 changes: 48 additions & 5 deletions connect/eaas/core/proto.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from pydantic import BaseModel
from pydantic import BaseModel, Field

from typing import Any, List, Literal, Optional, Union


class MessageType:
SETUP_REQUEST = 'setup_request'
SETUP_RESPONSE = 'setup_response'
WEB_SETUP_REQUEST = 'web_setup_request'
WEB_SETUP_RESPONSE = 'web_setup_response'
WEB_TASK = 'web_task'
TASK = 'task'
SHUTDOWN = 'shutdown'
# delete after stop using version 1
Expand All @@ -18,6 +21,8 @@ class TaskOptions(BaseModel):
task_category: str
correlation_id: Optional[str]
reply_to: Optional[str]
api_key: Optional[str]
installation_id: Optional[str]


class TaskOutput(BaseModel):
Expand All @@ -38,6 +43,7 @@ class Task(BaseModel):
options: TaskOptions
input: TaskInput
output: Optional[TaskOutput]
model_type: Literal['task'] = 'task'


class LogMeta(BaseModel):
Expand Down Expand Up @@ -69,6 +75,7 @@ class SetupResponse(BaseModel):
environment_type: Optional[str]
logging: Optional[Logging]
event_definitions: Optional[List[EventDefinition]]
model_type: Literal['setup_response'] = 'setup_response'


class Schedulable(BaseModel):
Expand All @@ -84,17 +91,50 @@ class Repository(BaseModel):

class SetupRequest(BaseModel):
# for version 1 'capabilities' renaming to 'event_subscriptions'
event_subscriptions: dict
event_subscriptions: Optional[dict]
ui_modules: Optional[dict]
variables: Optional[list]
schedulables: Optional[List[Schedulable]]
repository: Optional[Repository]
runner_version: Optional[str]
model_type: Literal['setup_request'] = 'setup_request'


class HttpResponse(BaseModel):
status: int
headers: dict
content: Optional[Any]


class HttpRequest(BaseModel):
method: str
url: str
headers: dict
content: Optional[Any]


class WebTaskOptions(BaseModel):
correlation_id: str
reply_to: str
api_key: Optional[str]
installation_id: Optional[str]


class WebTask(BaseModel):
options: WebTaskOptions
request: Optional[HttpRequest]
response: Optional[HttpResponse]
model_type: Literal['web_task'] = 'web_task'


class Message(BaseModel):
version: Literal[1, 2] = 1
message_type: str
data: Union[Task, SetupRequest, SetupResponse, None]
data: Union[
WebTask, Task,
SetupRequest, SetupResponse,
None,
] = Field(discriminator='model_type')

def serialize(self, protocol_version=2):
if protocol_version == 2:
Expand Down Expand Up @@ -162,10 +202,13 @@ def serialize(self, protocol_version=2):
@classmethod
def deserialize(cls, raw):
version = raw.get('version', 1)
if version == 2:
return cls(**raw)

message_type = raw['message_type']
raw_data = raw.get('data')

if version == 2:
return cls(**raw)

if message_type == MessageType.CAPABILITIES:
return cls(
version=version,
Expand Down
Loading

0 comments on commit cdfca05

Please sign in to comment.