Search code examples
c#azure.net-coreazure-sql-databaseazure-sdk-.net

Azure ArmClient Rename & Copy DB Operations


A bit of background, I am looking to replace existing code in a C# App from the existing Microsoft.Azure.Management.Fluent (now deprecated) to the newer Azure.ResourceManager components.

Existing code to copy a database:

public async Task<bool> CopyDb(string? server, string? fromName, string? toName)
{
    _log.LogInformation("Connecting to Azure");
    var azure = GetAzureObject();

    var servers = await azure.SqlServers.ListAsync();
    var fromServer = servers.FirstOrDefault(f => server != null && server.Contains(f.Name));
    if (fromServer == null)
    {
        throw new InvalidOperationException("Unable to find original database server");
    }

    var toNameBackup = $"{toName}-Old";

    var existingDbs = await fromServer.Databases.ListAsync();

    var fromDB = existingDbs.FirstOrDefault(f => f.Name.Equals(fromName));
    if (fromDB == null)
    {
        throw new InvalidOperationException("Unable to find original database");
    }

    if (existingDbs.Any(a => a.Name.Equals(toNameBackup, StringComparison.OrdinalIgnoreCase)) 
        && existingDbs.Any(a => a.Name.Equals(toName, StringComparison.OrdinalIgnoreCase)))
    {
        _log.LogInformation("Deleting any existing backup called {0}", toNameBackup);
        await fromServer.Databases.DeleteAsync(toNameBackup);
    }

    if (existingDbs.Any(a => a.Name.Equals(toName, StringComparison.OrdinalIgnoreCase)))
    {
        _log.LogInformation("Renaming target database from {0} to {1} (if exists)", toName, toNameBackup);
        await (await fromServer.Databases.GetAsync(toName)).RenameAsync(toNameBackup);
    }

    _log.LogInformation("Copying database from from {0} to {1}", fromName, toName);
    var result = await fromServer.Databases.
        Define(toName).
        WithSourceDatabase(fromDB).
        WithMode(Microsoft.Azure.Management.Sql.Fluent.Models.CreateMode.Copy).CreateAsync();

    return result != null;
}

private Microsoft.Azure.Management.Fluent.IAzure GetAzureObject()
{
    var clientId = _configuration["AzureClientId"];
    var clientSecret = _configuration["AzureClientSecret"];
    var tenantId = _configuration["AzureTenantId"];
    var subscriptionId = _configuration["AzureSubscriptionId"];

    var credentials = Microsoft.Azure.Management.ResourceManager.Fluent.SdkContext.AzureCredentialsFactory.FromServicePrincipal(
        clientId: clientId,
        clientSecret: clientSecret,
        tenantId: tenantId,
        environment: Microsoft.Azure.Management.ResourceManager.Fluent.AzureEnvironment.AzureGlobalCloud);

    return Microsoft.Azure.Management.Fluent.Azure.Configure().Authenticate(credentials).WithSubscription(subscriptionId);
}

The newer components all work with resources and I've been struggling how to do a couple operations with the newer Azure.ArmClient. I've been able to query with it finding my SQL server and databases. I can even delete some DBs, but I'm unable to work out how to rename or copy databases like the above code. I know there are alternative ways to do this directly in SQL, but I'd prefer to see how to do it in code.

I have had a look around MS docs, I can only find information on the object definitions but no examples.

I have managed to get down to the point of renaming:-

var backupDb = fromServer.GetSqlDatabase(toName);
if (backupDb != null && backupDb.Value != null)
{
    // What do I pass in to the definition?
    var moveDefinition = new SqlResourceMoveDefinition()
    {
        // What to set here?
    };

    await (await backupDb.Value.GetAsync()).Value.RenameAsync(moveDefinition);
}

I'm not sure on how to define the SqlResourceMoveDefinition. I also can't work out at all how to perform the copy like in the older SDK.

Anyone have any guides on how to achieve these operations in C#?


Solution

  • Managed to work it out after eventually working from https://learn.microsoft.com/en-us/dotnet/azure/sdk/resource-management?tabs=PowerShell. There may be better ways to do this, and I'll edit the answer when I find them if others don't by then!

    public async Task<bool> CopyDb(string? server, string? fromName, string? toName)
    {
        _log.LogInformation("Connecting to Azure");
    
        var azure = GetAzureSubscription();
    
        var servers = azure.GetSqlServers().ToList();
    
        var fromServer = servers.SingleOrDefault(f => server != null && f.Data != null && server.Contains(f.Data.Name));
        if (fromServer == null)
        {
            throw new InvalidOperationException("Unable to find original database server");
        }
    
        var oldName = $"{toName}-Old";
        var databases = fromServer.GetSqlDatabases();
    
        _log.LogInformation("Check for any existing backup called {0}", oldName);
        if (await databases.ExistsAsync(oldName))
        {
            _log.LogInformation("Deleting for any existing backup called {0}", oldName);
            var oldBackup = await databases.GetAsync(oldName);
            await oldBackup.Value.DeleteAsync(WaitUntil.Completed);
        }
    
        _log.LogInformation("Check target database {0} exists", toName, oldName);
        if (await databases.ExistsAsync(toName))
        {
            _log.LogInformation("Renaming target database from {0} to {1}", toName, oldName);
            var toDbBackup = await databases.GetAsync(toName);
            var resourceIdString = toDbBackup.Value.Data.Id.Parent?.ToString();
            var newResourceId = new ResourceIdentifier($"{resourceIdString}/databases/{oldName}");
            var moveDefinition = new SqlResourceMoveDefinition(newResourceId);
            var toDb = await toDbBackup.Value.GetAsync();
            await toDb.Value.RenameAsync(moveDefinition);
        }
    
        _log.LogInformation("Copying database from from {0} to {1}", fromName, toName);
        var fromDb = await databases.GetAsync(fromName);
        var result = await databases.CreateOrUpdateAsync(WaitUntil.Completed, toName, fromDb.Value.Data);
        _log.LogInformation("Operation completed!");
    
        return result.HasValue;
    }
    
    private SubscriptionResource GetAzureSubscription()
    {
        var configValue = _configuration["AzureSubscriptionId"];
        var subscriptionId = new ResourceIdentifier($"/subscriptions/{configValue}");
        return GetAzureArmClient().GetSubscriptionResource(subscriptionId);
    }
    
    private ArmClient GetAzureArmClient()
    {
        var clientId = _configuration["AzureClientId"];
        var clientSecret = _configuration["AzureClientSecret"];
        var tenantId = _configuration["AzureTenantId"];
    
        var credentials = new ClientSecretCredential(
            clientId: clientId,
            clientSecret: clientSecret,
            tenantId: tenantId);
    
        return new ArmClient(credentials);
    }