Skip to content

Create Unregister-AzResourceProvider Cmdlet #12273

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

Merged
merged 7 commits into from
Jul 2, 2020
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,52 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
using System.Management.Automation;
using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties.Resources;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;

/// <summary>
/// Unregister the previewed features of a certain azure resource provider.
/// </summary>
[Cmdlet("Unregister", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "ProviderFeature", SupportsShouldProcess = true), OutputType(typeof(PSProviderFeature))]
public class UnregisterAzureProviderFeatureCmdlet : ProviderFeatureCmdletBase
{
/// <summary>
/// Gets or sets the provider name
/// </summary>
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The feature name.")]
[ValidateNotNullOrEmpty]
public string FeatureName { get; set; }

/// <summary>
/// Gets or sets the provider name
/// </summary>
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource provider namespace.")]
[ValidateNotNullOrEmpty]
public string ProviderNamespace { get; set; }

/// <summary>
/// Executes the cmdlet
/// </summary>
public override void ExecuteCmdlet()
{
this.ConfirmAction(
processMessage: ProjectResources.UnregisterProviderMessage,
target: this.ProviderNamespace,
action: () => this.WriteObject(this.ProviderFeatureClient.UnregisterProviderFeature(providerName: this.ProviderNamespace, featureName: this.FeatureName)));
}
}
}
2 changes: 1 addition & 1 deletion src/Resources/ResourceManager/ResourceManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<PackageReference Include="AutoMapper" Version="6.2.2" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="3.7.1-preview" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="3.7.3-preview" />
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions src/Resources/ResourceManager/SdkClient/ProviderFeatureClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ public PSProviderFeature RegisterProviderFeature(string providerName, string fea
return this.FeaturesManagementClient.Features.Register(providerName, featureName).ToPSProviderFeature();
}

/// <summary>
/// Unregisters a feature on the current subscription
/// </summary>
/// <param name="providerName">The name of the resource provider</param>
/// <param name="featureName">The name of the feature</param>
public PSProviderFeature UnregisterProviderFeature(string providerName, string featureName)
{
return this.FeaturesManagementClient.Features.Unregister(providerName, featureName).ToPSProviderFeature();
}

/// <summary>
/// Checks if a feature is registered with the current subscription
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Resources.Test
{
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Rest.Azure;
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using WindowsAzure.Commands.Test.Utilities.Common;
using Xunit;
using Xunit.Abstractions;

/// <summary>
/// Tests the Azure Provider Feature cmdlets
/// </summary>
public class UnregisterProviderFeatureCmdletTests : RMTestBase
{
/// <summary>
/// An instance of the cmdlet
/// </summary>
private readonly UnregisterAzureProviderFeatureCmdlet cmdlet;

/// <summary>
/// A mock of the client
/// </summary>
private readonly Mock<IFeaturesOperations> featureOperationsMock;

/// <summary>
/// A mock of the command runtime
/// </summary>
private readonly Mock<ICommandRuntime> commandRuntimeMock;
private MockCommandRuntime mockRuntime;

/// <summary>
/// Initializes a new instance of the <see cref="UnregisterProviderFeatureCmdletTests"/> class.
/// </summary>
public UnregisterProviderFeatureCmdletTests(ITestOutputHelper output)
{
this.featureOperationsMock = new Mock<IFeaturesOperations>();
var featureClient = new Mock<IFeatureClient>();

featureClient
.SetupGet(client => client.Features)
.Returns(() => this.featureOperationsMock.Object);

this.commandRuntimeMock = new Mock<ICommandRuntime>();

this.commandRuntimeMock
.Setup(m => m.ShouldProcess(It.IsAny<string>(), It.IsAny<string>()))
.Returns(() => true);

this.cmdlet = new UnregisterAzureProviderFeatureCmdlet()
{
ProviderFeatureClient = new ProviderFeatureClient
{
FeaturesManagementClient = featureClient.Object
}
};
PSCmdletExtensions.SetCommandRuntimeMock(cmdlet, commandRuntimeMock.Object);
mockRuntime = new MockCommandRuntime();
commandRuntimeMock.Setup(f => f.Host).Returns(mockRuntime.Host);
}

/// <summary>
/// Validates all Unregister-AzureRmResourceProvider scenarios
/// </summary>
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void UnregisterResourceProviderFeatureTests()
{
const string ProviderName = "Providers.Test";
const string FeatureName = "Feature1";

var registeredFeature = new FeatureResult
{
Id = "featureId1",
Name = ProviderName + "/" + FeatureName,
Properties = new FeatureProperties
{
State = ProviderFeatureClient.RegisteredStateName,
},
Type = "Microsoft.Features/feature"
};

this.featureOperationsMock
.Setup(client => client.UnregisterWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), null, It.IsAny<CancellationToken>()))
.Callback((string providerName, string featureName, Dictionary<string, List<string>> customHeaders, CancellationToken ignored) =>
{
Assert.Equal(ProviderName, providerName, StringComparer.OrdinalIgnoreCase);
Assert.Equal(FeatureName, featureName, StringComparer.OrdinalIgnoreCase);
})
.Returns(() => Task.FromResult(new AzureOperationResponse<FeatureResult>()
{
Body = registeredFeature
}));

this.cmdlet.ProviderNamespace = ProviderName;
this.cmdlet.FeatureName = FeatureName;

this.commandRuntimeMock
.Setup(m => m.WriteObject(It.IsAny<object>()))
.Callback((object obj) =>
{
Assert.IsType<PSProviderFeature>(obj);
var feature = (PSProviderFeature)obj;
Assert.Equal(ProviderName, feature.ProviderName, StringComparer.OrdinalIgnoreCase);
Assert.Equal(FeatureName, feature.FeatureName, StringComparer.OrdinalIgnoreCase);
});

this.cmdlet.ExecuteCmdlet();

this.VerifyCallPatternAndReset(succeeded: true);
}

/// <summary>
/// Verifies the right call patterns are made
/// </summary>
private void VerifyCallPatternAndReset(bool succeeded)
{
this.featureOperationsMock.Verify(f => f.UnregisterWithHttpMessagesAsync(It.IsAny<string>(),
It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Once());
this.commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<object>()), succeeded ? Times.Once() : Times.Never());

this.featureOperationsMock.ResetCalls();
this.commandRuntimeMock.ResetCalls();
}
}
}
2 changes: 1 addition & 1 deletion src/Resources/Resources.Test/Resources.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Update="Microsoft.Azure.Management.ResourceManager" Version="3.7.1-preview" />
<PackageReference Update="Microsoft.Azure.Management.ResourceManager" Version="3.7.3-preview" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.9.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ function Test-AzureProviderFeature

Register-AzProviderFeature -ProviderName "Microsoft.Cache" -FeatureName "betaAccess3"

$cacheRegisteredFeatures = Get-AzProviderFeature -ProviderName "Microsoft.Cache"
$cacheRegisteredFeatures = Get-AzProviderFeature -ProviderName "Microsoft.Cache" -FeatureName "betaAccess3"

Assert-True { $cacheRegisteredFeatures.Length -gt 0 }

Unregister-AzProviderFeature -ProviderName "Microsoft.Cache" -FeatureName "betaAccess3"

$UnregisteredFeatures = Get-AzProviderFeature -ProviderName "Microsoft.Cache" -FeatureName "betaAccess3"

Assert-True { $UnregisteredFeatures.Length -gt 0 }
}

Large diffs are not rendered by default.

Loading