12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
+ using System ;
15
16
using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
16
- using Microsoft . Azure . Commands . Common . Authentication . Models ;
17
17
using Microsoft . Azure . Commands . Sql . ServiceObjective . Model ;
18
- using Microsoft . Azure . Commands . Sql . ServiceObjective . Services ;
19
- using Microsoft . Azure . Commands . Sql . Services ;
20
18
using System . Collections . Generic ;
21
19
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 ;
22
23
23
24
namespace Microsoft . Azure . Commands . Sql . ServiceObjective . Adapter
24
25
{
@@ -28,9 +29,14 @@ namespace Microsoft.Azure.Commands.Sql.ServiceObjective.Adapter
28
29
public class AzureSqlServerServiceObjectiveAdapter
29
30
{
30
31
/// <summary>
31
- /// Gets or sets the AzureSqlDatabaseServerServiceObjectiveCommunicator which has all the needed management clients
32
+ /// Gets or sets the server communicator.
32
33
/// </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 ; }
34
40
35
41
/// <summary>
36
42
/// Gets or sets the Azure profile
@@ -45,7 +51,8 @@ public class AzureSqlServerServiceObjectiveAdapter
45
51
public AzureSqlServerServiceObjectiveAdapter ( IAzureContext context )
46
52
{
47
53
Context = context ;
48
- Communicator = new AzureSqlServerServiceObjectiveCommunicator ( Context ) ;
54
+ ServerCommunicator = new AzureSqlServerCommunicator ( Context ) ;
55
+ CapabilitiesCommunicator = new AzureSqlCapabilitiesCommunicator ( Context ) ;
49
56
}
50
57
51
58
/// <summary>
@@ -55,10 +62,16 @@ public AzureSqlServerServiceObjectiveAdapter(IAzureContext context)
55
62
/// <param name="serverName">The name of the server</param>
56
63
/// <param name="serviceObjectiveName">The name of the service objective</param>
57
64
/// <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 )
59
66
{
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 ( ) ;
62
75
}
63
76
64
77
/// <summary>
@@ -69,34 +82,69 @@ public AzureSqlServerServiceObjectiveModel GetServiceObjective(string resourceGr
69
82
/// <returns>A list of all the ServiceObjectives</returns>
70
83
public List < AzureSqlServerServiceObjectiveModel > ListServiceObjectives ( string resourceGroupName , string serverName )
71
84
{
72
- var resp = Communicator . List ( resourceGroupName , serverName ) ;
85
+ var server = ServerCommunicator . Get ( resourceGroupName , serverName ) ;
86
+ var capabilities = CapabilitiesCommunicator . Get ( server . Location ) ;
73
87
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 ( ) ;
78
93
}
79
94
80
95
/// <summary>
81
- /// Convert a Management.Sql.LegacySdk.Models.ServiceObjective to AzureSqlDatabaseServerServiceObjectiveModel
96
+ /// Convert a SLO capability to AzureSqlDatabaseServerServiceObjectiveModel
82
97
/// </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>
86
102
/// <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 )
88
108
{
89
- AzureSqlServerServiceObjectiveModel slo = new AzureSqlServerServiceObjectiveModel ( )
109
+ return new AzureSqlServerServiceObjectiveModel ( )
90
110
{
91
111
ResourceGroupName = resourceGroupName ,
92
112
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
98
123
} ;
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 ;
100
148
}
101
149
}
102
150
}
0 commit comments