Skip to content

Commit a5ce138

Browse files
committed
Merge pull request #1054 from chidmdxx/AddParameterSetGetResourceGroup
Add parameter set AzureRmResourceGroup
2 parents f91e06c + e33f84d commit a5ce138

File tree

7 files changed

+76
-23
lines changed

7 files changed

+76
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function Test-RemoveNonExistingResourceGroup
131131
# Setup
132132
$rgname = Get-ResourceGroupName
133133

134-
Assert-Throws { Remove-AzureRmResourceGroup $rgname -Force } "Provided resource group does not exist."
134+
Assert-Throws { Remove-AzureRmResourceGroup -Name $rgname -Force } "Provided resource group does not exist."
135135
}
136136

137137
<#

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ public ResourceIdentifier(string idFromServer)
7474
}
7575
}
7676

77+
public static ResourceIdentifier FromResourceGroupIdentifier(string resourceGroupId)
78+
{
79+
if (!string.IsNullOrEmpty(resourceGroupId))
80+
{
81+
string[] tokens = resourceGroupId.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
82+
if (tokens.Length < 4)
83+
{
84+
throw new ArgumentException(ProjectResources.InvalidFormatOfResourceGroupId, "resourceGroupId");
85+
}
86+
return new ResourceIdentifier
87+
{
88+
Subscription = tokens[1],
89+
ResourceGroupName = tokens[3],
90+
};
91+
}
92+
93+
return new ResourceIdentifier();
94+
}
95+
7796
public static string GetProviderFromResourceType(string resourceType)
7897
{
7998
if (resourceType == null)

src/ResourceManager/Resources/Commands.Resources/Properties/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Resources/Commands.Resources/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@
198198
<data name="InvalidFormatOfResourceId" xml:space="preserve">
199199
<value>Invalid format of the resource identifier.</value>
200200
</data>
201+
<data name="InvalidFormatOfResourceGroupId" xml:space="preserve">
202+
<value>Invalid format of the resource group identifier. Expected 'subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}'.</value>
203+
</data>
201204
<data name="InvalidTagFormat" xml:space="preserve">
202205
<value>Invalid tag format. Expect @{Name = "tagName"} or @{Name = "tagName"; Value = "tagValue"}</value>
203206
</data>

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,24 @@
1818

1919
namespace Microsoft.Azure.Commands.Resources
2020
{
21-
using System.Linq;
22-
2321
/// <summary>
2422
/// Filters resource groups.
2523
/// </summary>
26-
[Cmdlet(VerbsCommon.Get, "AzureRmResourceGroup"), OutputType(typeof(List<PSResourceGroup>))]
24+
[Cmdlet(VerbsCommon.Get, "AzureRmResourceGroup", DefaultParameterSetName = ResourceGroupNameParameterSet), OutputType(typeof(List<PSResourceGroup>))]
2725
public class GetAzureResourceGroupCommand : ResourcesBaseCmdlet
2826
{
27+
/// <summary>
28+
/// List resources group by name parameter set.
29+
/// </summary>
30+
internal const string ResourceGroupNameParameterSet = "Lists the resource group based in the name.";
31+
32+
/// <summary>
33+
/// List resources group by Id parameter set.
34+
/// </summary>
35+
internal const string ResourceGroupIdParameterSet = "Lists the resource group based in the Id.";
36+
2937
[Alias("ResourceGroupName")]
30-
[Parameter(Position = 0, Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = "GetSingle")]
38+
[Parameter(Mandatory = false, ParameterSetName = ResourceGroupNameParameterSet, ValueFromPipelineByPropertyName = true)]
3139
[ValidateNotNullOrEmpty]
3240
public string Name { get; set; }
3341

@@ -36,16 +44,14 @@ public class GetAzureResourceGroupCommand : ResourcesBaseCmdlet
3644
public string Location { get; set; }
3745

3846
[Alias("ResourceGroupId", "ResourceId")]
39-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group Id.")]
47+
[Parameter(Mandatory = false, ParameterSetName = ResourceGroupIdParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group Id.")]
4048
[ValidateNotNullOrEmpty]
4149
public string Id { get; set; }
4250

4351
protected override void ProcessRecord()
4452
{
4553
WriteWarning("The output object of this cmdlet will be modified in a future release.");
46-
Name = string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Id)
47-
? Id.Split('/').Last()
48-
: Name;
54+
Name = Name ?? ResourceIdentifier.FromResourceGroupIdentifier(this.Id).ResourceGroupName;
4955

5056
this.WriteObject(
5157
ResourcesClient.FilterResourceGroups(name: this.Name, tag: null, detailed: false, location: this.Location),

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,26 @@ namespace Microsoft.Azure.Commands.Resources
2323
/// <summary>
2424
/// Removes a new resource group.
2525
/// </summary>
26-
[Cmdlet(VerbsCommon.Remove, "AzureRmResourceGroup", SupportsShouldProcess = true), OutputType(typeof(bool))]
26+
[Cmdlet(VerbsCommon.Remove, "AzureRmResourceGroup", SupportsShouldProcess = true, DefaultParameterSetName = ResourceGroupNameParameterSet), OutputType(typeof(bool))]
2727
public class RemoveAzureResourceGroupCommand : ResourcesBaseCmdlet
2828
{
29+
/// <summary>
30+
/// List resources group by name parameter set.
31+
/// </summary>
32+
internal const string ResourceGroupNameParameterSet = "Lists the resource group based in the name.";
33+
34+
/// <summary>
35+
/// List resources group by Id parameter set.
36+
/// </summary>
37+
internal const string ResourceGroupIdParameterSet = "Lists the resource group based in the Id.";
38+
2939
[Alias("ResourceGroupName")]
30-
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the resource group.")]
40+
[Parameter(Mandatory = true, ParameterSetName = ResourceGroupNameParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the resource group.")]
3141
[ValidateNotNullOrEmpty]
3242
public string Name {get; set;}
3343

3444
[Alias("ResourceGroupId", "ResourceId")]
35-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource group Id.")]
45+
[Parameter(Mandatory = true, ParameterSetName = ResourceGroupIdParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group Id.")]
3646
[ValidateNotNullOrEmpty]
3747
public string Id { get; set; }
3848

@@ -41,9 +51,7 @@ public class RemoveAzureResourceGroupCommand : ResourcesBaseCmdlet
4151

4252
protected override void ProcessRecord()
4353
{
44-
Name = string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Id)
45-
? Id.Split('/').Last()
46-
: Name;
54+
Name = Name ?? ResourceIdentifier.FromResourceGroupIdentifier(this.Id).ResourceGroupName;
4755

4856
ConfirmAction(
4957
Force.IsPresent,

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,31 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System.Collections;
16+
using System.Linq;
1617
using System.Management.Automation;
1718
using Microsoft.Azure.Commands.Resources.Models;
1819

1920
namespace Microsoft.Azure.Commands.Resources
2021
{
21-
using System.Linq;
2222

2323
/// <summary>
2424
/// Updates an existing resource group.
2525
/// </summary>
26-
[Cmdlet(VerbsCommon.Set, "AzureRmResourceGroup"), OutputType(typeof(PSResourceGroup))]
26+
[Cmdlet(VerbsCommon.Set, "AzureRmResourceGroup", DefaultParameterSetName = ResourceGroupNameParameterSet), OutputType(typeof(PSResourceGroup))]
2727
public class SetAzureResourceGroupCommand : ResourcesBaseCmdlet
2828
{
29+
/// <summary>
30+
/// List resources group by name parameter set.
31+
/// </summary>
32+
internal const string ResourceGroupNameParameterSet = "Lists the resource group based in the name.";
33+
34+
/// <summary>
35+
/// List resources group by Id parameter set.
36+
/// </summary>
37+
internal const string ResourceGroupIdParameterSet = "Lists the resource group based in the Id.";
38+
2939
[Alias("ResourceGroupName")]
30-
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")]
40+
[Parameter(Mandatory = true, ParameterSetName = ResourceGroupNameParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")]
3141
[ValidateNotNullOrEmpty]
3242
public string Name { get; set; }
3343

@@ -36,17 +46,15 @@ public class SetAzureResourceGroupCommand : ResourcesBaseCmdlet
3646
public Hashtable[] Tag { get; set; }
3747

3848
[Alias("ResourceGroupId", "ResourceId")]
39-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource group Id.")]
49+
[Parameter(Mandatory = true, ParameterSetName = ResourceGroupIdParameterSet, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource group Id.")]
4050
[ValidateNotNullOrEmpty]
4151
public string Id { get; set; }
4252

4353
protected override void ProcessRecord()
4454
{
4555
UpdatePSResourceGroupParameters parameters = new UpdatePSResourceGroupParameters
4656
{
47-
ResourceGroupName = string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Id)
48-
? Id.Split('/').Last()
49-
: Name,
57+
ResourceGroupName = Name ?? ResourceIdentifier.FromResourceGroupIdentifier(this.Id).ResourceGroupName,
5058
Tag = Tag,
5159
};
5260
WriteWarning("The output object of this cmdlet will be modified in a future release.");

0 commit comments

Comments
 (0)