Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
ConditionalSkill,
CorsOptions,
CustomAnalyzer,
DataSource,
DataSourceCredentials,
DataContainer,
DictionaryDecompounderTokenFilter,
DistanceScoringFunction,
DistanceScoringParameters,
Expand Down Expand Up @@ -135,6 +138,9 @@
"ConditionalSkill",
"CorsOptions",
"CustomAnalyzer",
"DataSource",
"DataSourceCredentials",
"DataContainer",
"DictionaryDecompounderTokenFilter",
"DistanceScoringFunction",
"DistanceScoringParameters",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from ._generated.models import Analyzer, Tokenizer
from ._generated.models import (
Analyzer,
Tokenizer
)


class PatternAnalyzer(Analyzer):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
# pylint:disable=unused-import,ungrouped-imports
from typing import Any, List, Sequence, Union
from azure.core.credentials import AzureKeyCredential
from ._generated.models import Skill
from ._generated.models import Skill, DataSource
from .. import Index, AnalyzeResult, AnalyzeRequest


class SearchServiceClient(HeadersMixin):
class SearchServiceClient(HeadersMixin): # pylint: disable=too-many-public-methods
"""A client to interact with an existing Azure search service.

:param endpoint: The URL endpoint of an Azure search service
Expand Down Expand Up @@ -554,3 +554,114 @@ def create_or_update_skillset(self, name, **kwargs):
)

return self._client.skillsets.create_or_update(name, skillset, **kwargs)

@distributed_trace
def create_datasource(self, data_source, **kwargs):
# type: (str, DataSource, **Any) -> Dict[str, Any]
"""Creates a new datasource.

:param data_source: The definition of the datasource to create.
:type data_source: ~search.models.DataSource
:return: The created DataSource
:rtype: dict

.. admonition:: Example:

.. literalinclude:: ../samples/sample_data_source_operations.py
:start-after: [START create_data_source]
:end-before: [END create_data_source]
:language: python
:dedent: 4
:caption: Create a Data Source
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = self._client.data_sources.create(data_source, **kwargs)
return result

@distributed_trace
def create_or_update_datasource(self, data_source, name=None, **kwargs):
# type: (str, DataSource, Optional[str], **Any) -> Dict[str, Any]
"""Creates a new datasource or updates a datasource if it already exists.

:param name: The name of the datasource to create or update.
:type name: str
:param data_source: The definition of the datasource to create or update.
:type data_source: ~search.models.DataSource
:return: The created DataSource
:rtype: dict
"""
# TODO: access_condition
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))

if not name:
name = data_source.name
result = self._client.data_sources.create_or_update(name, data_source, **kwargs)
return result

@distributed_trace
def get_datasource(self, name, **kwargs):
# type: (str, **Any) -> Dict[str, Any]
"""Retrieves a datasource definition.

:param name: The name of the datasource to retrieve.
:type name: str
:return: The DataSource that is fetched.
:rtype: dict

.. admonition:: Example:

.. literalinclude:: ../samples/sample_data_source_operations.py
:start-after: [START get_data_source]
:end-before: [END get_data_source]
:language: python
:dedent: 4
:caption: Retrieve a DataSource
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = self._client.data_sources.get(name, **kwargs)
return result

@distributed_trace
def get_datasources(self, **kwargs):
# type: (**Any) -> Sequence[DataSource]
"""Lists all datasources available for a search service.

:return: List of all the data sources.
:rtype: `list[dict]`

.. admonition:: Example:

.. literalinclude:: ../samples/sample_data_source_operations.py
:start-after: [START list_data_source]
:end-before: [END list_data_source]
:language: python
:dedent: 4
:caption: List all the DataSources
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = self._client.data_sources.list(**kwargs)
return result.data_sources
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per @johanste This will either need to return a PagedItem or be renamed to get_datasources (same comment for async version) @brjohnstmsft has indicated that the only one that realistically might be paged on the service side is the indexes list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you confirm if there is an upper limit for the number of data sources?

Copy link
Contributor Author

@rakshith91 rakshith91 Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will implement paging in a separate issue - that way this wont be blocked for indexers

#11025

EDIT

keeping in mind that service doesn't support paging yet, at this point we would want paging only for indexes (since there can be upto 3000 indexes). Realistically, datasources shouldn't have paging support.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So with #11025 closed, this should be renamed to get_datasources I think?


@distributed_trace
def delete_datasource(self, name, **kwargs):
# type: (str, **Any) -> None
"""Deletes a datasource.

:param name: The name of the datasource to delete.
:type name: str

:return: None
:rtype: None

.. admonition:: Example:

.. literalinclude:: ../samples/sample_data_source_operations.py
:start-after: [START delete_data_source]
:end-before: [END delete_data_source]
:language: python
:dedent: 4
:caption: Delete a DataSource
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = self._client.data_sources.delete(name, **kwargs)
return result
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from ... import Index, AnalyzeResult, AnalyzeRequest


class SearchServiceClient(HeadersMixin):
class SearchServiceClient(HeadersMixin): # pylint: disable=too-many-public-methods
"""A client to interact with an existing Azure search service.

