Skip to content

Commit 678d1c7

Browse files
committed
Merge pull request #1039 from chidmdxx/ResourceGroupFixes
Update the AzureRmResourceGroup cmdlets parameters
2 parents 5599724 + de84df5 commit 678d1c7

11 files changed

+126
-97
lines changed

src/ResourceManager/Resources/Commands.Resources.Test/ResourceGroups/GetAzureResourceGroupCommandTests.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class GetAzureResourceGroupCommandTests : RMTestBase
3131
private Mock<ICommandRuntime> commandRuntimeMock;
3232

3333
private string resourceGroupName = "myResourceGroup";
34+
private string resourceGroupId = "/subscriptions/subId/resourceGroups/myResourceGroup";
3435

3536
private string resourceGroupLocation = "West US";
3637

@@ -57,7 +58,7 @@ public void GetsResourcesGroups()
5758
Resources = new List<PSResource>() { new PSResource() { Name = "resource1" } }
5859
};
5960
result.Add(expected);
60-
resourcesClientMock.Setup(f => f.FilterResourceGroups(resourceGroupName, null, true)).Returns(result);
61+
resourcesClientMock.Setup(f => f.FilterResourceGroups(resourceGroupName, null, false, null)).Returns(result);
6162

6263
cmdlet.Name = resourceGroupName;
6364

@@ -70,5 +71,29 @@ public void GetsResourcesGroups()
7071

7172
commandRuntimeMock.Verify(f => f.WriteObject(result, true), Times.Once());
7273
}
74+
75+
[Fact]
76+
[Trait(Category.AcceptanceType, Category.CheckIn)]
77+
public void GetsResourcesGroupsById()
78+
{
79+
List<PSResourceGroup> result = new List<PSResourceGroup>();
80+
PSResourceGroup expected = new PSResourceGroup()
81+
{
82+
Location = resourceGroupLocation,
83+
ResourceGroupName = resourceGroupName,
84+
Resources = new List<PSResource>() { new PSResource() { Name = "resource1" } }
85+
};
86+
result.Add(expected);
87+
resourcesClientMock.Setup(f => f.FilterResourceGroups(null, null, true, null)).Returns(result);
88+
89+
cmdlet.Id = resourceGroupId;
90+
91+
cmdlet.ExecuteCmdlet();
92+
93+
Assert.Equal(1, result.Count);
94+
Assert.Equal(resourceGroupName, result[0].ResourceGroupName);
95+
Assert.Equal(resourceGroupLocation, result[0].Location);
96+
Assert.Equal(1, result[0].Resources.Count);
97+
}
7398
}
7499
}

src/ResourceManager/Resources/Commands.Resources.Test/ResourceGroups/NewAzureResourceGroupCommandTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public NewAzureResourceGroupCommandTests()
6565

6666
[Fact]
6767
[Trait(Category.AcceptanceType, Category.CheckIn)]
68-
public void CreatesNewPSResourceGroupWithUserTemplate()
68+
public void CreatesNewPSResourceGroup()
6969
{
7070
CreatePSResourceGroupParameters expectedParameters = new CreatePSResourceGroupParameters()
7171
{
@@ -89,17 +89,12 @@ public void CreatesNewPSResourceGroupWithUserTemplate()
8989

9090
cmdlet.Name = expectedParameters.ResourceGroupName;
9191
cmdlet.Location = expectedParameters.Location;
92-
cmdlet.TemplateFile = expectedParameters.TemplateFile;
93-
cmdlet.DeploymentName = expectedParameters.DeploymentName;
9492
cmdlet.Tag = expectedParameters.Tag;
9593

9694
cmdlet.ExecuteCmdlet();
9795

9896
Assert.Equal(expectedParameters.ResourceGroupName, actualParameters.ResourceGroupName);
9997
Assert.Equal(expectedParameters.Location, actualParameters.Location);
100-
Assert.Equal(expectedParameters.DeploymentName, actualParameters.DeploymentName);
101-
Assert.Equal(expectedParameters.TemplateFile, actualParameters.TemplateFile);
102-
Assert.NotNull(actualParameters.TemplateParameterObject);
10398
Assert.Equal(expectedParameters.Tag, actualParameters.Tag);
10499

105100
commandRuntimeMock.Verify(f => f.WriteObject(expected), Times.Once());

src/ResourceManager/Resources/Commands.Resources.Test/ResourceGroups/RemoveAzureResourceGroupCommandTests.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class RemoveAzureResourceGroupCommandTests : RMTestBase
3030
private Mock<ICommandRuntime> commandRuntimeMock;
3131

3232
private string resourceGroupName = "myResourceGroup";
33+
private string resourceGroupId = "/subscriptions/subId/resourceGroups/myResourceGroup";
3334

3435
public RemoveAzureResourceGroupCommandTests()
3536
{
@@ -50,13 +51,26 @@ public void RemovesResourceGroup()
5051
resourcesClientMock.Setup(f => f.DeleteResourceGroup(resourceGroupName));
5152

5253
cmdlet.Name = resourceGroupName;
53-
cmdlet.PassThru = true;
5454
cmdlet.Force = true;
5555

5656
cmdlet.ExecuteCmdlet();
5757

5858
resourcesClientMock.Verify(f => f.DeleteResourceGroup(resourceGroupName), Times.Once());
59-
commandRuntimeMock.Verify(f => f.WriteObject(true), Times.Once());
59+
}
60+
61+
[Fact]
62+
[Trait(Category.AcceptanceType, Category.CheckIn)]
63+
public void RemovesResourceGroupFromId()
64+
{
65+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
66+
resourcesClientMock.Setup(f => f.DeleteResourceGroup(resourceGroupName));
67+
68+
cmdlet.Id = resourceGroupId;
69+
cmdlet.Force = true;
70+
71+
cmdlet.ExecuteCmdlet();
72+
73+
resourcesClientMock.Verify(f => f.DeleteResourceGroup(resourceGroupName), Times.Once());
6074
}
6175
}
6276
}

src/ResourceManager/Resources/Commands.Resources.Test/ResourceGroups/SetAzureResourceGroupCommandTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class SetAzureResourceGroupCommandTests : RMTestBase
3333
private Mock<ICommandRuntime> commandRuntimeMock;
3434

3535
private string resourceGroupName = "myResourceGroup";
36+
private string resourceGroupId = "/subscriptions/subId/resourceGroups/myResourceGroup";
3637

3738
private List<Hashtable> tags;
3839

@@ -83,5 +84,36 @@ public void UpdatesSetPSResourceGroupWithTag()
8384

8485
commandRuntimeMock.Verify(f => f.WriteObject(expected), Times.Once());
8586
}
87+
88+
[Fact]
89+
[Trait(Category.AcceptanceType, Category.CheckIn)]
90+
public void UpdatesSetPSResourceGroupWithTagFromId()
91+
{
92+
UpdatePSResourceGroupParameters expectedParameters = new UpdatePSResourceGroupParameters()
93+
{
94+
ResourceGroupName = resourceGroupName,
95+
Tag = tags.ToArray()
96+
};
97+
UpdatePSResourceGroupParameters actualParameters = new UpdatePSResourceGroupParameters();
98+
PSResourceGroup expected = new PSResourceGroup()
99+
{
100+
ResourceGroupName = expectedParameters.ResourceGroupName,
101+
Resources = new List<PSResource>() { new PSResource() { Name = "resource1" } },
102+
Tags = expectedParameters.Tag
103+
};
104+
resourcesClientMock.Setup(f => f.UpdatePSResourceGroup(It.IsAny<UpdatePSResourceGroupParameters>()))
105+
.Returns(expected)
106+
.Callback((UpdatePSResourceGroupParameters p) => { actualParameters = p; });
107+
108+
cmdlet.Id = resourceGroupId;
109+
cmdlet.Tag = expectedParameters.Tag;
110+
111+
cmdlet.ExecuteCmdlet();
112+
113+
Assert.Equal(expectedParameters.ResourceGroupName, actualParameters.ResourceGroupName);
114+
Assert.Equal(expectedParameters.Tag, actualParameters.Tag);
115+
116+
commandRuntimeMock.Verify(f => f.WriteObject(expected), Times.Once());
117+
}
86118
}
87119
}

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/ResourceGroupTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@ public void TestNewDeploymentAndProviderRegistration()
7575
ResourcesController.NewInstance.RunPsTest("Test-NewDeploymentAndProviderRegistration");
7676
}
7777

