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

add management object group to core #22592

Merged
merged 7 commits into from
Jul 12, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core;
using Azure.Core.Pipeline;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Azure.ResourceManager.TestFramework
{
public class ManagementGroupCleanupPolicy : HttpPipelineSynchronousPolicy
{
private readonly object _listLock = new object();
private Regex _mgmtGroupPattern = new Regex(@"(/providers/Microsoft\.Management/managementGroups/[^?/]+)\?api-version");
private readonly IList<string> _mgmtGroupCreated = new List<string>();

public IList<string> ManagementGroupsCreated
{
get { return _mgmtGroupCreated; }
}

public override void OnSendingRequest(HttpMessage message)
{
if (message.Request.Method == RequestMethod.Put)
{
var match = _mgmtGroupPattern.Match(message.Request.Uri.ToString());
if (match.Success)
{
lock (_listLock)
{
_mgmtGroupCreated.Add(match.Groups[1].Value);
}
}
}
}
}
}
36 changes: 29 additions & 7 deletions common/ManagementTestShared/Redesign/ManagementRecordedTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ namespace Azure.ResourceManager.TestFramework
public abstract class ManagementRecordedTestBase<TEnvironment> : RecordedTestBase<TEnvironment>
where TEnvironment: TestEnvironment, new()
{
protected ResourceGroupCleanupPolicy CleanupPolicy = new ResourceGroupCleanupPolicy();
protected ResourceGroupCleanupPolicy ResourceGroupCleanupPolicy = new ResourceGroupCleanupPolicy();

protected ResourceGroupCleanupPolicy OneTimeCleanupPolicy = new ResourceGroupCleanupPolicy();
protected ResourceGroupCleanupPolicy OneTimeResourceGroupCleanupPolicy = new ResourceGroupCleanupPolicy();

protected ManagementGroupCleanupPolicy ManagementGroupCleanupPolicy = new ManagementGroupCleanupPolicy();

protected ManagementGroupCleanupPolicy OneTimeManagementGroupCleanupPolicy = new ManagementGroupCleanupPolicy();

protected ArmClient GlobalClient { get; private set; }

Expand Down Expand Up @@ -70,7 +74,8 @@ private ArmClient GetCleanupClient()
protected ArmClient GetArmClient(ArmClientOptions clientOptions = default)
{
var options = InstrumentClientOptions(clientOptions ?? new ArmClientOptions());
options.AddPolicy(CleanupPolicy, HttpPipelinePosition.PerCall);
options.AddPolicy(ResourceGroupCleanupPolicy, HttpPipelinePosition.PerCall);
options.AddPolicy(ManagementGroupCleanupPolicy, HttpPipelinePosition.PerCall);

return CreateClient<ArmClient>(
TestEnvironment.SubscriptionId,
Expand All @@ -89,7 +94,7 @@ protected void CleanupResourceGroups()
{
if (Mode != RecordedTestMode.Playback)
{
Parallel.ForEach(CleanupPolicy.ResourceGroupsCreated, resourceGroup =>
Parallel.ForEach(ResourceGroupCleanupPolicy.ResourceGroupsCreated, resourceGroup =>
{
try
{
Expand All @@ -98,7 +103,19 @@ protected void CleanupResourceGroups()
}
catch (RequestFailedException e) when (e.Status == 404)
{
//we assume the test case cleaned up it up if it no longer exists.
//we assume the test case cleaned it up if it no longer exists.
}
});

Parallel.ForEach(ManagementGroupCleanupPolicy.ManagementGroupsCreated, mgmtGroupId =>
{
try
{
_cleanupClient.GetManagementGroupOperations(mgmtGroupId).StartDelete();
}
catch (RequestFailedException e) when (e.Status == 404 || e.Status == 403)
{
//we assume the test case cleaned it up if it no longer exists.
}
});
}
Expand Down Expand Up @@ -138,7 +155,8 @@ public void OneTimeSetUp()
StartSessionRecording();

var options = InstrumentClientOptions(new ArmClientOptions(), SessionRecording);
options.AddPolicy(OneTimeCleanupPolicy, HttpPipelinePosition.PerCall);
options.AddPolicy(OneTimeResourceGroupCleanupPolicy, HttpPipelinePosition.PerCall);
options.AddPolicy(OneTimeManagementGroupCleanupPolicy, HttpPipelinePosition.PerCall);

GlobalClient = CreateClient<ArmClient>(
SessionEnvironment.SubscriptionId,
Expand Down Expand Up @@ -174,11 +192,15 @@ public void OneTimeCleanupResourceGroups()
{
if (Mode != RecordedTestMode.Playback)
{
Parallel.ForEach(OneTimeCleanupPolicy.ResourceGroupsCreated, resourceGroup =>
Parallel.ForEach(OneTimeResourceGroupCleanupPolicy.ResourceGroupsCreated, resourceGroup =>
{
var sub = _cleanupClient.GetSubscriptions().TryGet(SessionEnvironment.SubscriptionId);
sub?.GetResourceGroups().Get(resourceGroup).Value.StartDelete();
});
Parallel.ForEach(OneTimeManagementGroupCleanupPolicy.ManagementGroupsCreated, mgmtGroupId =>
{
_cleanupClient.GetManagementGroupOperations(mgmtGroupId).StartDelete();
});
}

if (!(GlobalClient is null))
Expand Down
16 changes: 14 additions & 2 deletions sdk/resourcemanager/Azure.ResourceManager.Core/src/ArmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -243,5 +242,18 @@ public virtual RestApiContainer GetRestApis(string nameSpace)
/// <param name="cancellationToken"> The cancellation token to use. </param>
/// <exception cref="ArgumentNullException"> <paramref name="resourceProviderNamespace"/> is null. </exception>
public virtual async Task<Response<ProviderInfo>> GetProviderAsync(string resourceProviderNamespace, string expand = null, CancellationToken cancellationToken = default) => await _tenant.GetProviderAsync(resourceProviderNamespace, expand, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Gets the management group container for this tenant.
/// </summary>
/// <returns> A container of the management groups. </returns>
public virtual ManagementGroupContainer GetManagementGroups() => _tenant.GetManagementGroups();

/// <summary>
/// Gets the managmeent group operations object associated with the id.
/// </summary>
/// <param name="id"> The id of the management group operations. </param>
/// <returns> A client to perform operations on the management group. </returns>
public virtual ManagementGroupOperations GetManagementGroupOperations(string id) => _tenant.GetManagementGroupOperations(id);
}
}

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

Loading