Skip to content

Commit a15b8ba

Browse files
committed
Update Get-AzSqlServerServiceObjectives to use capabilities
The capabilities API provides more data about service objectives, particularly the Sku-related properties (sku name, capacity, etc). This is helpful for REST API users to determine the possible sku parameters. Also added table formatter so that the most useful set of columns is returned by default.
1 parent 084c498 commit a15b8ba

File tree

5 files changed

+155
-120
lines changed

5 files changed

+155
-120
lines changed

src/Sql/Sql/ServiceObjective/Cmdlet/GetAzureSqlServerServiceObjective.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
1516
using Microsoft.Azure.Commands.Sql.ServiceObjective.Model;
1617
using System.Collections.Generic;
1718
using System.Management.Automation;
@@ -43,12 +44,12 @@ protected override IEnumerable<AzureSqlServerServiceObjectiveModel> GetEntity()
4344
{
4445
ICollection<AzureSqlServerServiceObjectiveModel> results = null;
4546

46-
if (this.MyInvocation.BoundParameters.ContainsKey("ServiceObjectiveName") && !WildcardPattern.ContainsWildcardCharacters(ServiceObjectiveName))
47+
if (this.MyInvocation.BoundParameters.ContainsKey("ServiceObjectiveName"))
4748
{
48-
results = new List<AzureSqlServerServiceObjectiveModel>
49-
{
50-
ModelAdapter.GetServiceObjective(this.ResourceGroupName, this.ServerName, this.ServiceObjectiveName)
51-
};
49+
results = ModelAdapter.GetServiceObjective(
50+
this.ResourceGroupName,
51+
this.ServerName,
52+
this.ServiceObjectiveName);
5253
}
5354
else
5455
{

src/Sql/Sql/ServiceObjective/Model/AzureSqlServerServiceObjectiveModel.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// ----------------------------------------------------------------------------------
1414

1515

16+
using Microsoft.Azure.Commands.Sql.Database.Cmdlet;
17+
1618
namespace Microsoft.Azure.Commands.Sql.ServiceObjective.Model
1719
{
1820
/// <summary>
@@ -33,6 +35,9 @@ public class AzureSqlServerServiceObjectiveModel
3335
/// <summary>
3436
/// Gets or sets the name of the service objective
3537
/// </summary>
38+
/// <remarks>
39+
/// Can be used as input to <see cref="NewAzureSqlDatabase.RequestedServiceObjectiveName"/>
40+
/// </remarks>
3641
public string ServiceObjectiveName { get; set; }
3742

3843
/// <summary>
@@ -54,5 +59,45 @@ public class AzureSqlServerServiceObjectiveModel
5459
/// Gets or sets Whether or no the Service Objective is a system value
5560
/// </summary>
5661
public bool IsSystem { get; set; }
62+
63+
/// <summary>
64+
/// Gets or sets the edition
65+
/// </summary>
66+
/// <remarks>
67+
/// Can be used as input to <see cref="NewAzureSqlDatabase.Edition"/>
68+
/// </remarks>
69+
public string Edition { get; set; }
70+
71+
/// <summary>
72+
/// Gets or sets the sku name
73+
/// </summary>
74+
/// <remarks>
75+
/// Not used as input to another cmdlet, but useful to help document the available sku names.
76+
/// </remarks>
77+
public string Sku { get; set; }
78+
79+
/// <summary>
80+
/// Gets or sets the family
81+
/// </summary>
82+
/// <remarks>
83+
/// Can be used as input to <see cref="NewAzureSqlDatabase.ComputeGeneration"/> (which has "Family" alias)
84+
/// </remarks>
85+
public string Family { get; set; }
86+
87+
/// <summary>
88+
/// Gets or sets the capacity (e.g. in DTU or vcores).
89+
/// </summary>
90+
/// <remarks>
91+
/// Can be used as input to <see cref="NewAzureSqlDatabase.VCore"/> (which has "Capacity" alias).
92+
/// </remarks>
93+
public int? Capacity { get; set; }
94+
95+
/// <summary>
96+
/// Gets or sets the capacity unit (e.g. DTU or vcores).
97+
/// </summary>
98+
/// <remarks>
99+
/// Not used as input to another cmdlet, but useful to help document the capacity.
100+
/// </remarks>
101+
public string CapacityUnit { get; set; }
57102
}
58103
}

src/Sql/Sql/ServiceObjective/Service/AzureSqlServerServiceObjectiveAdapter.cs

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
1516
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
16-
using Microsoft.Azure.Commands.Common.Authentication.Models;
1717
using Microsoft.Azure.Commands.Sql.ServiceObjective.Model;
18-
using Microsoft.Azure.Commands.Sql.ServiceObjective.Services;
19-
using Microsoft.Azure.Commands.Sql.Services;
2018
using System.Collections.Generic;
2119
using System.Linq;
20+
using Microsoft.Azure.Commands.Sql.Location_Capabilities.Services;
21+
using Microsoft.Azure.Commands.Sql.Server.Services;
22+
using Microsoft.Azure.Management.Sql.Models;
2223

2324
namespace Microsoft.Azure.Commands.Sql.ServiceObjective.Adapter
2425
{
@@ -28,9 +29,14 @@ namespace Microsoft.Azure.Commands.Sql.ServiceObjective.Adapter
2829
public class AzureSqlServerServiceObjectiveAdapter
2930
{
3031
/// <summary>
31-
/// Gets or sets the AzureSqlDatabaseServerServiceObjectiveCommunicator which has all the needed management clients
32+
/// Gets or sets the server communicator.
3233
/// </summary>
33-
private AzureSqlServerServiceObjectiveCommunicator Communicator { get; set; }
34+
public AzureSqlServerCommunicator ServerCommunicator { get; set; }
35+
36+
/// <summary>
37+
/// Gets or sets the capabilities communicator.
38+
/// </summary>
39+
public AzureSqlCapabilitiesCommunicator CapabilitiesCommunicator { get; set; }
3440

3541
/// <summary>
3642
/// Gets or sets the Azure profile
@@ -45,7 +51,8 @@ public class AzureSqlServerServiceObjectiveAdapter
4551
public AzureSqlServerServiceObjectiveAdapter(IAzureContext context)
4652
{
4753
Context = context;
48-
Communicator = new AzureSqlServerServiceObjectiveCommunicator(Context);
54+
ServerCommunicator = new AzureSqlServerCommunicator(Context);
55+
CapabilitiesCommunicator = new AzureSqlCapabilitiesCommunicator(Context);
4956
}
5057

5158
/// <summary>
@@ -55,10 +62,16 @@ public AzureSqlServerServiceObjectiveAdapter(IAzureContext context)
5562
/// <param name="serverName">The name of the server</param>
5663
/// <param name="serviceObjectiveName">The name of the service objective</param>
5764
/// <returns>The ServiceObjective</returns>
58-
public AzureSqlServerServiceObjectiveModel GetServiceObjective(string resourceGroupName, string serverName, string serviceObjectiveName)
65+
public List<AzureSqlServerServiceObjectiveModel> GetServiceObjective(string resourceGroupName, string serverName, string serviceObjectiveName)
5966
{
60-
var resp = Communicator.Get(resourceGroupName, serverName, serviceObjectiveName);
61-
return CreateServiceObjectiveModelFromResponse(resourceGroupName, serverName, resp);
67+
var server = ServerCommunicator.Get(resourceGroupName, serverName);
68+
var capabilities = CapabilitiesCommunicator.Get(server.Location);
69+
70+
return (
71+
from sv in FilterByName(capabilities.SupportedServerVersions, server.Version)
72+
from e in sv.SupportedEditions
73+
from slo in FilterByName(e.SupportedServiceLevelObjectives, serviceObjectiveName)
74+
select CreateServiceObjectiveModelFromResponse(e, slo, resourceGroupName, serverName)).ToList();
6275
}
6376

6477
/// <summary>
@@ -69,34 +82,69 @@ public AzureSqlServerServiceObjectiveModel GetServiceObjective(string resourceGr
6982
/// <returns>A list of all the ServiceObjectives</returns>
7083
public List<AzureSqlServerServiceObjectiveModel> ListServiceObjectives(string resourceGroupName, string serverName)
7184
{
72-
var resp = Communicator.List(resourceGroupName, serverName);
85+
var server = ServerCommunicator.Get(resourceGroupName, serverName);
86+
var capabilities = CapabilitiesCommunicator.Get(server.Location);
7387

74-
return resp.Select((s) =>
75-
{
76-
return CreateServiceObjectiveModelFromResponse(resourceGroupName, serverName, s);
77-
}).ToList();
88+
return (
89+
from sv in FilterByName(capabilities.SupportedServerVersions, server.Version)
90+
from e in sv.SupportedEditions
91+
from slo in e.SupportedServiceLevelObjectives
92+
select CreateServiceObjectiveModelFromResponse(e, slo, resourceGroupName, serverName)).ToList();
7893
}
7994

8095
/// <summary>
81-
/// Convert a Management.Sql.LegacySdk.Models.ServiceObjective to AzureSqlDatabaseServerServiceObjectiveModel
96+
/// Convert a SLO capability to AzureSqlDatabaseServerServiceObjectiveModel
8297
/// </summary>
83-
/// <param name="resourceGroupName">The resource group the server is in</param>
84-
/// <param name="serverName">The name of the server</param>
85-
/// <param name="resp">The management client ServiceObjective response to convert</param>
98+
/// <param name="edition">The edition</param>
99+
/// <param name="slo">The service objective</param>
100+
/// <param name="resourceGroupName">The resource group name</param>
101+
/// /// <param name="serverName">The server name</param>
86102
/// <returns>The converted ServiceObjective model</returns>
87-
private static AzureSqlServerServiceObjectiveModel CreateServiceObjectiveModelFromResponse(string resourceGroupName, string serverName, Management.Sql.Models.ServiceObjective resp)
103+
private static AzureSqlServerServiceObjectiveModel CreateServiceObjectiveModelFromResponse(
104+
EditionCapability edition,
105+
ServiceObjectiveCapability slo,
106+
string resourceGroupName = null,
107+
string serverName = null)
88108
{
89-
AzureSqlServerServiceObjectiveModel slo = new AzureSqlServerServiceObjectiveModel()
109+
return new AzureSqlServerServiceObjectiveModel()
90110
{
91111
ResourceGroupName = resourceGroupName,
92112
ServerName = serverName,
93-
ServiceObjectiveName = resp.ServiceObjectiveName,
94-
IsDefault = resp.IsDefault,
95-
IsSystem = resp.IsSystem,
96-
Description = resp.Description,
97-
Enabled = resp.Enabled
113+
ServiceObjectiveName = slo.Name,
114+
IsDefault = slo.Status == CapabilityStatus.Default,
115+
IsSystem = string.Equals(edition.Name, "System", StringComparison.OrdinalIgnoreCase),
116+
Description = string.Empty,
117+
Enabled = IsEnabled(slo.Status),
118+
Edition = edition.Name,
119+
Sku = slo.Sku.Name,
120+
Family = slo.Sku.Family,
121+
Capacity = slo.Sku.Capacity,
122+
CapacityUnit = slo.PerformanceLevel.Unit
98123
};
99-
return slo;
124+
}
125+
126+
#region Filter by name
127+
128+
private static IEnumerable<ServerVersionCapability> FilterByName(IEnumerable<ServerVersionCapability> capabilities, string name)
129+
{
130+
return capabilities.Where(c => string.Equals(c.Name, name, StringComparison.OrdinalIgnoreCase));
131+
}
132+
133+
private static IEnumerable<EditionCapability> FilterByName(IEnumerable<EditionCapability> capabilities, string name)
134+
{
135+
return capabilities.Where(c => string.Equals(c.Name, name, StringComparison.OrdinalIgnoreCase));
136+
}
137+
138+
private static IEnumerable<ServiceObjectiveCapability> FilterByName(IEnumerable<ServiceObjectiveCapability> capabilities, string name)
139+
{
140+
return capabilities.Where(c => string.Equals(c.Name, name, StringComparison.OrdinalIgnoreCase));
141+
}
142+
143+
#endregion
144+
145+
private static bool IsEnabled(CapabilityStatus? status)
146+
{
147+
return status == CapabilityStatus.Default || status == CapabilityStatus.Available;
100148
}
101149
}
102150
}

src/Sql/Sql/ServiceObjective/Service/AzureSqlServerServiceObjectiveCommunicator.cs

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/Sql/Sql/Sql.format.ps1xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,36 @@
5353
</ListEntries>
5454
</ListControl>
5555
</View>
56+
<View>
57+
<Name>Microsoft.Azure.Commands.Sql.ServiceObjective.Model.AzureSqlServerServiceObjectiveModel</Name>
58+
<ViewSelectedBy>
59+
<TypeName>Microsoft.Azure.Commands.Sql.ServiceObjective.Model.AzureSqlServerServiceObjectiveModel</TypeName>
60+
</ViewSelectedBy>
61+
<TableControl>
62+
<TableHeaders>
63+
<TableColumnHeader><Label>ServiceObjectiveName</Label></TableColumnHeader>
64+
<TableColumnHeader><Label>Sku</Label></TableColumnHeader>
65+
<TableColumnHeader><Label>Edition</Label></TableColumnHeader>
66+
<TableColumnHeader><Label>Family</Label></TableColumnHeader>
67+
<TableColumnHeader><Label>Capacity</Label></TableColumnHeader>
68+
<TableColumnHeader><Label>CapacityUnit</Label></TableColumnHeader>
69+
<TableColumnHeader><Label>Enabled</Label></TableColumnHeader>
70+
</TableHeaders>
71+
<TableRowEntries>
72+
<TableRowEntry>
73+
<TableColumnItems>
74+
<TableColumnItem><PropertyName>ServiceObjectiveName</PropertyName></TableColumnItem>
75+
<TableColumnItem><PropertyName>Sku</PropertyName></TableColumnItem>
76+
<TableColumnItem><PropertyName>Edition</PropertyName></TableColumnItem>
77+
<TableColumnItem><PropertyName>Family</PropertyName></TableColumnItem>
78+
<TableColumnItem><PropertyName>Capacity</PropertyName></TableColumnItem>
79+
<TableColumnItem><PropertyName>CapacityUnit</PropertyName></TableColumnItem>
80+
<TableColumnItem><PropertyName>Enabled</PropertyName></TableColumnItem>
81+
</TableColumnItems>
82+
</TableRowEntry>
83+
</TableRowEntries>
84+
</TableControl>
85+
</View>
5686
<View>
5787
<Name>Microsoft.Azure.Commands.Sql.AdvancedThreatProtection.Model.ServerAdvancedThreatProtectionPolicyModel</Name>
5888
<ViewSelectedBy>

0 commit comments

Comments
 (0)