Skip to content

Commit 911fb81

Browse files
committed
Adding custom output for Capabilities cmdlet
When filtering the results of the Capabilites cmdlet, custom output is provided.
1 parent 2b36b14 commit 911fb81

File tree

5 files changed

+165
-3
lines changed

5 files changed

+165
-3
lines changed

src/ResourceManager/Resources/Commands.Resources/AzureResourceManager.psd1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ FormatsToProcess = @(
7272
'.\Compute\Microsoft.Azure.Commands.Compute.format.generated.ps1xml',
7373
'.\Network\Microsoft.Azure.Commands.Network.format.ps1xml',
7474
'.\Storage\Microsoft.WindowsAzure.Commands.Storage.format.ps1xml',
75-
'.\StorageManagement\Microsoft.Azure.Commands.Management.Storage.format.ps1xml',
75+
'.\StorageManagement\Microsoft.Azure.Commands.Management.Storage.format.ps1xml',
7676
'.\OperationalInsights\Microsoft.Azure.Commands.OperationalInsights.format.ps1xml',
7777
'.\AzureBackup\Microsoft.Azure.Commands.AzureBackup.format.ps1xml',
78-
'.\UsageAggregates\Microsoft.Azure.Commands.UsageAggregates.Format.ps1xml'
78+
'.\UsageAggregates\Microsoft.Azure.Commands.UsageAggregates.Format.ps1xml',
79+
'.\Sql\Microsoft.Azure.Commands.Sql.format.ps1xml'
7980
)
8081

8182
# Modules to import as nested modules of the module specified in ModuleToProcess

src/ResourceManager/Sql/Commands.Sql/Commands.Sql.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@
139139
<Compile Include="TransparentDataEncryption\Model\AzureSqlDatabaseTransparentDataEncryptionModel.cs" />
140140
<Compile Include="TransparentDataEncryption\Services\AzureSqlDatabaseTransparentDataEncryptionAdapter.cs" />
141141
<Compile Include="TransparentDataEncryption\Services\AzureSqlDatabaseTransparentDataEncryptionCommunicator.cs" />
142+
<Content Include="Microsoft.Azure.Commands.Sql.format.ps1xml">
143+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
144+
</Content>
142145
<None Include="MSSharedLibKey.snk" />
143146
<Compile Include="Common\Constants.cs" />
144147
<Compile Include="Database\Cmdlet\AzureSqlDatabaseActivityCmdletBase.cs" />

src/ResourceManager/Sql/Commands.Sql/Location Capabilities/Cmdlet/GetAzureSqlCapability.cs

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.Azure.Commands.Sql.Location_Capabilities.Services;
1818
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1919
using System.Linq;
20+
using System.Text;
2021

2122
namespace Microsoft.Azure.Commands.Sql.Location_Capabilities.Cmdlet
2223
{
@@ -90,33 +91,144 @@ public override void ExecuteCmdlet()
9091
{
9192
AzureSqlCapabilitiesAdapter adapter = new AzureSqlCapabilitiesAdapter(Profile, Profile.Context.Subscription);
9293
LocationCapabilityModel model = adapter.GetLocationCapabilities(LocationName);
94+
int depth = 0;
9395

9496
switch(ParameterSetName)
9597
{
9698
case _default:
9799
{
98100
FilterByDefaults(model);
101+
depth = 3;
99102
}
100103
break;
101104
case _filtered:
102105
{
103106
if (this.MyInvocation.BoundParameters.ContainsKey("ServerVersionName"))
104107
{
105108
FilterByServerVersion(model);
109+
depth = 1;
106110
}
107111
if (this.MyInvocation.BoundParameters.ContainsKey("EditionName"))
108112
{
109113
FilterByEditionName(model);
114+
depth = 2;
110115
}
111116
if (this.MyInvocation.BoundParameters.ContainsKey("ServiceObjectiveName"))
112117
{
113118
FilterByServiceObjectiveName(model);
119+
depth = 3;
114120
}
115121
}
116122
break;
117123
}
118124

119-
this.WriteObject(model);
125+
if(depth > 0)
126+
{
127+
model.ExpandedDetails = CreateExpandedDetails(model, depth);
128+
}
129+
130+
this.WriteObject(model, true);
131+
}
132+
133+
/// <summary>
134+
/// Given a <see cref="LocationCapabilityModel"/> constructs a formatted string of its expanded details to the desired depth.
135+
/// </summary>
136+
/// <param name="model">The model details</param>
137+
/// <param name="depth">The depth to expand to</param>
138+
/// <returns>The formatted string containing the model details</returns>
139+
private string CreateExpandedDetails(LocationCapabilityModel model, int depth)
140+
{
141+
StringBuilder builder = new StringBuilder();
142+
143+
foreach(var version in model.SupportedServerVersions)
144+
{
145+
string versionInfo = GetVersionInformation(version);
146+
147+
if(depth > 1)
148+
{
149+
ExpandEdition(depth, builder, version, versionInfo);
150+
}
151+
else
152+
{
153+
builder.AppendLine(versionInfo);
154+
}
155+
}
156+
157+
return builder.ToString();
158+
}
159+
160+
/// <summary>
161+
/// Formats all the supported editions in <paramref name="version"/> as strings prefixed with <paramref name="prefix"/>
162+
/// and appends them to the <paramref name="builder"/>
163+
/// </summary>
164+
/// <param name="depth">How deep to expand the information</param>
165+
/// <param name="builder">The string builder to append the information</param>
166+
/// <param name="version">The version object to expand and format</param>
167+
/// <param name="prefix">The prefix to apply to the information strings</param>
168+
private void ExpandEdition(int depth, StringBuilder builder, ServerVersionCapabilityModel version, string prefix)
169+
{
170+
foreach (var edition in version.SupportedEditions)
171+
{
172+
string editionInfo = GetEditionInformation(prefix, edition);
173+
174+
if (depth > 2)
175+
{
176+
ExpandServiceObjective(builder, edition, editionInfo);
177+
}
178+
else
179+
{
180+
builder.AppendLine(editionInfo);
181+
}
182+
}
183+
}
184+
185+
/// <summary>
186+
/// Formats all the supported service objectives in <paramref name="edition"/> as strings prefixed with <paramref name="prefix"/>
187+
/// and appends them to the <paramref name="builder"/>
188+
/// </summary>
189+
/// <param name="builder">The string building to add the formatted string to</param>
190+
/// <param name="edition">The edition containing the supported service objectives</param>
191+
/// <param name="prefix">The prefix for the formatted string</param>
192+
private void ExpandServiceObjective(StringBuilder builder, EditionCapabilityModel edition, string prefix)
193+
{
194+
foreach (var slo in edition.SupportedServiceObjectives)
195+
{
196+
string serviceObjectiveInfo = GetServiceObjectiveInformation(prefix, slo);
197+
198+
builder.AppendLine(serviceObjectiveInfo);
199+
}
200+
}
201+
202+
/// <summary>
203+
/// Gets the string formatting of the server version object
204+
/// </summary>
205+
/// <param name="version">The server version information to format as a string</param>
206+
/// <returns>The formatted string containing the server version information</returns>
207+
private string GetVersionInformation(ServerVersionCapabilityModel version)
208+
{
209+
return string.Format("Version: {0} ({1})", version.ServerVersionName, version.Status);
210+
}
211+
212+
/// <summary>
213+
/// Gets the string formatting of the edition object
214+
/// </summary>
215+
/// <param name="baseString">The prefix before the edition information</param>
216+
/// <param name="edition">The edition information to format and append to the end of the baseString</param>
217+
/// <returns>The formatted string containing the edition information</returns>
218+
private string GetEditionInformation(string baseString, EditionCapabilityModel edition)
219+
{
220+
return string.Format("{0} -> Edition: {1} ({2})", baseString, edition.EditionName, edition.Status);
221+
}
222+
223+
/// <summary>
224+
/// Gets the string formatting of the service objective object
225+
/// </summary>
226+
/// <param name="baseString">The prefix before the service objective information</param>
227+
/// <param name="objective">The service objective information to append</param>
228+
/// <returns>The formatted string containing the service objective information</returns>
229+
private string GetServiceObjectiveInformation(string baseString, ServiceObjectiveCapabilityModel objective)
230+
{
231+
return string.Format("{0} -> Service Objective: {1} ({2})", baseString, objective.ServiceObjectiveName, objective.Status);
120232
}
121233

122234
/// <summary>

src/ResourceManager/Sql/Commands.Sql/Location Capabilities/Model/LocationCapabilityModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,10 @@ public class LocationCapabilityModel
3535
/// Gets or sets a list of all the supported server versions and their capabilities
3636
/// </summary>
3737
public IList<ServerVersionCapabilityModel> SupportedServerVersions { get; set; }
38+
39+
/// <summary>
40+
/// Gets or sets a formatted string containing the expanded details of the model
41+
/// </summary>
42+
public string ExpandedDetails { get; set; }
3843
}
3944
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Configuration>
3+
<ViewDefinitions>
4+
<View>
5+
<Name>Microsoft.Azure.Commands.Sql.Location_Capabilities.Model.LocationCapabilityModel</Name>
6+
<ViewSelectedBy>
7+
<TypeName>Microsoft.Azure.Commands.Sql.Location_Capabilities.Model.LocationCapabilityModel</TypeName>
8+
</ViewSelectedBy>
9+
<ListControl>
10+
<ListEntries>
11+
<ListEntry>
12+
<ListItems>
13+
<ListItem>
14+
<Label>Location</Label>
15+
<PropertyName>LocationName</PropertyName>
16+
</ListItem>
17+
<ListItem>
18+
<Label>Status</Label>
19+
<PropertyName>Status</PropertyName>
20+
</ListItem>
21+
<ListItem>
22+
<Label>ExpandedDetails</Label>
23+
<PropertyName>ExpandedDetails</PropertyName>
24+
<ItemSelectionCondition>
25+
<PropertyName>ExpandedDetails</PropertyName>
26+
</ItemSelectionCondition>
27+
</ListItem>
28+
<ListItem>
29+
<Label>SupportedServerVersions</Label>
30+
<PropertyName>SupportedServerVersions</PropertyName>
31+
<ItemSelectionCondition>
32+
<ScriptBlock>$_.ExpandedDetails -eq $null</ScriptBlock>
33+
</ItemSelectionCondition>
34+
</ListItem>
35+
</ListItems>
36+
</ListEntry>
37+
</ListEntries>
38+
</ListControl>
39+
</View>
40+
</ViewDefinitions>
41+
</Configuration>

0 commit comments

Comments
 (0)