78-
[Fact]
79-
public void TestNewResourceGroupWithTemplate()
80-
{
81-
ResourcesController.NewInstance.RunPsTest("Test-NewResourceGroupWithTemplateThenGetWithAndWithoutDetails");
82-
}
83-
8478
[Fact]
8579
public void TestRemoveDeployment()
8680
{

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/ResourceGroupTests.ps1

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -251,42 +251,4 @@ function Test-RemoveDeployment
251251
# Cleanup
252252
Clean-ResourceGroup $rgName
253253
}
254-
}
255-
256-
function Test-NewResourceGroupWithTemplateThenGetWithAndWithoutDetails
257-
{
258-
# Setup
259-
$rgname = Get-ResourceGroupName
260-
$websiteName = Get-ResourceName
261-
$location = Get-ProviderLocation ResourceManagement
262-
$templateFile = "Resources\EmptyWebsiteTemplate.json"
263-
264-
try
265-
{
266-
# Test
267-
$actual = New-AzureRmResourceGroup -Name $rgname -Location $location -TemplateFile $templateFile `
268-
-siteName $websiteName -hostingPlanName "test" -siteLocation "West US" `
269-
-Tag @{ Name = "testtag"; Value = "testval" }
270-
271-
$expected1 = Get-AzureRmResourceGroup -Name $rgname
272-
# Assert
273-
Assert-AreEqual $expected1.ResourceGroupName $actual.ResourceGroupName
274-
Assert-AreEqual $expected1.Tags[0]["Name"] $actual.Tags[0]["Name"]
275-
Assert-AreEqual $expected1.Resources.Count 2
276-
277-
$expected2 = Get-AzureRmResourceGroup
278-
# Assert
279-
Assert-AreEqual $expected2[0].Resources.Count 0
280-
281-
$expected3 = Get-AzureRmResourceGroup -Detailed
282-
$names = $expected3 | Select-Object -ExpandProperty ResourceGroupName
283-
$index = [Array]::IndexOf($names, $expected1.ResourceGroupName)
284-
# Assert
285-
Assert-AreEqual $expected3[$index].Resources.Count 2
286-
}
287-
finally
288-
{
289-
# Cleanup
290-
Clean-ResourceGroup $rgname
291-
}
292254
}

