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
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