forked from airbytehq/airbyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐙 octavia-cli: implement init command (airbytehq#9665)
- Loading branch information
1 parent
9aade15
commit a73ed08
Showing
10 changed files
with
356 additions
and
33 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,78 @@ | ||
# | ||
# Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import os | ||
|
||
import airbyte_api_client | ||
import click | ||
from airbyte_api_client.api import health_api, workspace_api | ||
from airbyte_api_client.model.workspace_id_request_body import WorkspaceIdRequestBody | ||
from urllib3.exceptions import MaxRetryError | ||
|
||
from .init.commands import DIRECTORIES_TO_CREATE as REQUIRED_PROJECT_DIRECTORIES | ||
|
||
|
||
class UnhealthyApiError(click.ClickException): | ||
pass | ||
|
||
|
||
class UnreachableAirbyteInstanceError(click.ClickException): | ||
pass | ||
|
||
|
||
class WorkspaceIdError(click.ClickException): | ||
pass | ||
|
||
|
||
def check_api_health(api_client: airbyte_api_client.ApiClient) -> None: | ||
"""Check if the Airbyte API is network reachable and healthy. | ||
Args: | ||
api_client (airbyte_api_client.ApiClient): Airbyte API client. | ||
Raises: | ||
click.ClickException: Raised if the Airbyte api server is unavailable according to the API response. | ||
click.ClickException: Raised if the Airbyte URL is not reachable. | ||
""" | ||
api_instance = health_api.HealthApi(api_client) | ||
try: | ||
api_response = api_instance.get_health_check() | ||
if not api_response.available: | ||
raise UnhealthyApiError( | ||
"Your Airbyte instance is not ready to receive requests: the health endpoint returned 'available: False.'" | ||
) | ||
except (airbyte_api_client.ApiException, MaxRetryError) as e: | ||
raise UnreachableAirbyteInstanceError( | ||
f"Could not reach your Airbyte instance, make sure the instance is up and running an network reachable: {e}" | ||
) | ||
|
||
|
||
def check_workspace_exists(api_client: airbyte_api_client.ApiClient, workspace_id: str) -> None: | ||
"""Check if the provided workspace id corresponds to an existing workspace on the Airbyte instance. | ||
Args: | ||
api_client (airbyte_api_client.ApiClient): Airbyte API client. | ||
workspace_id (str): Id of the workspace whose existence we are trying to verify. | ||
Raises: | ||
click.ClickException: Raised if the workspace does not exist on the Airbyte instance. | ||
""" | ||
api_instance = workspace_api.WorkspaceApi(api_client) | ||
try: | ||
api_instance.get_workspace(WorkspaceIdRequestBody(workspace_id=workspace_id), _check_return_type=False) | ||
except airbyte_api_client.ApiException: | ||
raise WorkspaceIdError("The workspace you are trying to use does not exist in your Airbyte instance") | ||
|
||
|
||
def check_is_initialized(project_directory: str = ".") -> bool: | ||
"""Check if required project directories exist to consider the project as initialized. | ||
Args: | ||
project_directory (str, optional): Where the project should be initialized. Defaults to ".". | ||
Returns: | ||
bool: [description] | ||
""" | ||
sub_directories = [f.name for f in os.scandir(project_directory) if f.is_dir()] | ||
return set(REQUIRED_PROJECT_DIRECTORIES).issubset(sub_directories) |
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,3 @@ | ||
# | ||
# Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
# |
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,34 @@ | ||
# | ||
# Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import os | ||
from typing import Iterable, Tuple | ||
|
||
import click | ||
|
||
DIRECTORIES_TO_CREATE = {"connections", "destinations", "sources"} | ||
|
||
|
||
def create_directories(directories_to_create: Iterable[str]) -> Tuple[Iterable[str], Iterable[str]]: | ||
created_directories = [] | ||
not_created_directories = [] | ||
for directory in directories_to_create: | ||
try: | ||
os.mkdir(directory) | ||
created_directories.append(directory) | ||
except FileExistsError: | ||
not_created_directories.append(directory) | ||
return created_directories, not_created_directories | ||
|
||
|
||
@click.command(help="Initialize required directories for the project.") | ||
def init(): | ||
click.echo("🔨 - Initializing the project.") | ||
created_directories, not_created_directories = create_directories(DIRECTORIES_TO_CREATE) | ||
if created_directories: | ||
message = f"✅ - Created the following directories: {', '.join(created_directories)}." | ||
click.echo(click.style(message, fg="green")) | ||
if not_created_directories: | ||
message = f"❓ - Already existing directories: {', '.join(not_created_directories) }." | ||
click.echo(click.style(message, fg="yellow", bold=True)) |
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,85 @@ | ||
# | ||
# Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import os | ||
import shutil | ||
import tempfile | ||
from pathlib import Path | ||
|
||
import airbyte_api_client | ||
import pytest | ||
from airbyte_api_client.model.workspace_id_request_body import WorkspaceIdRequestBody | ||
from octavia_cli import check_context | ||
from urllib3.exceptions import MaxRetryError | ||
|
||
|
||
@pytest.fixture | ||
def mock_api_client(mocker): | ||
return mocker.Mock() | ||
|
||
|
||
def test_api_check_health_available(mock_api_client, mocker): | ||
mocker.patch.object(check_context, "health_api") | ||
mock_api_response = mocker.Mock(available=True) | ||
check_context.health_api.HealthApi.return_value.get_health_check.return_value = mock_api_response | ||
|
||
assert check_context.check_api_health(mock_api_client) is None | ||
check_context.health_api.HealthApi.assert_called_with(mock_api_client) | ||
api_instance = check_context.health_api.HealthApi.return_value | ||
api_instance.get_health_check.assert_called() | ||
|
||
|
||
def test_api_check_health_unavailable(mock_api_client, mocker): | ||
mocker.patch.object(check_context, "health_api") | ||
mock_api_response = mocker.Mock(available=False) | ||
check_context.health_api.HealthApi.return_value.get_health_check.return_value = mock_api_response | ||
with pytest.raises(check_context.UnhealthyApiError): | ||
check_context.check_api_health(mock_api_client) | ||
|
||
|
||
def test_api_check_health_unreachable_api_exception(mock_api_client, mocker): | ||
mocker.patch.object(check_context, "health_api") | ||
check_context.health_api.HealthApi.return_value.get_health_check.side_effect = airbyte_api_client.ApiException() | ||
with pytest.raises(check_context.UnreachableAirbyteInstanceError): | ||
check_context.check_api_health(mock_api_client) | ||
|
||
|
||
def test_api_check_health_unreachable_max_retry_error(mock_api_client, mocker): | ||
mocker.patch.object(check_context, "health_api") | ||
check_context.health_api.HealthApi.return_value.get_health_check.side_effect = MaxRetryError("foo", "bar") | ||
with pytest.raises(check_context.UnreachableAirbyteInstanceError): | ||
check_context.check_api_health(mock_api_client) | ||
|
||
|
||
def test_check_workspace_exists(mock_api_client, mocker): | ||
mocker.patch.object(check_context, "workspace_api") | ||
mock_api_instance = mocker.Mock() | ||
check_context.workspace_api.WorkspaceApi.return_value = mock_api_instance | ||
assert check_context.check_workspace_exists(mock_api_client, "foo") is None | ||
check_context.workspace_api.WorkspaceApi.assert_called_with(mock_api_client) | ||
mock_api_instance.get_workspace.assert_called_with(WorkspaceIdRequestBody("foo"), _check_return_type=False) | ||
|
||
|
||
def test_check_workspace_exists_error(mock_api_client, mocker): | ||
mocker.patch.object(check_context, "workspace_api") | ||
check_context.workspace_api.WorkspaceApi.return_value.get_workspace.side_effect = airbyte_api_client.ApiException() | ||
with pytest.raises(check_context.WorkspaceIdError): | ||
check_context.check_workspace_exists(mock_api_client, "foo") | ||
|
||
|
||
@pytest.fixture | ||
def project_directories(): | ||
dirpath = tempfile.mkdtemp() | ||
yield str(Path(dirpath).parent.absolute()), [os.path.basename(dirpath)] | ||
shutil.rmtree(dirpath) | ||
|
||
|
||
def test_check_is_initialized(mocker, project_directories): | ||
project_directory, sub_directories = project_directories | ||
mocker.patch.object(check_context, "REQUIRED_PROJECT_DIRECTORIES", sub_directories) | ||
assert check_context.check_is_initialized(project_directory) | ||
|
||
|
||
def test_check_not_initialized(): | ||
assert not check_context.check_is_initialized(".") |
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,3 @@ | ||
# | ||
# Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
# |
Oops, something went wrong.