Skip to content

Instantly share code, notes, and snippets.

@bruceharrison1984
Last active November 4, 2020 18:25
Show Gist options
  • Save bruceharrison1984/6344bb8ecdd2c805c31b067bf464bde6 to your computer and use it in GitHub Desktop.
Save bruceharrison1984/6344bb8ecdd2c805c31b067bf464bde6 to your computer and use it in GitHub Desktop.
Check if Azure Synapse is Online
using Azure.Identity;
using Azure.ResourceManager.Resources;
using MyFunctionProject.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace MyFunctionProject.Services
{
public interface IAzureResourceManagementApiService
{
Task<bool> IsSynapseOnlineAsync(CancellationToken cancellationToken);
}
public class AzureResources : IAzureResourceManagementApiService
{
private readonly IOptions<AzureConfig> _azureConfig;
private readonly ISqlConnectionService _connectionService;
private readonly ILogger<AzureResources> _logger;
public AzureResources(IOptions<AzureConfig> azureConfig, ISqlConnectionService connectionService, ILogger<AzureResources> logger)
{
_azureConfig = azureConfig;
_connectionService = connectionService;
_logger = logger;
}
public async Task<bool> IsSynapseOnlineAsync(CancellationToken cancellationToken)
{
var resourceClient = new ResourcesManagementClient(_azureConfig.Value.SubscriptionId, new DefaultAzureCredential());
var resp = await resourceClient.Resources.GetAsync(_azureConfig.Value.ResourceGroup, $"Microsoft.Sql/servers", _connectionService.DatabaseServerName, "databases", _connectionService.DatabaseName, "2020-08-01-preview", cancellationToken);
var props = (Dictionary<string, object>)resp.Value.Properties;
if (!props.ContainsKey("status"))
throw new Exception($"Querying Azure Resource Management for '{_connectionService.DatabaseServerName}/{_connectionService.DatabaseName}' did not return the expected response. 'status' key not found in resource properties.");
var status = (string)props["status"];
switch (status.ToLower())
{
case "online":
return true;
default:
{
_logger.LogWarning($"Synapse database '{_connectionService.DatabaseServerName}/{_connectionService.DatabaseName}' is not online, it is '{status}'");
return false;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment