Skip to content

Commit

Permalink
combine resource and resource operations classes (#23205)
Browse files Browse the repository at this point in the history
* update simple resources to no longer have operations
rename ResourceOperations to ArmResource
rename ResourceContainer to ArmContainer

* combine genericResource and genericResourceExpanded
add test case to validate the expand deserializes properly

* combine managementGroupInfo and manageGroup

* update core tests after changes

* update fields to be consistent with autorest

* update after test interceptor changes

* update to take resourceidentifier
differentiate between a tenant provider and a subscription provider
add overloads to get generic resource list to take string and resource identifier

* update api after changes

* add test case to validate bad sub id being passed into armclient

* temporarily use alpha version of core

* remove unused overload

* add recording so test can be played back in ci

* use testenv creds
  • Loading branch information
m-nash authored Aug 11, 2021
1 parent b227271 commit 5f9e928
Show file tree
Hide file tree
Showing 138 changed files with 6,576 additions and 63,602 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected void CleanupResourceGroups()
{
try
{
_cleanupClient.GetManagementGroupOperations(mgmtGroupId).StartDelete();
_cleanupClient.GetManagementGroup(mgmtGroupId).StartDelete();
}
catch (RequestFailedException e) when (e.Status == 404 || e.Status == 403)
{
Expand Down Expand Up @@ -206,7 +206,7 @@ public void OneTimeCleanupResourceGroups()
});
Parallel.ForEach(OneTimeManagementGroupCleanupPolicy.ManagementGroupsCreated, mgmtGroupId =>
{
_cleanupClient.GetManagementGroupOperations(mgmtGroupId).StartDelete();
_cleanupClient.GetManagementGroup(mgmtGroupId).StartDelete();
});
}

Expand Down
2 changes: 1 addition & 1 deletion eng/Directory.Build.Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
</ItemGroup>

<ItemGroup Condition="'$(IsMgmtLibrary)' == 'true'">
<PackageReference Include="Azure.Core" />
<PackageReference Include="Azure.Core" VersionOverride="1.18.0-alpha.20210810.5"/>
<PackageReference Include="System.Text.Json" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void Intercept(IInvocation invocation)
// We don't want to instrument generated rest clients.
if ((type.Name.EndsWith("Client") && !type.Name.EndsWith("RestClient")) ||
// Generated ARM clients will have a property containing the sub-client that ends with Operations.
//TODO: remove after all track2 .net mgmt libraries are updated to the new generation
(invocation.Method.Name.StartsWith("get_") && type.Name.EndsWith("Operations")))
{
if (IsNullResult(invocation))
Expand All @@ -41,11 +42,10 @@ 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 != null && type.BaseType.Name.EndsWith("Operations")))) ||
(invocation.Method.Name.StartsWith("get_") && ManagementInterceptor.InheritsFromArmResource(type)) ||
// 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
(invocation.Method.Name.StartsWith("Get") && type.Name.EndsWith("Operations")))
(invocation.Method.Name.StartsWith("Get") && ManagementInterceptor.InheritsFromArmResource(type)))
{
if (IsNullResult(invocation))
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void Intercept(IInvocation invocation)
}
}
}
else if (invocation.Method.Name.EndsWith("Value") && type.BaseType.Name.EndsWith("Operations"))
else if (invocation.Method.Name.EndsWith("Value") && InheritsFromArmResource(type))
{
invocation.ReturnValue = _testBase.InstrumentClient(type, result, new IInterceptor[] { new ManagementInterceptor(_testBase) });
}
Expand All @@ -72,7 +72,7 @@ public void Intercept(IInvocation invocation)
else if (invocation.Method.Name.StartsWith("Get") &&
invocation.Method.Name.EndsWith("Enumerator") &&
type.IsGenericType &&
InheritsFromOperationBase(type.GetGenericArguments().First()))
InheritsFromArmResource(type.GetGenericArguments().First()))
{
var wrapperType = typeof(AsyncPageableInterceptor<>);
var genericType = wrapperType.MakeGenericType(type.GetGenericArguments()[0]);
Expand All @@ -81,18 +81,18 @@ public void Intercept(IInvocation invocation)
}
}

private bool InheritsFromOperationBase(Type elementType)
internal static bool InheritsFromArmResource(Type elementType)
{
if (elementType.BaseType == null)
return false;

if (elementType.BaseType == typeof(object))
return false;

if (elementType.BaseType.Name == "ResourceOperations")
if (elementType.BaseType.Name == "ArmResource")
return true;

return InheritsFromOperationBase(elementType.BaseType);
return InheritsFromArmResource(elementType.BaseType);
}

private object GetValueFromOther(Type taskResultType, object instrumentedResult)
Expand Down
19 changes: 15 additions & 4 deletions sdk/core/Azure.Core/tests/ManagementPipelineBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@
using System.Threading;
using Azure.Core.Pipeline;
using Azure.Core.TestFramework;
using Azure.Identity;
using Azure.ResourceManager;
using NUnit.Framework;
using static Azure.Core.Tests.Management.ManagementPipelineBuilderTests;

namespace Azure.Core.Tests.Management
{
public class ManagementPipelineBuilderTests
internal class ManagementPipelineBuilderTests : RecordedTestBase<MgmtPipelineTestEnvironment>
{
internal class MgmtPipelineTestEnvironment : TestEnvironment { }

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

[TestCase]
[SyncOnly]
public void AddPerCallPolicy()
{
var options = new ArmClientOptions();
Expand All @@ -27,13 +37,14 @@ public void AddPerCallPolicy()
Assert.IsNotNull(policies.ToArray().FirstOrDefault(p => p.GetType() == typeof(DummyPolicy)));
}

[TestCase]
[RecordedTest]
[SyncOnly]
public void AddPerCallPolicyViaClient()
{
var options = new ArmClientOptions();
var options = InstrumentClientOptions(new ArmClientOptions());
var dummyPolicy = new DummyPolicy();
options.AddPolicy(dummyPolicy, HttpPipelinePosition.PerCall);
var client = new ArmClient(Guid.NewGuid().ToString(), new MockCredential(), options);
var client = InstrumentClient(new ArmClient(TestEnvironment.Credential, options));

var pipelineProperty = client.GetType().GetProperty("Pipeline", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty);
var pipeline = pipelineProperty.GetValue(client) as HttpPipeline;
Expand Down
30 changes: 15 additions & 15 deletions sdk/core/Azure.Core/tests/ManagementRecordedTestBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task ValidateInstrumentArmOperation()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
var sub = client.DefaultSubscription;
var operation = (await sub.GetArmOperationAsync()).Value;
var operation = (await sub.GetLroAsync()).Value;
var result = operation.Method();

Assert.AreEqual("TestResourceProxy", operation.GetType().Name);
Expand All @@ -60,7 +60,7 @@ public async Task ValidateInstrumentArmResponse()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
var sub = client.DefaultSubscription;
var response = (await sub.GetArmOperationAsync()).Value;
var response = (await sub.GetLroAsync()).Value;
var result = response.Method();

Assert.AreEqual("TestResourceProxy", response.GetType().Name);
Expand All @@ -83,9 +83,9 @@ public void ValidateInstrumentGetContainer()
public void ValidateInstrumentGetOperations()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
var testResource = client.GetTestResourceOperations();
var testResource = client.GetTestResource();

Assert.AreEqual("TestResourceOperationsProxy", testResource.GetType().Name);
Assert.AreEqual("TestResourceProxy", testResource.GetType().Name);
Assert.AreEqual("success", testResource.Method());
}

Expand All @@ -105,8 +105,8 @@ public async Task ValidateInstrumentPageable()
public async Task ValidateWaitForCompletion()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
var testResourceOp = await rgOp.GetArmOperationAsync();
TestResource rgOp = client.GetTestResource();
var testResourceOp = await rgOp.GetLroAsync();
TestResource testResource = await testResourceOp.WaitForCompletionAsync();
Assert.AreEqual("TestResourceProxy", testResource.GetType().Name);
Assert.AreEqual("success", testResource.Method());
Expand All @@ -116,32 +116,32 @@ public async Task ValidateWaitForCompletion()
public void ValidateExceptionResponse()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
TestResource rgOp = client.GetTestResource();
Assert.ThrowsAsync(typeof(ArgumentException), async () => await rgOp.GetResponseExceptionAsync());
}

[Test]
public void ValidateExceptionOperation()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
Assert.ThrowsAsync(typeof(ArgumentException), async () => await rgOp.GetArmOperationExceptionAsync());
TestResource rgOp = client.GetTestResource();
Assert.ThrowsAsync(typeof(ArgumentException), async () => await rgOp.GetLroExceptionAsync());
}

[Test]
public async Task ValidateExceptionOperationWaitForCompletion()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
var testResourceOp = await rgOp.GetArmOperationAsync(true);
TestResource rgOp = client.GetTestResource();
var testResourceOp = await rgOp.GetLroAsync(true);
Assert.ThrowsAsync(typeof(ArgumentException), async () => await testResourceOp.WaitForCompletionAsync());
}

[Test]
public async Task ValidateLroWrapper()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
TestResource rgOp = client.GetTestResource();
TestResource testResource = await rgOp.LroWrapperAsync();
Assert.AreEqual("TestResourceProxy", testResource.GetType().Name);
Assert.AreEqual("success", testResource.Method());
Expand All @@ -151,7 +151,7 @@ public async Task ValidateLroWrapper()
public async Task ValidateStartLroWrapper()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
TestResource rgOp = client.GetTestResource();
var testResourceOp = await rgOp.StartLroWrapperAsync();
TestResource testResource = await testResourceOp.WaitForCompletionAsync();
Assert.AreEqual("TestResourceProxy", testResource.GetType().Name);
Expand All @@ -162,7 +162,7 @@ public async Task ValidateStartLroWrapper()
public async Task ValidateSkipWait()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
TestResource rgOp = client.GetTestResource();
Stopwatch timer = Stopwatch.StartNew();
TestResource testResource = await rgOp.LroWrapperAsync();
timer.Stop();
Expand All @@ -174,7 +174,7 @@ public async Task ValidateSkipWait()
public async Task ValidateStartSkipWait()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
TestResource rgOp = client.GetTestResource();
var testResourceOp = await rgOp.StartLroWrapperAsync();
Stopwatch timer = Stopwatch.StartNew();
TestResource testResource = await testResourceOp.WaitForCompletionAsync();
Expand Down

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

4 changes: 2 additions & 2 deletions sdk/core/Azure.Core/tests/TestClients/ManagementTestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public ManagementTestClient(TestClientOptions options)
_diagnostics = new ClientDiagnostics(options);
}

public virtual TestResourceOperations GetTestResourceOperations()
public virtual TestResource GetTestResource()
{
return new TestResourceOperations();
return new TestResource();
}

public virtual TestResourceContainer GetTestResourceContainer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

namespace Azure.Core.Tests
{
public class ArmOperationTest : Operation<TestResource>, IOperationSource<TestResource>
public class TestLroOperation : Operation<TestResource>, IOperationSource<TestResource>
{
private TestResource _value;
private bool _exceptionOnWait;
private OperationOrResponseInternals<TestResource> _operationHelper;
private int _delaySteps = 0;

protected ArmOperationTest()
protected TestLroOperation()
{
}

public ArmOperationTest(TestResource value, bool exceptionOnWait = false, int delaySteps = 0)
public TestLroOperation(TestResource value, bool exceptionOnWait = false, int delaySteps = 0)
{
_value = value;
_exceptionOnWait = exceptionOnWait;
Expand Down
Loading

0 comments on commit 5f9e928

Please sign in to comment.