src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/ResourceClient.ResourceManager.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ private string GenerateDeploymentName(CreatePSResourceGroupDeploymentParameters
342342
/// <param name="name">The resource group name.</param>
343343
/// <param name="tag">The resource group tag.</param>
344344
/// <param name="detailed">Whether the return is detailed or not.</param>
345+
/// <param name="location">The resource group location.</param>
345346
/// <returns>The filtered resource groups</returns>
346-
public virtual List<PSResourceGroup> FilterResourceGroups(string name, Hashtable tag, bool detailed)
347+
public virtual List<PSResourceGroup> FilterResourceGroups(string name, Hashtable tag, bool detailed, string location = null)
347348
{
348349
List<PSResourceGroup> result = new List<PSResourceGroup>();
350+
349351
if (string.IsNullOrEmpty(name))
350352
{
351353
var response = ResourceManagementClient.ResourceGroups.List(null);
@@ -356,6 +358,10 @@ public virtual List<PSResourceGroup> FilterResourceGroups(string name, Hashtable
356358
resourceGroups.AddRange(response.ResourceGroups);
357359
}
358360

361+
resourceGroups = !string.IsNullOrEmpty(location)
362+
? resourceGroups.Where(resourceGroup => this.NormalizeLetterOrDigitToUpperInvariant(resourceGroup.Location).Equals(this.NormalizeLetterOrDigitToUpperInvariant(location))).ToList()
363+
: resourceGroups;
364+
359365
// TODO: Replace with server side filtering when available
360366
if (tag != null && tag.Count >= 1)
361367
{

src/ResourceManager/Resources/Commands.Resources/ResourceGroups/GetAzureResourceGroupCommand.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,43 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Commands.Resources.Models;
16-
using System.Collections;
1716
using System.Collections.Generic;
1817
using System.Management.Automation;
1918

2019
namespace Microsoft.Azure.Commands.Resources
2120
{
21+
using System.Linq;
22+
2223
/// <summary>
2324
/// Filters resource groups.
2425
/// </summary>
2526
[Cmdlet(VerbsCommon.Get, "AzureRmResourceGroup"), OutputType(typeof(List<PSResourceGroup>))]
2627
public class GetAzureResourceGroupCommand : ResourcesBaseCmdlet
2728
{
2829
[Alias("ResourceGroupName")]
29-
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "GetSingle")]
30+
[Parameter(Position = 0, Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = "GetSingle")]
3031
[ValidateNotNullOrEmpty]
3132
public string Name { get; set; }
3233

33-
[Parameter(Position = 0, Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = "GetMultiple")]
34-
public Hashtable Tag { get; set; }
34+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group location.")]
35+
[ValidateNotNullOrEmpty]
36+
public string Location { get; set; }
3537

36-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = "GetMultiple")]
37-
public SwitchParameter Detailed { get; set; }
38+
[Alias("ResourceGroupId", "ResourceId")]
39+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group Id.")]
40+
[ValidateNotNullOrEmpty]
41+
public string Id { get; set; }
3842

3943
protected override void ProcessRecord()
4044
{
41-
if(this.Tag != null)
42-
{
43-
WriteWarning("The Tag parameter is being deprecated and will be removed in a future release.");
44-
}
45-
if(this.Detailed.IsPresent)
46-
{
47-
WriteWarning("The Detailed switch parameter is being deprecated and will be removed in a future release.");
48-
}
4945
WriteWarning("The output object of this cmdlet will be modified in a future release.");
50-
var detailed = Detailed.IsPresent || !string.IsNullOrEmpty(Name);
51-
WriteObject(ResourcesClient.FilterResourceGroups(Name, Tag, detailed), true);
46+
Name = string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Id)
47+
? Id.Split('/').Last()
48+
: Name;
49+
50+
this.WriteObject(
51+
ResourcesClient.FilterResourceGroups(name: this.Name, tag: null, detailed: false, location: this.Location),
52+
true);
5253
}
5354
}
5455
}

src/ResourceManager/Resources/Commands.Resources/ResourceGroups/NewAzureResourceGroupCommand.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
using System.Collections;
1616
using System.Management.Automation;
1717
using Microsoft.Azure.Commands.Resources.Models;
18-
using Microsoft.WindowsAzure.Commands.Utilities.Common;
18+
using Microsoft.Azure.Commands.Resources.Models.ActiveDirectory;
1919

