|
25 | 25 | using System.Collections.Generic;
|
26 | 26 | using System.Linq;
|
27 | 27 | using System.Net.Http;
|
| 28 | +using System.Text; |
28 | 29 | using System.Threading;
|
29 | 30 | using System.Threading.Tasks;
|
30 | 31 |
|
@@ -234,5 +235,140 @@ private ResourceManagementClient GetCurrentResourcesClient(IAzureContext context
|
234 | 235 | }
|
235 | 236 | return ResourcesClient;
|
236 | 237 | }
|
| 238 | + |
| 239 | + /// <summary> |
| 240 | + /// Retrieves storage keys. |
| 241 | + /// </summary> |
| 242 | + /// <param name="storageAccountSubscriptionId">Storage account subscription id</param> |
| 243 | + /// <param name="storageAccountName">Storage account name</param> |
| 244 | + /// <returns>Dictionary containing storage keys</returns> |
| 245 | + internal Dictionary<StorageKeyKind, string> RetrieveStorageKeys(Guid storageAccountSubscriptionId, string storageAccountName) |
| 246 | + { |
| 247 | + // Retrieve the id of the storage account. |
| 248 | + // |
| 249 | + string storageAccountId = RetrieveStorageAccountIdAsync(storageAccountSubscriptionId, storageAccountName).GetAwaiter().GetResult(); |
| 250 | + |
| 251 | + // Extract storage account keys. |
| 252 | + // |
| 253 | + return RetrieveStorageKeysAsync(storageAccountId).GetAwaiter().GetResult(); |
| 254 | + } |
| 255 | + |
| 256 | + /// <summary> |
| 257 | + /// Retrieves storage account keys. |
| 258 | + /// </summary> |
| 259 | + /// <param name="storageAccountId">Storage account id</param> |
| 260 | + /// <returns>Dictionary containing storage keys</returns> |
| 261 | + private async Task<Dictionary<StorageKeyKind, string>> RetrieveStorageKeysAsync(string storageAccountId) |
| 262 | + { |
| 263 | + bool isClassicStorage = storageAccountId.Contains("Microsoft.ClassicStorage/storageAccounts"); |
| 264 | + |
| 265 | + // Build a URI for calling corresponding REST-API |
| 266 | + // |
| 267 | + StringBuilder uriBuilder = new StringBuilder(Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager).ToString()); |
| 268 | + uriBuilder.AppendFormat("{0}/listKeys?api-version={1}", |
| 269 | + storageAccountId, |
| 270 | + isClassicStorage ? "2016-11-01" : "2017-06-01"); |
| 271 | + |
| 272 | + // Define an exception to be thrown on failure. |
| 273 | + // |
| 274 | + Exception exception = new Exception(string.Format(Properties.Resources.RetrievingStorageAccountKeysFailed, storageAccountId)); |
| 275 | + |
| 276 | + // Call the URI and get storage account keys. |
| 277 | + // |
| 278 | + JToken storageAccountKeysResponse = await SendAsync(uriBuilder.ToString(), HttpMethod.Post, exception); |
| 279 | + |
| 280 | + // Extract keys out of response. |
| 281 | + // |
| 282 | + Dictionary<StorageKeyKind, string> storageAccountKeys = new Dictionary<StorageKeyKind, string>(); |
| 283 | + string primaryKey = null; |
| 284 | + string secondaryKey = null; |
| 285 | + if (isClassicStorage) |
| 286 | + { |
| 287 | + primaryKey = (string)storageAccountKeysResponse["primaryKey"]; |
| 288 | + secondaryKey = (string)storageAccountKeysResponse["secondaryKey"]; |
| 289 | + } |
| 290 | + else |
| 291 | + { |
| 292 | + JArray storageAccountKeysArray = (JArray)storageAccountKeysResponse["keys"]; |
| 293 | + if (storageAccountKeysArray == null) |
| 294 | + { |
| 295 | + throw exception; |
| 296 | + } |
| 297 | + |
| 298 | + primaryKey = (string)storageAccountKeysArray[0]["value"]; |
| 299 | + secondaryKey = (string)storageAccountKeysArray[1]["value"]; |
| 300 | + } |
| 301 | + |
| 302 | + if (string.IsNullOrEmpty(primaryKey) || string.IsNullOrEmpty(secondaryKey)) |
| 303 | + { |
| 304 | + throw exception; |
| 305 | + } |
| 306 | + |
| 307 | + storageAccountKeys.Add(StorageKeyKind.Primary, primaryKey); |
| 308 | + storageAccountKeys.Add(StorageKeyKind.Secondary, secondaryKey); |
| 309 | + return storageAccountKeys; |
| 310 | + } |
| 311 | + |
| 312 | + /// <summary> |
| 313 | + /// Retrieves id of a storage account |
| 314 | + /// </summary> |
| 315 | + /// <param name="storageAccountSubscriptionId">Storage account subscription id</param> |
| 316 | + /// <param name="storageAccountName">Storage account name</param> |
| 317 | + /// <returns>Id of the storage account</returns> |
| 318 | + private async Task<string> RetrieveStorageAccountIdAsync(Guid storageAccountSubscriptionId, string storageAccountName) |
| 319 | + { |
| 320 | + // Build a URI for calling corresponding REST-API. |
| 321 | + // |
| 322 | + StringBuilder uriBuilder = new StringBuilder(Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager).ToString()); |
| 323 | + uriBuilder.AppendFormat("/resources?api-version=2018-05-01&$filter=(subscriptionId%20eq%20'{0}')%20and%20((resourceType%20eq%20'microsoft.storage/storageaccounts')%20or%20(resourceType%20eq%20'microsoft.classicstorage/storageaccounts'))%20and%20(name%20eq%20'{1}')", |
| 324 | + storageAccountSubscriptionId, |
| 325 | + storageAccountName); |
| 326 | + string nextLink = uriBuilder.ToString(); |
| 327 | + JToken response = null; |
| 328 | + |
| 329 | + while (!string.IsNullOrEmpty(nextLink)) |
| 330 | + { |
| 331 | + response = await SendAsync(nextLink, HttpMethod.Get, new Exception(string.Format(Properties.Resources.RetrievingStorageAccountIdUnderSubscriptionFailed, storageAccountName, storageAccountSubscriptionId))); |
| 332 | + nextLink = (string)response["nextLink"]; |
| 333 | + } |
| 334 | + |
| 335 | + JArray valuesArray = (JArray)response["value"]; |
| 336 | + if (!valuesArray.HasValues) |
| 337 | + { |
| 338 | + throw new Exception(string.Format(Properties.Resources.StorageAccountNotFound, storageAccountName)); |
| 339 | + } |
| 340 | + |
| 341 | + JToken idValueToken = valuesArray[0]; |
| 342 | + string id = (string)idValueToken["id"]; |
| 343 | + if (string.IsNullOrEmpty(id)) |
| 344 | + { |
| 345 | + throw new Exception(string.Format(Properties.Resources.RetrievingStorageAccountIdUnderSubscriptionFailed, storageAccountName, storageAccountSubscriptionId)); |
| 346 | + } |
| 347 | + |
| 348 | + return id; |
| 349 | + } |
| 350 | + |
| 351 | + /// <summary> |
| 352 | + /// Sends an async HTTP request. |
| 353 | + /// </summary> |
| 354 | + /// <param name="url">URL of the request.</param> |
| 355 | + /// <param name="method">Http method.</param> |
| 356 | + /// <param name="exceptionToThrowOnFailure">Exception to be thrown if request did not succeed.</param> |
| 357 | + /// <returns>Response of the request.</returns> |
| 358 | + private async Task<JToken> SendAsync(string url, HttpMethod method, Exception exceptionToThrowOnFailure) |
| 359 | + { |
| 360 | + ResourceManagementClient client = GetCurrentResourcesClient(Context); |
| 361 | + HttpRequestMessage httpRequest = new HttpRequestMessage(); |
| 362 | + httpRequest.Method = method; |
| 363 | + httpRequest.RequestUri = new Uri(url); |
| 364 | + await client.Credentials.ProcessHttpRequestAsync(httpRequest, CancellationToken.None).ConfigureAwait(false); |
| 365 | + HttpResponseMessage httpResponse = await client.HttpClient.SendAsync(httpRequest, CancellationToken.None).ConfigureAwait(false); |
| 366 | + if (!httpResponse.IsSuccessStatusCode) |
| 367 | + { |
| 368 | + throw exceptionToThrowOnFailure; |
| 369 | + } |
| 370 | + |
| 371 | + return JToken.Parse(await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); |
| 372 | + } |
237 | 373 | }
|
238 | 374 | }
|
0 commit comments