Skip to content
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

Define onlyIfUnchanged parameter for CreateOrUpdate and Delete operations #11420

Merged
merged 7 commits into from
Apr 28, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Define onlyIfUnchanged for ETag support (#2)
This was suggested to match what AppConfiguration does. It simplifies it, with no practical reason for IfNoneMatch. We could always add that later as well.
  • Loading branch information
heaths committed Apr 23, 2020
commit 5ac4930a1a5ad3ab5b9154402b12da3c39a61a08
64 changes: 42 additions & 22 deletions sdk/search/Azure.Search.Documents/src/SearchServiceClient.cs
Original file line number Diff line number Diff line change
@@ -1443,7 +1443,11 @@ await SynonymMapsClient.CreateAsync(
/// Creates a new synonym map or updates an existing synonym map.
/// </summary>
/// <param name="synonymMap">Required. The <see cref="SynonymMap"/> to create or update.</param>
/// <param name="options">Optional <see cref="SearchConditionalOptions"/> to customize the operation's behavior.</param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SynonymMap.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="options">Optional <see cref="SearchRequestOptions"/> to customize the operation's behavior.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Response{T}"/> from the server containing the <see cref="SynonymMap"/> that was created.
@@ -1454,20 +1458,25 @@ await SynonymMapsClient.CreateAsync(
[ForwardsClientCalls]
public virtual Response<SynonymMap> CreateOrUpdateSynonymMap(
SynonymMap synonymMap,
SearchConditionalOptions options = null,
bool onlyIfUnchanged = false,
SearchRequestOptions options = null,
CancellationToken cancellationToken = default) =>
SynonymMapsClient.CreateOrUpdate(
synonymMap?.Name,
synonymMap,
options?.ClientRequestId,
options?.IfMatch?.ToString(),
options?.IfNoneMatch?.ToString(),
onlyIfUnchanged ? synonymMap?.ETag?.ToString() : null,
null,
cancellationToken);

/// <summary>
/// Creates a new synonym map or updates an existing synonym map.
/// </summary>
/// <param name="synonymMap">Required. The <see cref="SynonymMap"/> to create or update.</param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SynonymMap.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="options">Optional <see cref="SearchConditionalOptions"/> to customize the operation's behavior.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
@@ -1479,57 +1488,68 @@ public virtual Response<SynonymMap> CreateOrUpdateSynonymMap(
[ForwardsClientCalls]
public virtual async Task<Response<SynonymMap>> CreateOrUpdateSynonymMapAsync(
SynonymMap synonymMap,
bool onlyIfUnchanged = false,
SearchConditionalOptions options = null,
CancellationToken cancellationToken = default) =>
await SynonymMapsClient.CreateOrUpdateAsync(
synonymMap?.Name,
synonymMap,
options?.ClientRequestId,
options?.IfMatch?.ToString(),
options?.IfNoneMatch?.ToString(),
onlyIfUnchanged ? synonymMap?.ETag?.ToString() : null,
null,
cancellationToken)
.ConfigureAwait(false);

/// <summary>
/// Deletes a synonym map.
/// </summary>
/// <param name="synonymMapName">The name of the synonym map to delete.</param>
/// <param name="options">Optional <see cref="SearchConditionalOptions"/> to customize the operation's behavior.</param>
/// <param name="synonymMap">The <see cref="SynonymMap"/> to delete.</param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SynonymMap.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="options">Optional <see cref="SearchRequestOptions"/> to customize the operation's behavior.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response"/> from the server.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="synonymMapName"/> is null.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="synonymMap"/> or <see cref="SynonymMap.Name"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
[ForwardsClientCalls]
public virtual Response DeleteSynonymMap(
string synonymMapName,
SearchConditionalOptions options = null,
SynonymMap synonymMap,
bool onlyIfUnchanged = false,
SearchRequestOptions options = null,
CancellationToken cancellationToken = default) =>
SynonymMapsClient.Delete(
synonymMapName,
synonymMap?.Name,
options?.ClientRequestId,
options?.IfMatch?.ToString(),
options?.IfNoneMatch?.ToString(),
onlyIfUnchanged ? synonymMap?.ETag?.ToString() : null,
null,
cancellationToken);

/// <summary>
/// Deletes a synonym map.
/// </summary>
/// <param name="synonymMapName">The name of the synonym map to delete.</param>
/// <param name="options">Optional <see cref="SearchConditionalOptions"/> to customize the operation's behavior.</param>
/// <param name="synonymMap">The <see cref="SynonymMap"/> to delete.</param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SynonymMap.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="options">Optional <see cref="SearchRequestOptions"/> to customize the operation's behavior.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response"/> from the server.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="synonymMapName"/> is null.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="synonymMap"/> or <see cref="SynonymMap.Name"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
[ForwardsClientCalls]
public virtual async Task<Response> DeleteSynonymMapAsync(
string synonymMapName,
SearchConditionalOptions options = null,
SynonymMap synonymMap,
bool onlyIfUnchanged = false,
SearchRequestOptions options = null,
CancellationToken cancellationToken = default) =>
await SynonymMapsClient.DeleteAsync(
synonymMapName,
synonymMap?.Name,
options?.ClientRequestId,
options?.IfMatch?.ToString(),
options?.IfNoneMatch?.ToString(),
onlyIfUnchanged ? synonymMap?.ETag?.ToString() : null,
null,
cancellationToken)
.ConfigureAwait(false);

34 changes: 22 additions & 12 deletions sdk/search/Azure.Search.Documents/tests/SearchServiceClientTests.cs
Original file line number Diff line number Diff line change
@@ -346,31 +346,41 @@ public async Task CrudSynonymMaps()

SearchServiceClient client = resources.GetServiceClient();

SynonymMap map = await client.CreateSynonymMapAsync(new SynonymMap(synonymMapName, "msft=>Microsoft"));
Assert.AreEqual(synonymMapName, map.Name);
Assert.AreEqual("solr", map.Format);
Assert.AreEqual("msft=>Microsoft", map.Synonyms);
SynonymMap createdMap = await client.CreateSynonymMapAsync(new SynonymMap(synonymMapName, "msft=>Microsoft"));
Assert.AreEqual(synonymMapName, createdMap.Name);
Assert.AreEqual("solr", createdMap.Format);
Assert.AreEqual("msft=>Microsoft", createdMap.Synonyms);

map = await client.CreateOrUpdateSynonymMapAsync(new SynonymMap(synonymMapName, "ms,msft=>Microsoft"), new SearchConditionalOptions { IfMatch = map.ETag });
Assert.AreEqual(synonymMapName, map.Name);
Assert.AreEqual("solr", map.Format);
Assert.AreEqual("ms,msft=>Microsoft", map.Synonyms);
SynonymMap updatedMap = await client.CreateOrUpdateSynonymMapAsync(
new SynonymMap(synonymMapName, "ms,msft=>Microsoft")
{
ETag = createdMap.ETag,
},
onlyIfUnchanged: true);
Assert.AreEqual(synonymMapName, updatedMap.Name);
Assert.AreEqual("solr", updatedMap.Format);
Assert.AreEqual("ms,msft=>Microsoft", updatedMap.Synonyms);

RequestFailedException ex = await CatchAsync<RequestFailedException>(async () =>
await client.CreateOrUpdateSynonymMapAsync(new SynonymMap(synonymMapName, "ms,msft=>Microsoft"), new SearchConditionalOptions { IfNoneMatch = map.ETag }));
await client.CreateOrUpdateSynonymMapAsync(
new SynonymMap(synonymMapName, "ms,msft=>Microsoft")
{
ETag = createdMap.ETag,
},
onlyIfUnchanged: true));
Assert.AreEqual((int)HttpStatusCode.PreconditionFailed, ex.Status);

Response<IReadOnlyList<SynonymMap>> mapsResponse = await client.GetSynonymMapsAsync(new[] { nameof(SynonymMap.Name) });
foreach (SynonymMap namedMap in mapsResponse.Value)
{
if (string.Equals(map.Name, namedMap.Name, StringComparison.OrdinalIgnoreCase))
if (string.Equals(updatedMap.Name, namedMap.Name, StringComparison.OrdinalIgnoreCase))
{
SynonymMap fetchedMap = await client.GetSynonymMapAsync(namedMap.Name);
Assert.AreEqual(map.Synonyms, fetchedMap.Synonyms);
Assert.AreEqual(updatedMap.Synonyms, fetchedMap.Synonyms);
}
}

await client.DeleteSynonymMapAsync(map.Name, new SearchConditionalOptions { IfMatch = map.ETag });
await client.DeleteSynonymMapAsync(updatedMap, onlyIfUnchanged: true);
}

/// <summary>
Loading