Skip to content

Commit

Permalink
Update arm response types to match current Azure.Core pattern (#20738)
Browse files Browse the repository at this point in the history
* WIP

* wip

* Change the accessbility to virtual for Resource.Id

* update strawman for operation changes

* finalize changes before testing

* update ph overloads

* move pipeline creation into armoperation

* wip

* updates based on new OperationInternals

* address PR comments

* update test classes to use new pattern

* final fixes for arm core tests

* use mock response instead of null

* updates to proto code

* add null check around basetype

Co-authored-by: YalinLi0312 <yall@microsoft.com>
Co-authored-by: m-nash <prognash@microsoft.com>
  • Loading branch information
3 people authored May 5, 2021
1 parent 72930a1 commit a31bb43
Show file tree
Hide file tree
Showing 38 changed files with 835 additions and 532 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Intercept(IInvocation invocation)

if (
// Generated ARM clients will have a property containing the sub-client that ends with Operations.
(invocation.Method.Name.StartsWith("get_") && (type.Name.EndsWith("Operations") || type.BaseType.Name.EndsWith("Operations"))) ||
(invocation.Method.Name.StartsWith("get_") && (type.Name.EndsWith("Operations") || (type.BaseType != null && type.BaseType.Name.EndsWith("Operations")))) ||
// Instrument the container construction methods inside Operations objects
(invocation.Method.Name.StartsWith("Get") && type.Name.EndsWith("Container")) ||
// Instrument the operations construction methods inside Operations objects
Expand Down
1 change: 1 addition & 0 deletions sdk/core/Azure.Core/tests/Azure.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
<DefineConstants>$(DefineConstants);HAS_INTERNALS_VISIBLE_CORE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IncludeGeneratorSharedCode>true</IncludeGeneratorSharedCode>
</PropertyGroup>

<ItemGroup>
Expand Down
47 changes: 22 additions & 25 deletions sdk/core/Azure.Core/tests/TestClients/ArmOperationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,66 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Core;

namespace Azure.Core.Tests
{
public class ArmOperationTest<T> : ArmOperation<T>
where T : class
public class ArmOperationTest : ArmOperation<TestResource>, IOperationSource<TestResource>
{
private T _value;
private TestResource _value;
private bool _exceptionOnWait;
private OperationOrResponseInternals<TestResource> _operationHelper;

protected ArmOperationTest()
{
}

public ArmOperationTest(T value, bool exceptionOnWait = false)
public ArmOperationTest(TestResource value, bool exceptionOnWait = false)
{
_value = value;
_exceptionOnWait = exceptionOnWait;
_operationHelper = new OperationOrResponseInternals<TestResource>(Response.FromValue(value, new MockResponse(200)));
}

public override string Id => "testId";

public override T Value => _value;
public override TestResource Value => _operationHelper.Value;

public override bool HasCompleted => true;
public override bool HasCompleted => _operationHelper.HasCompleted;

public override bool HasValue => true;
public override bool HasValue => _operationHelper.HasValue;

public override Response GetRawResponse()
{
return Response.FromValue(_value, null) as Response;
}
public override Response GetRawResponse() => _operationHelper.GetRawResponse();

public override ValueTask<Response<T>> WaitForCompletionAsync(CancellationToken cancellationToken = default)
public override ValueTask<Response<TestResource>> WaitForCompletionAsync(CancellationToken cancellationToken = default)
{
if (_exceptionOnWait)
throw new ArgumentException("FakeArg");

return new ValueTask<Response<T>>(Response.FromValue(_value, null));
return new ValueTask<Response<TestResource>>(Response.FromValue(_value, new MockResponse(200)));
}

public override ValueTask<Response<T>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken)
public override ValueTask<Response<TestResource>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken)
{
if (_exceptionOnWait)
throw new ArgumentException("FakeArg");

return new ValueTask<Response<T>>(Response.FromValue(_value, null));
return new ValueTask<Response<TestResource>>(Response.FromValue(_value, new MockResponse(200)));
}

public override ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default)
{
if (_exceptionOnWait)
throw new ArgumentException("FakeArg");
public override ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default) => _operationHelper.UpdateStatusAsync(cancellationToken);

return new ValueTask<Response>(Response.FromValue(_value, null) as Response);
}
public override Response UpdateStatus(CancellationToken cancellationToken = default) => _operationHelper.UpdateStatus(cancellationToken);

public override Response UpdateStatus(CancellationToken cancellationToken = default)
public TestResource CreateResult(Response response, CancellationToken cancellationToken)
{
if (_exceptionOnWait)
throw new ArgumentException("FakeArg");
return _value;
}

return Response.FromValue(_value, null) as Response;
public ValueTask<TestResource> CreateResultAsync(Response response, CancellationToken cancellationToken)
{
return new ValueTask<TestResource>(_value);
}
}
}
31 changes: 29 additions & 2 deletions sdk/core/Azure.Core/tests/TestClients/PhArmOperationTest.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Core;

