Skip to content

Commit 86382dd

Browse files
Create Unregister-AzResourceProvider Cmdlet (Azure#12273)
* Create UnregisterAzureProviderFeatureCmdlet * Add help file and update change log * record test * Update Unregister-AzProviderFeature.md * Update Unregister-AzProviderFeature.md * Fix tags tests Co-authored-by: shenshengkafei <[email protected]>
1 parent 51770dd commit 86382dd

18 files changed

+1259
-733
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
16+
{
17+
using System.Management.Automation;
18+
using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties.Resources;
19+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
20+
21+
/// <summary>
22+
/// Unregister the previewed features of a certain azure resource provider.
23+
/// </summary>
24+
[Cmdlet("Unregister", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "ProviderFeature", SupportsShouldProcess = true), OutputType(typeof(PSProviderFeature))]
25+
public class UnregisterAzureProviderFeatureCmdlet : ProviderFeatureCmdletBase
26+
{
27+
/// <summary>
28+
/// Gets or sets the provider name
29+
/// </summary>
30+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The feature name.")]
31+
[ValidateNotNullOrEmpty]
32+
public string FeatureName { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the provider name
36+
/// </summary>
37+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource provider namespace.")]
38+
[ValidateNotNullOrEmpty]
39+
public string ProviderNamespace { get; set; }
40+
41+
/// <summary>
42+
/// Executes the cmdlet
43+
/// </summary>
44+
public override void ExecuteCmdlet()
45+
{
46+
this.ConfirmAction(
47+
processMessage: ProjectResources.UnregisterProviderMessage,
48+
target: this.ProviderNamespace,
49+
action: () => this.WriteObject(this.ProviderFeatureClient.UnregisterProviderFeature(providerName: this.ProviderNamespace, featureName: this.FeatureName)));
50+
}
51+
}
52+
}

src/Resources/ResourceManager/ResourceManager.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="AutoMapper" Version="6.2.2" />
16-
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="3.7.1-preview" />
16+
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="3.7.3-preview" />
1717
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
1818
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
1919
</ItemGroup>

src/Resources/ResourceManager/SdkClient/ProviderFeatureClient.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ public PSProviderFeature RegisterProviderFeature(string providerName, string fea
145145
return this.FeaturesManagementClient.Features.Register(providerName, featureName).ToPSProviderFeature();
146146
}
147147

148+
/// <summary>
149+
/// Unregisters a feature on the current subscription
150+
/// </summary>
151+
/// <param name="providerName">The name of the resource provider</param>
152+
/// <param name="featureName">The name of the feature</param>
153+
public PSProviderFeature UnregisterProviderFeature(string providerName, string featureName)
154+
{
155+
return this.FeaturesManagementClient.Features.Unregister(providerName, featureName).ToPSProviderFeature();
156+
}
157+
148158
/// <summary>
149159
/// Checks if a feature is registered with the current subscription
150160
/// </summary>
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.Resources.Test
16+
{
17+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation;
18+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
19+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
20+
using Microsoft.Azure.Management.ResourceManager;
21+
using Microsoft.Azure.Management.ResourceManager.Models;
22+
using Microsoft.Rest.Azure;
23+
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
24+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
25+
using Moq;
26+
using System;
27+
using System.Collections.Generic;
28+
using System.Management.Automation;
29+
using System.Threading;
30+
using System.Threading.Tasks;
31+
using WindowsAzure.Commands.Test.Utilities.Common;
32+
using Xunit;
33+
using Xunit.Abstractions;
34+
35+
/// <summary>
36+
/// Tests the Azure Provider Feature cmdlets
37+
/// </summary>
38+
public class UnregisterProviderFeatureCmdletTests : RMTestBase
39+
{
40+
/// <summary>
41+
/// An instance of the cmdlet
42+
/// </summary>
43+
private readonly UnregisterAzureProviderFeatureCmdlet cmdlet;
44+
45+
/// <summary>
46+
/// A mock of the client
47+
/// </summary>
48+
private readonly Mock<IFeaturesOperations> featureOperationsMock;
49+
50+
/// <summary>
51+
/// A mock of the command runtime
52+
/// </summary>
53+
private readonly Mock<ICommandRuntime> commandRuntimeMock;
54+
private MockCommandRuntime mockRuntime;
55+
56+
/// <summary>
57+
/// Initializes a new instance of the <see cref="UnregisterProviderFeatureCmdletTests"/> class.
58+
/// </summary>
59+
public UnregisterProviderFeatureCmdletTests(ITestOutputHelper output)
60+
{
61+
this.featureOperationsMock = new Mock<IFeaturesOperations>();
62+
var featureClient = new Mock<IFeatureClient>();
63+
64+
featureClient
65+
.SetupGet(client => client.Features)
66+
.Returns(() => this.featureOperationsMock.Object);
67+
68+
this.commandRuntimeMock = new Mock<ICommandRuntime>();
69+
70+
this.commandRuntimeMock
71+
.Setup(m => m.ShouldProcess(It.IsAny<string>(), It.IsAny<string>()))
72+
.Returns(() => true);
73+
74+
this.cmdlet = new UnregisterAzureProviderFeatureCmdlet()
75+
{
76+
ProviderFeatureClient = new ProviderFeatureClient
77+
{
78+
FeaturesManagementClient = featureClient.Object
79+
}
80+
};
81+
PSCmdletExtensions.SetCommandRuntimeMock(cmdlet, commandRuntimeMock.Object);
82+
mockRuntime = new MockCommandRuntime();
83+
commandRuntimeMock.Setup(f => f.Host).Returns(mockRuntime.Host);
84+
}
85+
86+
/// <summary>
87+
/// Validates all Unregister-AzureRmResourceProvider scenarios
88+
/// </summary>
89+
[Fact]
90+
[Trait(Category.AcceptanceType, Category.CheckIn)]
91+
public void UnregisterResourceProviderFeatureTests()
92+
{
93+
const string ProviderName = "Providers.Test";
94+
const string FeatureName = "Feature1";
95+
96+
var registeredFeature = new FeatureResult
97+
{
98+
Id = "featureId1",
99+
Name = ProviderName + "/" + FeatureName,
100+
Properties = new FeatureProperties
101+
{
102+
State = ProviderFeatureClient.RegisteredStateName,
103+
},
104+
Type = "Microsoft.Features/feature"
105+
};
106+
107+
this.featureOperationsMock
108+
.Setup(client => client.UnregisterWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), null, It.IsAny<CancellationToken>()))
109+
.Callback((string providerName, string featureName, Dictionary<string, List<string>> customHeaders, CancellationToken ignored) =>
110+
{
111+
Assert.Equal(ProviderName, providerName, StringComparer.OrdinalIgnoreCase);
112+
Assert.Equal(FeatureName, featureName, StringComparer.OrdinalIgnoreCase);
113+
})
114+
.Returns(() => Task.FromResult(new AzureOperationResponse<FeatureResult>()
115+
{
116+
Body = registeredFeature
117+
}));
118+
119+
this.cmdlet.ProviderNamespace = ProviderName;
120+
this.cmdlet.FeatureName = FeatureName;
121+
122+
this.commandRuntimeMock
123+
.Setup(m => m.WriteObject(It.IsAny<object>()))
124+
.Callback((object obj) =>
125+
{
126+
Assert.IsType<PSProviderFeature>(obj);
127+
var feature = (PSProviderFeature)obj;
128+
Assert.Equal(ProviderName, feature.ProviderName, StringComparer.OrdinalIgnoreCase);
129+
Assert.Equal(FeatureName, feature.FeatureName, StringComparer.OrdinalIgnoreCase);
130+
});
131+
132+
this.cmdlet.ExecuteCmdlet();
133+
134+
this.VerifyCallPatternAndReset(succeeded: true);
135+
}
136+
137+
/// <summary>
138+
/// Verifies the right call patterns are made
139+
/// </summary>
140+
private void VerifyCallPatternAndReset(bool succeeded)
141+
{
142+
this.featureOperationsMock.Verify(f => f.UnregisterWithHttpMessagesAsync(It.IsAny<string>(),
143+
It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Once());
144+
this.commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<object>()), succeeded ? Times.Once() : Times.Never());
145+
146+
this.featureOperationsMock.ResetCalls();
147+
this.commandRuntimeMock.ResetCalls();
148+
}
149+
}
150+
}

src/Resources/Resources.Test/Resources.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</ItemGroup>
3232

3333
<ItemGroup>
34-
<PackageReference Update="Microsoft.Azure.Management.ResourceManager" Version="3.7.1-preview" />
34+
<PackageReference Update="Microsoft.Azure.Management.ResourceManager" Version="3.7.3-preview" />
3535
</ItemGroup>
3636
<ItemGroup>
3737
<PackageReference Include="FluentAssertions" Version="5.9.0" />

src/Resources/Resources.Test/ScenarioTests/ProviderFeatureTests.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ function Test-AzureProviderFeature
3434

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

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

3939
Assert-True { $cacheRegisteredFeatures.Length -gt 0 }
40+
41+
Unregister-AzProviderFeature -ProviderName "Microsoft.Cache" -FeatureName "betaAccess3"
42+
43+
$UnregisteredFeatures = Get-AzProviderFeature -ProviderName "Microsoft.Cache" -FeatureName "betaAccess3"
44+
45+
Assert-True { $UnregisteredFeatures.Length -gt 0 }
4046
}

src/Resources/Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ProviderFeatureTests/TestAzureProviderFeature.json

Lines changed: 301 additions & 117 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)