-
Notifications
You must be signed in to change notification settings - Fork 4.6k
🐙 octavia-cli: implement init command #9665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,76 @@ | ||
# | ||
# 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.") | ||
except (airbyte_api_client.ApiException, MaxRetryError): | ||
raise UnreachableAirbyteInstanceError( | ||
"Could not reach your Airbyte instance, make sure the instance is up and running an network reachable." | ||
alafanechere marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
|
||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.