-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Service cleanup in Azure AI Search (#15063)
- Loading branch information
1 parent
8cce6be
commit b2ef9d7
Showing
6 changed files
with
108 additions
and
106 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
86 changes: 86 additions & 0 deletions
86
src/OrchardCore/OrchardCore.Search.AzureAI.Core/Services/AzureAIClientFactory.cs
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,86 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using Azure.Identity; | ||
using Azure.Search.Documents; | ||
using Azure.Search.Documents.Indexes; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.Search.AzureAI.Models; | ||
|
||
namespace OrchardCore.Search.AzureAI.Services; | ||
|
||
public class AzureAIClientFactory(IOptions<AzureAISearchDefaultOptions> defaultOptions) | ||
{ | ||
private readonly ConcurrentDictionary<string, SearchClient> _clients = []; | ||
private readonly AzureAISearchDefaultOptions _defaultOptions = defaultOptions.Value; | ||
|
||
private SearchIndexClient _searchIndexClient; | ||
|
||
public SearchClient CreateSearchClient(string indexFullName) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(indexFullName, nameof(indexFullName)); | ||
|
||
if (!_clients.TryGetValue(indexFullName, out var client)) | ||
{ | ||
if (!_defaultOptions.IsConfigurationExists()) | ||
{ | ||
throw new Exception("Azure AI was not configured."); | ||
} | ||
|
||
if (!Uri.TryCreate(_defaultOptions.Endpoint, UriKind.Absolute, out var endpoint)) | ||
{ | ||
throw new Exception("The Endpoint provided to Azure AI Options contains invalid value."); | ||
} | ||
|
||
if (_defaultOptions.AuthenticationType == AzureAIAuthenticationType.ApiKey && _defaultOptions.Credential != null) | ||
{ | ||
client = new SearchClient(endpoint, indexFullName, _defaultOptions.Credential); | ||
} | ||
else if (_defaultOptions.AuthenticationType == AzureAIAuthenticationType.ManagedIdentity) | ||
{ | ||
client = new SearchClient(endpoint, indexFullName, GetManagedIdentityCredential()); | ||
} | ||
else | ||
{ | ||
client = new SearchClient(endpoint, indexFullName, new DefaultAzureCredential()); | ||
} | ||
|
||
_clients.TryAdd(indexFullName, client); | ||
} | ||
|
||
return client; | ||
} | ||
|
||
public SearchIndexClient CreateSearchIndexClient() | ||
{ | ||
if (_searchIndexClient == null) | ||
{ | ||
if (!_defaultOptions.IsConfigurationExists()) | ||
{ | ||
throw new Exception("Azure AI was not configured."); | ||
} | ||
|
||
if (!Uri.TryCreate(_defaultOptions.Endpoint, UriKind.Absolute, out var endpoint)) | ||
{ | ||
throw new Exception("The Endpoint provided to Azure AI Options contains invalid value."); | ||
} | ||
|
||
if (_defaultOptions.AuthenticationType == AzureAIAuthenticationType.ApiKey && _defaultOptions.Credential != null) | ||
{ | ||
_searchIndexClient = new SearchIndexClient(endpoint, _defaultOptions.Credential); | ||
} | ||
else if (_defaultOptions.AuthenticationType == AzureAIAuthenticationType.ManagedIdentity) | ||
{ | ||
_searchIndexClient = new SearchIndexClient(endpoint, GetManagedIdentityCredential()); | ||
} | ||
else | ||
{ | ||
_searchIndexClient = new SearchIndexClient(endpoint, new DefaultAzureCredential()); | ||
} | ||
} | ||
|
||
return _searchIndexClient; | ||
} | ||
|
||
private ManagedIdentityCredential GetManagedIdentityCredential() | ||
=> new(_defaultOptions.IdentityClientId); | ||
} |
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
57 changes: 0 additions & 57 deletions
57
src/OrchardCore/OrchardCore.Search.AzureAI.Core/Services/SearchClientFactory.cs
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
src/OrchardCore/OrchardCore.Search.AzureAI.Core/Services/SearchIndexClientAccessor.cs
This file was deleted.
Oops, something went wrong.