|
| 1 | +# |
| 2 | +# Copyright (c) 2021 Airbyte, Inc., all rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +import abc |
| 6 | +from typing import List |
| 7 | + |
| 8 | +import airbyte_api_client |
| 9 | +import octavia_cli.list.formatting as formatting |
| 10 | +from airbyte_api_client.api import connection_api, destination_api, destination_definition_api, source_api, source_definition_api |
| 11 | +from airbyte_api_client.model.workspace_id_request_body import WorkspaceIdRequestBody |
| 12 | + |
| 13 | + |
| 14 | +class BaseListing(abc.ABC): |
| 15 | + COMMON_LIST_FUNCTION_KWARGS = {"_check_return_type": False} |
| 16 | + |
| 17 | + @property |
| 18 | + @abc.abstractmethod |
| 19 | + def api( |
| 20 | + self, |
| 21 | + ): # pragma: no cover |
| 22 | + pass |
| 23 | + |
| 24 | + @property |
| 25 | + @abc.abstractmethod |
| 26 | + def fields_to_display( |
| 27 | + self, |
| 28 | + ) -> List[str]: # pragma: no cover |
| 29 | + pass |
| 30 | + |
| 31 | + @property |
| 32 | + @abc.abstractmethod |
| 33 | + def list_field_in_response( |
| 34 | + self, |
| 35 | + ) -> str: # pragma: no cover |
| 36 | + pass |
| 37 | + |
| 38 | + @property |
| 39 | + @abc.abstractmethod |
| 40 | + def list_function_name( |
| 41 | + self, |
| 42 | + ) -> str: # pragma: no cover |
| 43 | + pass |
| 44 | + |
| 45 | + @property |
| 46 | + def _list_fn(self): |
| 47 | + return getattr(self.api, self.list_function_name) |
| 48 | + |
| 49 | + @property |
| 50 | + def list_function_kwargs(self) -> dict: |
| 51 | + return {} |
| 52 | + |
| 53 | + def __init__(self, api_client: airbyte_api_client.ApiClient): |
| 54 | + self.api_instance = self.api(api_client) |
| 55 | + |
| 56 | + def _parse_response(self, api_response) -> List[List[str]]: |
| 57 | + items = [[item[field] for field in self.fields_to_display] for item in api_response[self.list_field_in_response]] |
| 58 | + return items |
| 59 | + |
| 60 | + def get_listing(self) -> List[List[str]]: |
| 61 | + api_response = self._list_fn(self.api_instance, **self.list_function_kwargs, **self.COMMON_LIST_FUNCTION_KWARGS) |
| 62 | + return self._parse_response(api_response) |
| 63 | + |
| 64 | + def __repr__(self): |
| 65 | + items = [formatting.format_column_names(self.fields_to_display)] + self.get_listing() |
| 66 | + return formatting.display_as_table(items) |
| 67 | + |
| 68 | + |
| 69 | +class SourceConnectorsDefinitions(BaseListing): |
| 70 | + api = source_definition_api.SourceDefinitionApi |
| 71 | + fields_to_display = ["name", "dockerRepository", "dockerImageTag", "sourceDefinitionId"] |
| 72 | + list_field_in_response = "source_definitions" |
| 73 | + list_function_name = "list_latest_source_definitions" |
| 74 | + |
| 75 | + |
| 76 | +class DestinationConnectorsDefinitions(BaseListing): |
| 77 | + api = destination_definition_api.DestinationDefinitionApi |
| 78 | + fields_to_display = ["name", "dockerRepository", "dockerImageTag", "destinationDefinitionId"] |
| 79 | + list_field_in_response = "destination_definitions" |
| 80 | + list_function_name = "list_latest_destination_definitions" |
| 81 | + |
| 82 | + |
| 83 | +class WorkspaceListing(BaseListing, abc.ABC): |
| 84 | + def __init__(self, api_client: airbyte_api_client.ApiClient, workspace_id: str): |
| 85 | + self.workspace_id = workspace_id |
| 86 | + super().__init__(api_client) |
| 87 | + |
| 88 | + @property |
| 89 | + def list_function_kwargs(self) -> dict: |
| 90 | + return {"workspace_id_request_body": WorkspaceIdRequestBody(workspace_id=self.workspace_id)} |
| 91 | + |
| 92 | + |
| 93 | +class Sources(WorkspaceListing): |
| 94 | + api = source_api.SourceApi |
| 95 | + fields_to_display = ["name", "sourceName", "sourceId"] |
| 96 | + list_field_in_response = "sources" |
| 97 | + list_function_name = "list_sources_for_workspace" |
| 98 | + |
| 99 | + |
| 100 | +class Destinations(WorkspaceListing): |
| 101 | + api = destination_api.DestinationApi |
| 102 | + fields_to_display = ["name", "destinationName", "destinationId"] |
| 103 | + list_field_in_response = "destinations" |
| 104 | + list_function_name = "list_destinations_for_workspace" |
| 105 | + |
| 106 | + |
| 107 | +class Connections(WorkspaceListing): |
| 108 | + api = connection_api.ConnectionApi |
| 109 | + fields_to_display = ["name", "connectionId", "status", "sourceId", "destinationId"] |
| 110 | + list_field_in_response = "connections" |
| 111 | + list_function_name = "list_connections_for_workspace" |
0 commit comments