-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Making -ResourceGroupName parameter optional for Get-AzureRmSqlServer cmdlet #4188
Changes from 8 commits
d5f2e69
a0a7986
c5e8cf5
03920c8
5766454
92e0d2a
c9f08c3
3ad36f5
c6e2cd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,16 @@ namespace Microsoft.Azure.Commands.Sql.Server.Cmdlet | |
[Cmdlet(VerbsCommon.Get, "AzureRmSqlServer", ConfirmImpact = ConfirmImpact.None, SupportsShouldProcess = true)] | ||
public class GetAzureSqlServer : AzureSqlServerCmdletBase, IModuleAssemblyInitializer | ||
{ | ||
/// <summary> | ||
/// Gets or sets the name of the resource group to use. | ||
/// </summary> | ||
[Parameter(Mandatory = false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you or some comment explaining why BaseBase when you have ResourceGroup param? I understand that's it's for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a comment. |
||
ValueFromPipelineByPropertyName = true, | ||
Position = 0, | ||
HelpMessage = "The name of the resource group.")] | ||
[ValidateNotNullOrEmpty] | ||
public override string ResourceGroupName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the name of the database server to use. | ||
/// </summary> | ||
|
@@ -38,7 +48,7 @@ public class GetAzureSqlServer : AzureSqlServerCmdletBase, IModuleAssemblyInitia | |
[Alias("Name")] | ||
[ValidateNotNullOrEmpty] | ||
public string ServerName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets a server from the service. | ||
/// </summary> | ||
|
@@ -47,14 +57,22 @@ protected override IEnumerable<AzureSqlServerModel> GetEntity() | |
{ | ||
ICollection<AzureSqlServerModel> results = null; | ||
|
||
if (this.MyInvocation.BoundParameters.ContainsKey("ServerName")) | ||
if (MyInvocation.BoundParameters.ContainsKey("ServerName") && MyInvocation.BoundParameters.ContainsKey("ResourceGroupName")) | ||
{ | ||
results = new List<AzureSqlServerModel>(); | ||
results.Add(ModelAdapter.GetServer(this.ResourceGroupName, this.ServerName)); | ||
} | ||
else if (MyInvocation.BoundParameters.ContainsKey("ResourceGroupName")) | ||
{ | ||
results = ModelAdapter.ListServersByResourceGroup(this.ResourceGroupName); | ||
} | ||
else if (!MyInvocation.BoundParameters.ContainsKey("ServerName")) | ||
{ | ||
results = ModelAdapter.ListServers(); | ||
} | ||
else | ||
{ | ||
results = ModelAdapter.GetServers(this.ResourceGroupName); | ||
throw new PSArgumentException("When specifying the serverName parameter the ResourceGroup parameter must also be used"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you split this into 2 ParamGroups so that it's enforced by powershell logic instead of custom code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might be wrong here, not sure if "no parameters" is possible to specify as a parameter set. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought a bit about this, but wasn't sure how to achieve it. According to MSDN-Parameter Set Requirements a parameter set must have at least 1 parameter. Another option is to allow specifying the ServerName without the resourceGroupName and then just retrieve the list of all servers and filter it down to just the one with the matching name (potentially bad perf)? The underlying API doesn't support retrieving a specific server without the resource group name. |
||
} | ||
|
||
return results; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, but it's a bit confusing to read. Does something like this work?
($server1, $server2, $server3) | foreach { Assert-True $_.ServerName -in $all2.ServerName }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly that does work. I wasn't aware that trying to access a property on an array would return the property value for each object in the array.