namespace Azure.Core.Tests
{
public class PhArmOperationTest<T> : ArmOperationTest<T>
public class PhArmOperationTest<T> : ArmOperation<T>
where T : class
{
private OperationOrResponseInternals<T> _operationHelper;

public override T Value => _operationHelper.Value;

public override bool HasValue => _operationHelper.HasValue;

public override string Id => "MyId";

public override bool HasCompleted => _operationHelper.HasCompleted;

protected PhArmOperationTest()
{
}

public PhArmOperationTest(T value) : base(value)
public PhArmOperationTest(T value)
{
_operationHelper = new OperationOrResponseInternals<T>(Response.FromValue(value, new MockResponse(200)));
}

public override ValueTask<Response<T>> WaitForCompletionAsync(CancellationToken cancellationToken = default) => _operationHelper.WaitForCompletionAsync(cancellationToken);

public override ValueTask<Response<T>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken) => _operationHelper.WaitForCompletionAsync(pollingInterval, cancellationToken);

public override Response GetRawResponse() => _operationHelper.GetRawResponse();

public override ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default) => _operationHelper.UpdateStatusAsync(cancellationToken);

public override Response UpdateStatus(CancellationToken cancellationToken = default) => _operationHelper.UpdateStatus(cancellationToken);
}
}
19 changes: 10 additions & 9 deletions sdk/core/Azure.Core/tests/TestClients/TestResourceOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.Pipeline;
using Azure.ResourceManager.Core;

namespace Azure.Core.Tests
{
Expand All @@ -17,14 +18,14 @@ public virtual TestResourceOperations GetAnotherOperations()
return new TestResource();
}

public virtual ArmOperationTest<TestResource> GetArmOperation(bool exceptionOnWait = false, CancellationToken cancellationToken = default)
public virtual ArmOperationTest GetArmOperation(bool exceptionOnWait = false, CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetArmOperation");
scope.Start();

try
{
return new ArmOperationTest<TestResource>(new TestResource(), exceptionOnWait);
return new ArmOperationTest(new TestResource(), exceptionOnWait);
}
catch (Exception e)
{
Expand All @@ -33,14 +34,14 @@ public virtual ArmOperationTest<TestResource> GetArmOperation(bool exceptionOnWa
}
}

public virtual Task<ArmOperationTest<TestResource>> GetArmOperationAsync(bool exceptionOnWait = false, CancellationToken cancellationToken = default)
public virtual Task<ArmOperationTest> GetArmOperationAsync(bool exceptionOnWait = false, CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetArmOperation");
scope.Start();

try
{
return Task.FromResult(new ArmOperationTest<TestResource>(new TestResource(), exceptionOnWait));
return Task.FromResult(new ArmOperationTest(new TestResource(), exceptionOnWait));
}
catch (Exception e)
{
Expand Down Expand Up @@ -81,7 +82,7 @@ public virtual Task<ArmResponseTest<TestResource>> GetArmResponseAsync(Cancellat
}
}

public virtual ArmOperationTest<TestResource> GetPhArmOperation(CancellationToken cancellationToken = default)
public virtual ArmOperation<TestResource> GetPhArmOperation(CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetPhArmOperation");
scope.Start();
Expand All @@ -97,14 +98,14 @@ public virtual ArmOperationTest<TestResource> GetPhArmOperation(CancellationToke
}
}

public virtual Task<ArmOperationTest<TestResource>> GetPhArmOperationAsync(CancellationToken cancellationToken = default)
public virtual Task<ArmOperation<TestResource>> GetPhArmOperationAsync(CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetPhArmOperation");
scope.Start();

try
{
return Task.FromResult<ArmOperationTest<TestResource>>(new PhArmOperationTest<TestResource>(new TestResource()));
return Task.FromResult<ArmOperation<TestResource>>(new PhArmOperationTest<TestResource>(new TestResource()));
}
catch (Exception e)
{
Expand Down Expand Up @@ -177,7 +178,7 @@ public virtual Task<ArmResponseTest<TestResource>> GetArmResponseExceptionAsync(
}
}

public virtual ArmOperationTest<TestResource> GetArmOperationException(CancellationToken cancellationToken = default)
public virtual ArmOperationTest GetArmOperationException(CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetArmOperationException");
scope.Start();
Expand All @@ -193,7 +194,7 @@ public virtual ArmOperationTest<TestResource> GetArmOperationException(Cancellat
}
}

public virtual Task<ArmOperationTest<TestResource>> GetArmOperationExceptionAsync(CancellationToken cancellationToken = default)
public virtual Task<ArmOperationTest> GetArmOperationExceptionAsync(CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetArmOperationException");
scope.Start();
Expand Down
83 changes: 0 additions & 83 deletions sdk/resourcemanager/Azure.ResourceManager.Core/src/ArmOperation.cs

This file was deleted.

46 changes: 0 additions & 46 deletions sdk/resourcemanager/Azure.ResourceManager.Core/src/ArmResponse.cs

This file was deleted.

Loading

0 comments on commit a31bb43

Please sign in to comment.