1515using Microsoft . Azure . Commands . Common . Authentication . Models ;
1616using Microsoft . Azure . Commands . Sql . JobAccount . Model ;
1717using Microsoft . Azure . Commands . Sql . JobAccount . Services ;
18- using Microsoft . Azure . Commands . Sql . Services ;
1918using Microsoft . Azure . Management . Sql . Models ;
2019using System ;
2120using System . Collections . Generic ;
2221using System . Linq ;
23- using System . Runtime . InteropServices ;
24- using System . Security ;
25- using System . Security . Permissions ;
26- using Microsoft . Azure . Commands . Sql . Server . Adapter ;
22+ using Microsoft . Azure . Commands . Sql . Server . Services ;
2723
2824namespace Microsoft . Azure . Commands . Sql . JobAccount . Adapter
2925{
@@ -58,10 +54,13 @@ public AzureSqlJobAccountAdapter(AzureContext context)
5854 /// <param name="resourceGroupName">The name of the resource group</param>
5955 /// <param name="serverName">The server the job account is in</param>
6056 /// <param name="jobAccountName">Name of the job account.</param>
61- /// <returns>The job account</returns>
62- public AzureSqlJobAccountModel GetJobAccount ( string resourceGroupName , string serverName , string jobAccountName )
57+ /// <param name="clientId">The client identifier.</param>
58+ /// <returns>
59+ /// The job account
60+ /// </returns>
61+ public AzureSqlJobAccountModel GetJobAccount ( string resourceGroupName , string serverName , string jobAccountName , string clientId )
6362 {
64- var resp = Communicator . Get ( resourceGroupName , serverName , jobAccountName , Util . GenerateTracingId ( ) ) ;
63+ var resp = Communicator . Get ( resourceGroupName , serverName , jobAccountName , clientId ) ;
6564 return CreateJobAccountModelFromResponse ( resourceGroupName , serverName , resp ) ;
6665 }
6766
@@ -70,19 +69,25 @@ public AzureSqlJobAccountModel GetJobAccount(string resourceGroupName, string se
7069 /// </summary>
7170 /// <param name="resourceGroupName">The name of the resource group</param>
7271 /// <param name="serverName">The server the job account is in</param>
73- /// <returns>A list of all the job account</returns>
74- public List < AzureSqlJobAccountModel > GetJobAccount ( string resourceGroupName , string serverName )
72+ /// <param name="clientId">The client identifier.</param>
73+ /// <returns>
74+ /// A list of all the job account
75+ /// </returns>
76+ public List < AzureSqlJobAccountModel > GetJobAccount ( string resourceGroupName , string serverName , string clientId )
7577 {
76- var resp = Communicator . List ( resourceGroupName , serverName , Util . GenerateTracingId ( ) ) ;
78+ var resp = Communicator . List ( resourceGroupName , serverName , clientId ) ;
7779 return resp . Select ( s => CreateJobAccountModelFromResponse ( resourceGroupName , serverName , s ) ) . ToList ( ) ;
7880 }
7981
8082 /// <summary>
8183 /// Upserts a server
8284 /// </summary>
8385 /// <param name="model">The server to upsert</param>
84- /// <returns>The updated server model</returns>
85- public AzureSqlJobAccountModel UpsertJobAccount ( AzureSqlJobAccountModel model )
86+ /// <param name="clientId">The client identifier.</param>
87+ /// <returns>
88+ /// The updated server model
89+ /// </returns>
90+ public AzureSqlJobAccountModel UpsertJobAccount ( AzureSqlJobAccountModel model , string clientId )
8691 {
8792 // Construct database id
8893 string databaseId = string . Format ( "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Sql/servers/{2}/databases/{3}" ,
@@ -91,7 +96,7 @@ public AzureSqlJobAccountModel UpsertJobAccount(AzureSqlJobAccountModel model)
9196 model . ServerName ,
9297 model . DatabaseName ) ;
9398
94- var resp = Communicator . CreateOrUpdate ( model . ResourceGroupName , model . ServerName , model . JobAccountName , Util . GenerateTracingId ( ) , new JobAccountCreateOrUpdateParameters
99+ var resp = Communicator . CreateOrUpdate ( model . ResourceGroupName , model . ServerName , model . JobAccountName , clientId , new JobAccountCreateOrUpdateParameters
95100 {
96101 Location = model . Location ,
97102 Tags = model . Tags ,
@@ -110,9 +115,10 @@ public AzureSqlJobAccountModel UpsertJobAccount(AzureSqlJobAccountModel model)
110115 /// <param name="resourceGroupName">The resource group the server is in</param>
111116 /// <param name="serverName">The server the job account is in</param>
112117 /// <param name="jobAccountName">Name of the job account to delete.</param>
113- public void RemoveJobAccount ( string resourceGroupName , string serverName , string jobAccountName )
118+ /// <param name="clientId">The client identifier.</param>
119+ public void RemoveJobAccount ( string resourceGroupName , string serverName , string jobAccountName , string clientId )
114120 {
115- Communicator . Remove ( resourceGroupName , serverName , jobAccountName , Util . GenerateTracingId ( ) ) ;
121+ Communicator . Remove ( resourceGroupName , serverName , jobAccountName , clientId ) ;
116122 }
117123
118124 /// <summary>
@@ -129,28 +135,57 @@ private static AzureSqlJobAccountModel CreateJobAccountModelFromResponse(string
129135 int lastSlashIndex = resp . Properties . DatabaseId . LastIndexOf ( '/' ) ;
130136 string databaseName = resp . Properties . DatabaseId . Substring ( lastSlashIndex + 1 ) ;
131137
132- AzureSqlJobAccountModel jobAccount = new AzureSqlJobAccountModel ( ) ;
133-
134- jobAccount . ResourceGroupName = resourceGroupName ;
135- jobAccount . ServerName = serverName ;
136- jobAccount . JobAccountName = resp . Name ;
137- jobAccount . Location = resp . Location ;
138- jobAccount . DatabaseName = databaseName ;
138+ AzureSqlJobAccountModel jobAccount = new AzureSqlJobAccountModel
139+ {
140+ ResourceGroupName = resourceGroupName ,
141+ ServerName = serverName ,
142+ JobAccountName = resp . Name ,
143+ Location = resp . Location ,
144+ DatabaseName = databaseName
145+ } ;
139146
140147 return jobAccount ;
141148 }
142149
143150 /// <summary>
144- /// Gets the Location of the server.
151+ /// Gets the Location of the server. Throws an exception if the server does not support job accounts.
145152 /// </summary>
146153 /// <param name="resourceGroupName">The resource group the server is in</param>
147154 /// <param name="serverName">The name of the server</param>
155+ /// <param name="clientId">The client identifier.</param>
148156 /// <returns></returns>
149- public string GetServerLocation ( string resourceGroupName , string serverName )
157+ /// <remarks>
158+ /// These 2 operations (get location, throw if not supported) are combined in order to minimize round trips.
159+ /// </remarks>
160+ public string GetServerLocationAndThrowIfJobAccountNotSupportedByServer ( string resourceGroupName , string serverName , string clientId )
150161 {
151- AzureSqlServerAdapter serverAdapter = new AzureSqlServerAdapter ( Context ) ;
152- var server = serverAdapter . GetServer ( resourceGroupName , serverName ) ;
162+ AzureSqlServerCommunicator serverCommunicator = new AzureSqlServerCommunicator ( Context ) ;
163+ var server = serverCommunicator . Get ( resourceGroupName , serverName , clientId ) ;
164+
165+ ThrowIfJobAccountNotSupportedByServer ( server ) ;
166+
153167 return server . Location ;
154168 }
169+
170+ /// <summary>
171+ /// Throws an exception if the server does not support job accounts.
172+ /// </summary>
173+ public void ThrowIfJobAccountNotSupportedByServer ( string resourceGroupName , string serverName , string clientId )
174+ {
175+ AzureSqlServerCommunicator serverCommunicator = new AzureSqlServerCommunicator ( Context ) ;
176+ Management . Sql . Models . Server server = serverCommunicator . Get ( resourceGroupName , serverName , clientId ) ;
177+ ThrowIfJobAccountNotSupportedByServer ( server ) ;
178+ }
179+
180+ /// <summary>
181+ /// Throws an exception if the server does not support job accounts.
182+ /// </summary>
183+ private static void ThrowIfJobAccountNotSupportedByServer ( Management . Sql . Models . Server server )
184+ {
185+ if ( server . Properties . Version != "12.0" )
186+ {
187+ throw new InvalidOperationException ( string . Format ( Properties . Resources . ServerNotApplicableForJobAccount , server . Name ) ) ;
188+ }
189+ }
155190 }
156191}
0 commit comments