Azure ArmClient 重命名和复制数据库操作

问题描述 投票:0回答:2

一些背景知识,我希望将 C# 应用程序中的现有代码从现有的 Microsoft.Azure.Management.Fluent (现已弃用)替换为较新的 Azure.ResourceManager 组件。

复制数据库的现有代码:

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);
}

较新的组件都与资源一起工作,我一直在努力如何使用较新的Azure.ArmClient进行一些操作。我已经能够用它查询找到我的 SQL 服务器和数据库。我什至可以删除一些数据库,但我无法弄清楚如何像上面的代码那样重命名或复制数据库。我知道有其他方法可以直接在 SQL 中执行此操作,但我更愿意了解如何在代码中执行此操作。

我浏览了 MS 文档,只能找到有关对象定义的信息,但没有示例。

我已经成功地进行了重命名:-

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);
}

我不确定如何定义 SqlResourceMoveDefinition。我也根本不知道如何像旧版 SDK 那样执行复制。

有人有关于如何在 C# 中实现这些操作的指南吗?

c# azure .net-core azure-sql-database azure-sdk-.net
2个回答
1
投票

最终在https://learn.microsoft.com/en-us/dotnet/azure/sdk/resource-management?tabs=PowerShell工作后设法解决了这个问题。可能有更好的方法来做到这一点,当我找到它们时,如果其他人还没有找到答案,我会编辑答案!

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);
}

0
投票

虽然有点晚了,但我最近不得不研究一下我们正在实现的创建数据库副本的功能。我偶然发现了你的答案,它很有帮助,但它并没有完全创建数据库的精确副本。

只是想回复分享我在数据库副本中发现的内容(稍微扩展一下您的代码片段):

    _log.LogInformation("Copying database from from {0} to {1}", fromName, toName);
    var fromDb = await databases.GetAsync(fromName);
    SqlDatabaseData data = new SqlDatabaseData(new AzureLocation("East US"))
    {
        CreateMode = SqlDatabaseCreateMode.Copy,
        SourceDatabaseId = fromDb.Value.Id
    }
    var result = await databases.CreateOrUpdateAsync(WaitUntil.Completed, toName, data);
    _log.LogInformation("Operation completed!");

要获取更多示例,我可以找到 Azure 的 GitHub 存储库。供参考:https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/sqlmanagement/Azure.ResourceManager.Sql/samples/Generate/Samples/Sample_SqlDatabaseCollection.cs.

© www.soinside.com 2019 - 2024. All rights reserved.