-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added clear and apply commands to ocean CLI * bumped version of ocean framework * Added group of defaults * Added migration ids * Reverted change log * Added an option to wait for a migration to finish * Fixed mypy + ruff errors * Fixed CR comments * Fixed CR comments * Added towncrier changelog * Fixed Pager Duty integration to use client inside the functions * Fixed changelog fragment * Fixed changelog fragment * Fixed changelog fragment grammer
- Loading branch information
1 parent
e6a00c0
commit bea8a5b
Showing
20 changed files
with
478 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added the ability to create and clean the defaults of an integration using the following CLI commands: `ocean defaults dock` and `ocean defaults clean` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sys | ||
from inspect import getmembers, isclass | ||
from types import ModuleType | ||
from typing import Type | ||
|
||
from pydantic import BaseModel | ||
|
||
from port_ocean.core.integrations.base import BaseIntegration | ||
from port_ocean.ocean import Ocean | ||
from port_ocean.utils import load_module | ||
|
||
|
||
def _get_base_integration_class_from_module( | ||
module: ModuleType, | ||
) -> Type[BaseIntegration]: | ||
for name, obj in getmembers(module): | ||
if ( | ||
isclass(obj) | ||
and type(obj) == type | ||
and issubclass(obj, BaseIntegration) | ||
and obj != BaseIntegration | ||
): | ||
return obj | ||
|
||
raise Exception(f"Failed to load integration from module: {module.__name__}") | ||
|
||
|
||
def create_default_app( | ||
path: str | None = None, config_factory: Type[BaseModel] | None = None | ||
) -> Ocean: | ||
sys.path.append(".") | ||
try: | ||
integration_path = f"{path}/integration.py" if path else "integration.py" | ||
module = load_module(integration_path) | ||
integration_class = _get_base_integration_class_from_module(module) | ||
except Exception: | ||
integration_class = None | ||
|
||
return Ocean(integration_class=integration_class, config_factory=config_factory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .dock import dock | ||
from .clean import clean | ||
|
||
__all__ = [ | ||
"dock", | ||
"clean", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- coding: utf-8 -*- | ||
import click | ||
|
||
from inspect import getmembers | ||
|
||
from port_ocean.utils import load_module | ||
from .group import defaults | ||
from port_ocean.cli.commands.main import print_logo, console | ||
from port_ocean.ocean import Ocean | ||
from port_ocean.core.defaults import clean_defaults | ||
from port_ocean.bootstrap import create_default_app | ||
|
||
|
||
@defaults.command() | ||
@click.argument("path", default=".", type=click.Path(exists=True)) | ||
@click.option( | ||
"-f", | ||
"--force", | ||
"force", | ||
is_flag=True, | ||
help="Delete all the entities of the Blueprint as well as the blueprint itself.", | ||
) | ||
@click.option( | ||
"-w", | ||
"--wait", | ||
"wait", | ||
is_flag=True, | ||
help="Wait for the migration to finish. when force is set to true.", | ||
) | ||
def clean(path: str, force: bool, wait: bool) -> None: | ||
""" | ||
Clean defaults of the integration from the .port/resources PATH. | ||
PATH: Path to the integration. If not provided, the current directory will be used. | ||
""" | ||
print_logo() | ||
|
||
console.print("Cleaning blueprints and configurations! ⚓️") | ||
|
||
if force: | ||
console.print( | ||
"Deleting entities forcefully I sure hope you know what you are doing 🚨 🚨 🚨 ", | ||
) | ||
default_app = create_default_app(path) | ||
|
||
main_path = f"{path}/main.py" if path else "main.py" | ||
|
||
app_module = load_module(main_path) | ||
app: Ocean = {name: item for name, item in getmembers(app_module)}.get( | ||
"app", | ||
default_app, | ||
) | ||
|
||
clean_defaults(app.integration.AppConfigHandlerClass.CONFIG_CLASS, force, wait) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
import click | ||
|
||
from inspect import getmembers | ||
|
||
from port_ocean.utils import load_module | ||
from .group import defaults | ||
from port_ocean.cli.commands.main import print_logo, console | ||
from port_ocean.ocean import Ocean | ||
from port_ocean.core.defaults.initialize import initialize_defaults | ||
from port_ocean.bootstrap import create_default_app | ||
|
||
|
||
@defaults.command() | ||
@click.argument("path", default=".", type=click.Path(exists=True)) | ||
def dock(path: str) -> None: | ||
""" | ||
Apply defaults of the integration from the .port/resources PATH. | ||
PATH: Path to the integration. If not provided, the current directory will be used. | ||
""" | ||
print_logo() | ||
|
||
console.print("Unloading cargo at the dock... 📦🚢") | ||
|
||
default_app = create_default_app(path) | ||
|
||
main_path = f"{path}/main.py" if path else "main.py" | ||
|
||
app_module = load_module(main_path) | ||
app: Ocean = {name: item for name, item in getmembers(app_module)}.get( | ||
"app", | ||
default_app, | ||
) | ||
|
||
initialize_defaults( | ||
app.integration.AppConfigHandlerClass.CONFIG_CLASS, | ||
app.config, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from port_ocean.cli.commands.main import cli_start | ||
|
||
|
||
@cli_start.group("defaults") | ||
def defaults() -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import asyncio | ||
|
||
import httpx | ||
from loguru import logger | ||
|
||
from port_ocean.clients.port.authentication import PortAuthentication | ||
from port_ocean.clients.port.utils import handle_status_code | ||
from port_ocean.core.models import Migration | ||
|
||
|
||
class MigrationClientMixin: | ||
def __init__(self, auth: PortAuthentication, client: httpx.AsyncClient): | ||
self.auth = auth | ||
self.client = client | ||
|
||
async def wait_for_migration_to_complete( | ||
self, | ||
migration_id: list[str], | ||
interval: int = 5, | ||
) -> Migration: | ||
logger.info( | ||
f"Waiting for migration with id: {migration_id} to complete", | ||
) | ||
|
||
headers = await self.auth.headers() | ||
response = await self.client.get( | ||
f"{self.auth.api_url}/migrations/{migration_id}", | ||
headers=headers, | ||
) | ||
|
||
handle_status_code(response, should_raise=True) | ||
|
||
migration_status = response.json().get("migration", {}).get("status", None) | ||
if ( | ||
migration_status == "RUNNING" | ||
or migration_status == "INITIALIZING" | ||
or migration_status == "PENDING" | ||
): | ||
await asyncio.sleep(interval) | ||
await self.wait_for_migration_to_complete(migration_id, interval) | ||
else: | ||
logger.info( | ||
f"Migration with id: {migration_id} finished with status {migration_status}", | ||
) | ||
|
||
return Migration.parse_obj(response.json()["migration"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .clean import clean_defaults | ||
from .initialize import initialize_defaults | ||
|
||
__all__ = [ | ||
"clean_defaults", | ||
"initialize_defaults", | ||
] |
Oops, something went wrong.