2020
namespace Microsoft.Azure.Commands.Resources
2121
{
22+
2223
/// <summary>
2324
/// Creates a new resource group.
2425
/// </summary>
25-
[Cmdlet(VerbsCommon.New, "AzureRmResourceGroup", DefaultParameterSetName = BaseParameterSetName), OutputType(typeof(PSResourceGroup))]
26-
public class NewAzureResourceGroupCommand : ResourceWithParameterBaseCmdlet, IDynamicParameters
26+
[Cmdlet(VerbsCommon.New, "AzureRmResourceGroup", DefaultParameterSetName = ParameterSet.Empty), OutputType(typeof(PSResourceGroup))]
27+
public class NewAzureResourceGroupCommand : ResourcesBaseCmdlet
2728
{
2829
[Alias("ResourceGroupName")]
2930
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")]
@@ -34,11 +35,6 @@ public class NewAzureResourceGroupCommand : ResourceWithParameterBaseCmdlet, IDy
3435
[ValidateNotNullOrEmpty]
3536
public string Location { get; set; }
3637

37-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true,
38-
HelpMessage = "The name of the deployment it's going to create. Only valid when a template is used. When a template is used, if the user doesn't specify a deployment name, use the current time, like \"20131223140835\".")]
39-
[ValidateNotNullOrEmpty]
40-
public string DeploymentName { get; set; }
41-
4238
[Alias("Tags")]
4339
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "An array of hashtables which represents resource tags.")]
4440
public Hashtable[] Tag { get; set; }
@@ -52,18 +48,11 @@ protected override void ProcessRecord()
5248
{
5349
ResourceGroupName = Name,
5450
Location = Location,
55-
DeploymentName = DeploymentName,
56-
TemplateFile = TemplateUri ?? this.TryResolvePath(TemplateFile),
57-
TemplateParameterObject = GetTemplateParameterObject(TemplateParameterObject),
5851
Force = Force.IsPresent,
5952
Tag = Tag,
6053
ConfirmAction = ConfirmAction
6154
};
62-
if(!string.IsNullOrEmpty(DeploymentName) || !string.IsNullOrEmpty(TemplateFile)
63-
|| TemplateParameterObject != null)
64-
{
65-
WriteWarning("The deployment parameters in New-AzureRmResourceGroup cmdlet is being deprecated and will be removed in a future release. Please use New-AzureRmResourceGroupDeployment to submit deployments.");
66-
}
55+
6756
WriteWarning("The output object of this cmdlet will be modified in a future release.");
6857
WriteObject(ResourcesClient.CreatePSResourceGroup(parameters));
6958
}

src/ResourceManager/Resources/Commands.Resources/ResourceGroups/RemoveAzureResourceGroupCommand.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace Microsoft.Azure.Commands.Resources
2020
{
21+
using System.Linq;
22+
2123
/// <summary>
2224
/// Removes a new resource group.
2325
/// </summary>
@@ -29,26 +31,26 @@ public class RemoveAzureResourceGroupCommand : ResourcesBaseCmdlet
2931
[ValidateNotNullOrEmpty]
3032
public string Name {get; set;}
3133

34+
[Alias("ResourceGroupId", "ResourceId")]
35+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource group Id.")]
36+
[ValidateNotNullOrEmpty]
37+
public string Id { get; set; }
38+
3239
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
3340
public SwitchParameter Force { get; set; }
34-
35-
[Parameter(Mandatory = false)]
36-
public SwitchParameter PassThru { get; set; }
3741

3842
protected override void ProcessRecord()
3943
{
44+
Name = string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Id)
45+
? Id.Split('/').Last()
46+
: Name;
47+
4048
ConfirmAction(
4149
Force.IsPresent,
4250
string.Format(ProjectResources.RemovingResourceGroup, Name),
4351
ProjectResources.RemoveResourceGroupMessage,
4452
Name,
4553
() => ResourcesClient.DeleteResourceGroup(Name));
46-
47-
if (PassThru)
48-
{
49-
WriteWarning("The PassThru switch parameter is being deprecated and will be removed in a future release.");
50-
WriteObject(true);
51-
}
5254
}
5355
}
5456
}

0 commit comments

Comments
 (0)