|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using Microsoft.WindowsAzure.Storage; |
| 6 | +using Microsoft.WindowsAzure.Storage.Auth; |
| 7 | + |
| 8 | +namespace Microsoft.Extensions.HealthChecks |
| 9 | +{ |
| 10 | + // REVIEW: Do we want these to continue to use default parameters? |
| 11 | + // REVIEW: What are the appropriate guards for these functions? |
| 12 | + |
| 13 | + public static class AzureHealthCheckBuilderExtensions |
| 14 | + { |
| 15 | + public static HealthCheckBuilder AddAzureBlobStorageCheck(this HealthCheckBuilder builder, string accountName, string accountKey, string containerName = null, TimeSpan? cacheDuration = null) |
| 16 | + { |
| 17 | + var credentials = new StorageCredentials(accountName, accountKey); |
| 18 | + var storageAccount = new CloudStorageAccount(credentials, true); |
| 19 | + return AddAzureBlobStorageCheck(builder, storageAccount, containerName, cacheDuration); |
| 20 | + } |
| 21 | + |
| 22 | + public static HealthCheckBuilder AddAzureBlobStorageCheck(HealthCheckBuilder builder, CloudStorageAccount storageAccount, string containerName = null, TimeSpan? cacheDuration = null) |
| 23 | + { |
| 24 | + builder.AddCheck($"AzureBlobStorageCheck {storageAccount.BlobStorageUri} {containerName}", async () => |
| 25 | + { |
| 26 | + bool result; |
| 27 | + try |
| 28 | + { |
| 29 | + var blobClient = storageAccount.CreateCloudBlobClient(); |
| 30 | + |
| 31 | + var properties = await blobClient.GetServicePropertiesAsync().ConfigureAwait(false); |
| 32 | + |
| 33 | + if (!String.IsNullOrWhiteSpace(containerName)) |
| 34 | + { |
| 35 | + var container = blobClient.GetContainerReference(containerName); |
| 36 | + |
| 37 | + result = await container.ExistsAsync(); |
| 38 | + } |
| 39 | + |
| 40 | + result = true; |
| 41 | + } |
| 42 | + catch (Exception) |
| 43 | + { |
| 44 | + result = false; |
| 45 | + } |
| 46 | + |
| 47 | + return result |
| 48 | + ? HealthCheckResult.Healthy($"AzureBlobStorage {storageAccount.BlobStorageUri} is available") |
| 49 | + : HealthCheckResult.Unhealthy($"AzureBlobStorage {storageAccount.BlobStorageUri} is unavailable"); |
| 50 | + }, cacheDuration ?? builder.DefaultCacheDuration); |
| 51 | + |
| 52 | + return builder; |
| 53 | + } |
| 54 | + |
| 55 | + public static HealthCheckBuilder AddAzureTableStorageCheck(this HealthCheckBuilder builder, string accountName, string accountKey, string tableName = null, TimeSpan? cacheDuration = null) |
| 56 | + { |
| 57 | + var credentials = new StorageCredentials(accountName, accountKey); |
| 58 | + var storageAccount = new CloudStorageAccount(credentials, true); |
| 59 | + return AddAzureTableStorageCheck(builder, storageAccount, tableName, cacheDuration); |
| 60 | + } |
| 61 | + |
| 62 | + public static HealthCheckBuilder AddAzureTableStorageCheck(HealthCheckBuilder builder, CloudStorageAccount storageAccount, string tableName = null, TimeSpan? cacheDuration = null) |
| 63 | + { |
| 64 | + builder.AddCheck($"AzureTableStorageCheck {storageAccount.TableStorageUri} {tableName}", async () => |
| 65 | + { |
| 66 | + bool result; |
| 67 | + try |
| 68 | + { |
| 69 | + var tableClient = storageAccount.CreateCloudTableClient(); |
| 70 | + |
| 71 | + var properties = await tableClient.GetServicePropertiesAsync().ConfigureAwait(false); |
| 72 | + |
| 73 | + if (String.IsNullOrWhiteSpace(tableName)) |
| 74 | + { |
| 75 | + var table = tableClient.GetTableReference(tableName); |
| 76 | + |
| 77 | + result = await table.ExistsAsync(); |
| 78 | + } |
| 79 | + result = true; |
| 80 | + } |
| 81 | + catch (Exception) |
| 82 | + { |
| 83 | + result = false; |
| 84 | + } |
| 85 | + |
| 86 | + return result |
| 87 | + ? HealthCheckResult.Healthy($"AzureTableStorage {storageAccount.BlobStorageUri} is available") |
| 88 | + : HealthCheckResult.Unhealthy($"AzureTableStorage {storageAccount.BlobStorageUri} is unavailable"); |
| 89 | + |
| 90 | + }, cacheDuration ?? builder.DefaultCacheDuration); |
| 91 | + |
| 92 | + return builder; |
| 93 | + } |
| 94 | + |
| 95 | + public static HealthCheckBuilder AddAzureFileStorageCheck(this HealthCheckBuilder builder, string accountName, string accountKey, string shareName = null, TimeSpan? cacheDuration = null) |
| 96 | + { |
| 97 | + var credentials = new StorageCredentials(accountName, accountKey); |
| 98 | + var storageAccount = new CloudStorageAccount(credentials, true); |
| 99 | + return AddAzureFileStorageCheck(builder, storageAccount, shareName, cacheDuration); |
| 100 | + } |
| 101 | + |
| 102 | + public static HealthCheckBuilder AddAzureFileStorageCheck(HealthCheckBuilder builder, CloudStorageAccount storageAccount, string shareName = null, TimeSpan? cacheDuration = null) |
| 103 | + { |
| 104 | + builder.AddCheck($"AzureFileStorageCheck {storageAccount.FileStorageUri} {shareName}", async () => |
| 105 | + { |
| 106 | + bool result; |
| 107 | + try |
| 108 | + { |
| 109 | + var fileClient = storageAccount.CreateCloudFileClient(); |
| 110 | + |
| 111 | + var properties = await fileClient.GetServicePropertiesAsync().ConfigureAwait(false); |
| 112 | + |
| 113 | + if (!String.IsNullOrWhiteSpace(shareName)) |
| 114 | + { |
| 115 | + var share = fileClient.GetShareReference(shareName); |
| 116 | + |
| 117 | + result = await share.ExistsAsync(); |
| 118 | + } |
| 119 | + |
| 120 | + result = true; |
| 121 | + } |
| 122 | + catch (Exception) |
| 123 | + { |
| 124 | + result = false; |
| 125 | + } |
| 126 | + |
| 127 | + return result |
| 128 | + ? HealthCheckResult.Healthy($"AzureFileStorage {storageAccount.BlobStorageUri} is available") |
| 129 | + : HealthCheckResult.Unhealthy($"AzureFileStorage {storageAccount.BlobStorageUri} is unavailable"); |
| 130 | + }, cacheDuration ?? builder.DefaultCacheDuration); |
| 131 | + |
| 132 | + return builder; |
| 133 | + } |
| 134 | + |
| 135 | + public static HealthCheckBuilder AddAzureQueueStorageCheck(this HealthCheckBuilder builder, string accountName, string accountKey, string queueName = null, TimeSpan? cacheDuration = null) |
| 136 | + { |
| 137 | + var credentials = new StorageCredentials(accountName, accountKey); |
| 138 | + var storageAccount = new CloudStorageAccount(credentials, true); |
| 139 | + return AddAzureQueueStorageCheck(builder, storageAccount, queueName, cacheDuration); |
| 140 | + } |
| 141 | + |
| 142 | + public static HealthCheckBuilder AddAzureQueueStorageCheck(HealthCheckBuilder builder, CloudStorageAccount storageAccount, string queueName = null, TimeSpan? cacheDuration = null) |
| 143 | + { |
| 144 | + builder.AddCheck($"AzureQueueStorageCheck {storageAccount.QueueStorageUri} {queueName}", async () => |
| 145 | + { |
| 146 | + bool result; |
| 147 | + try |
| 148 | + { |
| 149 | + var queueClient = storageAccount.CreateCloudQueueClient(); |
| 150 | + |
| 151 | + var properties = await queueClient.GetServicePropertiesAsync().ConfigureAwait(false); |
| 152 | + |
| 153 | + if (String.IsNullOrWhiteSpace(queueName)) |
| 154 | + { |
| 155 | + var queue = queueClient.GetQueueReference(queueName); |
| 156 | + |
| 157 | + result = await queue.ExistsAsync(); |
| 158 | + } |
| 159 | + result = true; |
| 160 | + } |
| 161 | + catch (Exception) |
| 162 | + { |
| 163 | + result = false; |
| 164 | + } |
| 165 | + |
| 166 | + return result |
| 167 | + ? HealthCheckResult.Healthy($"AzureFileStorage {storageAccount.BlobStorageUri} is available") |
| 168 | + : HealthCheckResult.Unhealthy($"AzureFileStorage {storageAccount.BlobStorageUri} is unavailable"); |
| 169 | + |
| 170 | + }, cacheDuration ?? builder.DefaultCacheDuration); |
| 171 | + |
| 172 | + return builder; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments