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

5521 - Rename TryGetValue to TryGet/TryGetAsync #19478

Merged
merged 14 commits into from
Mar 12, 2021
39 changes: 29 additions & 10 deletions sdk/resourcemanager/Azure.ResourceManager.Core/src/ContainerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Azure.Core;
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;

namespace Azure.ResourceManager.Core
{
Expand Down Expand Up @@ -37,29 +39,47 @@ protected ContainerBase(ResourceOperationsBase parent)
}

/// <summary>
/// Gets the parent resource of this resource
/// Gets the parent resource of this resource.
/// </summary>
protected ResourceOperationsBase Parent { get; }

/// <summary>
/// Returns the resource from Azure if it exists
/// Returns the resource from Azure if it exists.
/// </summary>
/// <param name="resourceName"> The name of the resource you want to get. </param>
/// <param name="resource"> The resource if it existed, null otherwise. </param>
/// <returns> Whether or not the resource existed. </returns>
public virtual bool TryGetValue(string resourceName, out ArmResponse<TOperations> resource)
public virtual TOperations TryGet(string resourceName)
{
var op = GetOperation(resourceName);

try
{
resource = op.Get();
return true;
return op.Get().Value;
}
catch (RequestFailedException e) when (e.Status == 404)
{
return null;
}
}

/// <summary>
/// Returns the resource from Azure if it exists.
/// </summary>
/// <param name="resourceName"> The name of the resource you want to get. </param>
/// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service.
/// The default value is <see cref="CancellationToken.None" />. </param>
/// <returns> Whether or not the resource existed. </returns>
public async virtual Task<TOperations> TryGetAsync(string resourceName, CancellationToken cancellationToken = default)
{
var op = GetOperation(resourceName);

try
{
return (await op.GetAsync(cancellationToken).ConfigureAwait(false)).Value;
}
catch
{
resource = null;
return false;
return null;
}
}

Expand All @@ -70,8 +90,7 @@ public virtual bool TryGetValue(string resourceName, out ArmResponse<TOperations
/// <returns> Whether or not the resource existed. </returns>
public virtual bool DoesExist(string resourceName)
{
ArmResponse<TOperations> output;
return TryGetValue(resourceName, out output);
return TryGet(resourceName) == null ? false : true;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading.Tasks;
using Azure.Identity;
using Azure.Core.TestFramework;
using NUnit.Framework;

namespace Azure.ResourceManager.Core.Tests
{
public class ContainerTryGetTest : ResourceManagerTestBase
{
private AzureResourceManagerClient _client;
private ResourceGroupContainer _container;
private ResourceGroup _resourceGroup;
private string _rgName;

public ContainerTryGetTest(bool isAsync)
: base(isAsync)//, RecordedTestMode.Record)
{
}

[SetUp]
public void SetUp()
{
_rgName = Recording.GenerateAssetName("CoreRg");
_client = GetArmClient();
_container = _client.DefaultSubscription.GetResourceGroupContainer();
_resourceGroup = _container.Construct(LocationData.WestUS2).CreateOrUpdate(_rgName);
}

[TestCase]
[RecordedTest]
public void TryGetTest()
{
ResourceGroup result = _container.TryGet(_rgName);
Assert.NotNull(result);
Assert.IsTrue(result.Data.Name == _rgName);
result = _container.TryGet("FakeName");
Assert.IsNull(result);
}

[TestCase]
[RecordedTest]
public async Task TryGetAsyncTest()
{
ResourceGroup result = await _container.TryGetAsync(_rgName);
Assert.NotNull(result);
Assert.IsTrue(result.Data.Name == _rgName);
result = await _container.TryGetAsync("FakeName");
Assert.IsNull(result);
}

[TestCase]
public void DoesExistTest()
{
Assert.IsTrue(_container.DoesExist(_rgName));
Assert.IsFalse(_container.DoesExist("FakeName"));
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading