|
| 1 | +# Copyright 2016-2023. Couchbase, Inc. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License") |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from asyncio import AbstractEventLoop |
| 19 | +from typing import (TYPE_CHECKING, |
| 20 | + Dict, |
| 21 | + Iterable) |
| 22 | + |
| 23 | +from couchbase.management.logic.analytics_mgmt_req_builder import AnalyticsMgmtRequestBuilder |
| 24 | +from couchbase.management.logic.analytics_mgmt_types import (AnalyticsDataset, |
| 25 | + AnalyticsIndex, |
| 26 | + AnalyticsLink, |
| 27 | + AzureBlobExternalAnalyticsLink, |
| 28 | + CouchbaseRemoteAnalyticsLink, |
| 29 | + S3ExternalAnalyticsLink) |
| 30 | + |
| 31 | +if TYPE_CHECKING: |
| 32 | + from acouchbase.logic.client_adapter import AsyncClientAdapter |
| 33 | + from couchbase.management.logic.analytics_mgmt_types import (ConnectLinkRequest, |
| 34 | + CreateDatasetRequest, |
| 35 | + CreateDataverseRequest, |
| 36 | + CreateIndexRequest, |
| 37 | + CreateLinkRequest, |
| 38 | + DisconnectLinkRequest, |
| 39 | + DropDatasetRequest, |
| 40 | + DropDataverseRequest, |
| 41 | + DropIndexRequest, |
| 42 | + DropLinkRequest, |
| 43 | + GetAllDatasetsRequest, |
| 44 | + GetAllIndexesRequest, |
| 45 | + GetLinksRequest, |
| 46 | + GetPendingMutationsRequest, |
| 47 | + ReplaceLinkRequest) |
| 48 | + |
| 49 | + |
| 50 | +class AsyncAnalyticsMgmtImpl: |
| 51 | + def __init__(self, client_adapter: AsyncClientAdapter) -> None: |
| 52 | + self._client_adapter = client_adapter |
| 53 | + self._request_builder = AnalyticsMgmtRequestBuilder() |
| 54 | + |
| 55 | + @property |
| 56 | + def loop(self) -> AbstractEventLoop: |
| 57 | + """**INTERNAL**""" |
| 58 | + return self._client_adapter.loop |
| 59 | + |
| 60 | + @property |
| 61 | + def request_builder(self) -> AnalyticsMgmtRequestBuilder: |
| 62 | + """**INTERNAL**""" |
| 63 | + return self._request_builder |
| 64 | + |
| 65 | + async def connect_link(self, req: ConnectLinkRequest) -> None: |
| 66 | + """**INTERNAL**""" |
| 67 | + await self._client_adapter.execute_mgmt_request(req) |
| 68 | + |
| 69 | + async def create_dataset(self, req: CreateDatasetRequest) -> None: |
| 70 | + """**INTERNAL**""" |
| 71 | + await self._client_adapter.execute_mgmt_request(req) |
| 72 | + |
| 73 | + async def create_dataverse(self, req: CreateDataverseRequest) -> None: |
| 74 | + """**INTERNAL**""" |
| 75 | + await self._client_adapter.execute_mgmt_request(req) |
| 76 | + |
| 77 | + async def create_index(self, req: CreateIndexRequest) -> None: |
| 78 | + """**INTERNAL**""" |
| 79 | + await self._client_adapter.execute_mgmt_request(req) |
| 80 | + |
| 81 | + async def create_link(self, req: CreateLinkRequest) -> None: |
| 82 | + """**INTERNAL**""" |
| 83 | + await self._client_adapter.execute_mgmt_request(req) |
| 84 | + |
| 85 | + async def disconnect_link(self, req: DisconnectLinkRequest) -> None: |
| 86 | + """**INTERNAL**""" |
| 87 | + await self._client_adapter.execute_mgmt_request(req) |
| 88 | + |
| 89 | + async def drop_dataset(self, req: DropDatasetRequest) -> None: |
| 90 | + """**INTERNAL**""" |
| 91 | + await self._client_adapter.execute_mgmt_request(req) |
| 92 | + |
| 93 | + async def drop_dataverse(self, req: DropDataverseRequest) -> None: |
| 94 | + """**INTERNAL**""" |
| 95 | + await self._client_adapter.execute_mgmt_request(req) |
| 96 | + |
| 97 | + async def drop_index(self, req: DropIndexRequest) -> None: |
| 98 | + """**INTERNAL**""" |
| 99 | + await self._client_adapter.execute_mgmt_request(req) |
| 100 | + |
| 101 | + async def drop_link(self, req: DropLinkRequest) -> None: |
| 102 | + """**INTERNAL**""" |
| 103 | + await self._client_adapter.execute_mgmt_request(req) |
| 104 | + |
| 105 | + async def get_all_datasets(self, req: GetAllDatasetsRequest) -> Iterable[AnalyticsDataset]: |
| 106 | + """**INTERNAL**""" |
| 107 | + datasets = [] |
| 108 | + ret = await self._client_adapter.execute_mgmt_request(req) |
| 109 | + raw_datasets = ret.raw_result.get('datasets', None) |
| 110 | + if raw_datasets: |
| 111 | + datasets = [AnalyticsDataset(**ds) for ds in raw_datasets] |
| 112 | + return datasets |
| 113 | + |
| 114 | + async def get_all_indexes(self, req: GetAllIndexesRequest) -> Iterable[AnalyticsIndex]: |
| 115 | + """**INTERNAL**""" |
| 116 | + indexes = [] |
| 117 | + ret = await self._client_adapter.execute_mgmt_request(req) |
| 118 | + raw_indexes = ret.raw_result.get('indexes', None) |
| 119 | + if raw_indexes: |
| 120 | + indexes = [AnalyticsIndex(**ds) for ds in raw_indexes] |
| 121 | + |
| 122 | + return indexes |
| 123 | + |
| 124 | + async def get_links(self, req: GetLinksRequest) -> Iterable[AnalyticsLink]: |
| 125 | + """**INTERNAL**""" |
| 126 | + analytics_links = [] |
| 127 | + ret = await self._client_adapter.execute_mgmt_request(req) |
| 128 | + cb_links = ret.raw_result.get('couchbase_links', None) |
| 129 | + if cb_links and len(cb_links) > 0: |
| 130 | + analytics_links.extend(map(lambda l: CouchbaseRemoteAnalyticsLink.link_from_server_json(l), cb_links)) |
| 131 | + s3_links = ret.raw_result.get('s3_links', None) |
| 132 | + if s3_links and len(s3_links) > 0: |
| 133 | + analytics_links.extend(map(lambda l: S3ExternalAnalyticsLink.link_from_server_json(l), s3_links)) |
| 134 | + azure_blob_links = ret.raw_result.get('azure_blob_links', None) |
| 135 | + if azure_blob_links and len(azure_blob_links) > 0: |
| 136 | + analytics_links.extend( |
| 137 | + map(lambda l: AzureBlobExternalAnalyticsLink.link_from_server_json(l), azure_blob_links)) |
| 138 | + |
| 139 | + return analytics_links |
| 140 | + |
| 141 | + async def get_pending_mutations(self, req: GetPendingMutationsRequest) -> Dict[str, int]: |
| 142 | + """**INTERNAL**""" |
| 143 | + ret = await self._client_adapter.execute_mgmt_request(req) |
| 144 | + return ret.raw_result.get('stats', {}) |
| 145 | + |
| 146 | + async def replace_link(self, req: ReplaceLinkRequest) -> None: |
| 147 | + """**INTERNAL**""" |
| 148 | + await self._client_adapter.execute_mgmt_request(req) |
0 commit comments