:param endpoint: The URL endpoint of an Azure search service
Expand Down Expand Up @@ -555,3 +555,109 @@ async def create_or_update_skillset(self, name, **kwargs):
)

return await self._client.skillsets.create_or_update(name, skillset, **kwargs)

@distributed_trace_async
async def create_datasource(self, data_source, **kwargs):
# type: (str, DataSource, **Any) -> Dict[str, Any]
"""Creates a new datasource.
:param data_source: The definition of the datasource to create.
:type data_source: ~search.models.DataSource
:return: The created DataSource
:rtype: dict

.. admonition:: Example:

.. literalinclude:: ../samples/async_samples/sample_data_source_operations_async.py
:start-after: [START create_data_source_async]
:end-before: [END create_data_source_async]
:language: python
:dedent: 4
:caption: Create a DataSource
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = await self._client.data_sources.create(data_source, **kwargs)
return result

@distributed_trace_async
async def create_or_update_datasource(self, data_source, name=None, **kwargs):
# type: (str, DataSource, Optional[str], **Any) -> Dict[str, Any]
"""Creates a new datasource or updates a datasource if it already exists.

:param name: The name of the datasource to create or update.
:type name: str
:param data_source: The definition of the datasource to create or update.
:type data_source: ~search.models.DataSource
:return: The created DataSource
:rtype: dict
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))

if not name:
name = data_source.name
result = await self._client.data_sources.create_or_update(name, data_source, **kwargs)
return result

@distributed_trace_async
async def delete_datasource(self, name, **kwargs):
# type: (str, **Any) -> None
"""Deletes a datasource.

:param name: The name of the datasource to delete.
:type name: str

:return: None
:rtype: None

.. admonition:: Example:

.. literalinclude:: ../samples/async_samples/sample_data_source_operations_async.py
:start-after: [START delete_data_source_async]
:end-before: [END delete_data_source_async]
:language: python
:dedent: 4
:caption: Delete a DataSource
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = await self._client.data_sources.delete(name, **kwargs)
return result

@distributed_trace_async
async def get_datasource(self, name, **kwargs):
# type: (str, **Any) -> Dict[str, Any]
"""Retrieves a datasource definition.

:param name: The name of the datasource to retrieve.
:type name: str
:return: The DataSource that is fetched.

.. literalinclude:: ../samples/async_samples/sample_data_source_operations_async.py
:start-after: [START get_data_source_async]
:end-before: [END get_data_source_async]
:language: python
:dedent: 4
:caption: Retrieve a DataSource
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = await self._client.data_sources.get(name, **kwargs)
return result

@distributed_trace_async
async def get_datasources(self, **kwargs):
# type: (**Any) -> Sequence[DataSource]
"""Lists all datasources available for a search service.

:return: List of all the data sources.
:rtype: `list[dict]`

.. admonition:: Example:

.. literalinclude:: ../samples/async_samples/sample_data_source_operations_async.py
:start-after: [START list_data_source_async]
:end-before: [END list_data_source_async]
:language: python
:dedent: 4
:caption: List all DataSources
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = await self._client.data_sources.list(**kwargs)
return result.data_sources
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: sample_data_source_operations_async.py
DESCRIPTION:
This sample demonstrates how to get, create, update, or delete a Synonym Map.
USAGE:
python sample_data_source_operations_async.py

Set the environment variables with your own values before running the sample:
1) AZURE_SEARCH_SERVICE_ENDPOINT - the endpoint of your Azure Cognitive Search service
2) AZURE_SEARCH_API_KEY - your search API key
"""

import asyncio
import os

service_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
key = os.getenv("AZURE_SEARCH_API_KEY")
connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING")

from azure.core.credentials import AzureKeyCredential
from azure.search.documents import DataSource, DataContainer, DataSourceCredentials
from azure.search.documents.aio import SearchServiceClient

service_client = SearchServiceClient(service_endpoint, AzureKeyCredential(key))

async def create_data_source():
# [START create_data_source_async]
credentials = DataSourceCredentials(connection_string=connection_string)
container = DataContainer(name='searchcontainer')
data_source = DataSource(name="async-sample-datasource", type="azureblob", credentials=credentials, container=container)
async with service_client:
result = await service_client.create_datasource(data_source)
print("Create new Data Source - async-sample-datasource")
# [END create_data_source_async]

async def list_data_sources():
# [START list_data_source_async]
async with service_client:
result = await service_client.get_datasources()
names = [x.name for x in result]
print("Found {} Data Sources in the service: {}".format(len(result), ", ".join(names)))
# [END list_data_source_async]

async def get_data_source():
# [START get_data_source_async]
async with service_client:
result = await service_client.get_datasource("async-sample-datasource")
print("Retrived Data Source 'async-sample-datasource'")
return result
# [END get_data_source_async]

async def delete_data_source():
# [START delete_data_source_async]
async with service_client:
service_client.delete_datasource("async-sample-datasource")
print("Data Source 'async-sample-datasource' successfully deleted")
# [END delete_data_source_async]

async def main():
await create_data_source()
await list_data_sources()
await get_data_source()
await delete_data_source()
await service_client.close()

if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Loading