Azure Resource Management API调用是否有C#sdk / wrapper?

时间:2016-02-08 19:59:06

标签: c# api azure azure-resource-manager azure-sdk-.net

我想执行基于资源组的调用。例如: https://msdn.microsoft.com/en-us/library/azure/mt163572.aspx

Azure管理库似乎没有这种能力(除非我遗漏了一些东西)。是否有可用于进行此类呼叫的SDK或客户端包装器?

编辑: 高拉夫指出了我所需要的东西。为了帮助清除Azure资源管理API中泥泞的泥泞水域,我要做的就是为人们做一个坚实的工作。

在您应用的Packet Manager中执行以下操作: 安装包Microsoft.Azure.Management.Resources -Pre 然后 安装包Microsoft.Azure.Management.Compute -Pre 然后 安装包Microsoft.IdentityModel.Clients.ActiveDirectory -Pre

按照此博客获取授权标头/令牌: https://msdn.microsoft.com/en-us/library/azure/dn722415.aspx

然后像这样调用新的API(注意轻微的名称更改):

class Program
{
    static void Main(string[] args)
    {
        var token = GetAuthorizationHeader();
        var credential = new Microsoft.Rest.TokenCredentials(token);
        using (var client = new ComputeManagementClient(credential) { SubscriptionId = ConfigurationManager.AppSettings["subscriptionId"] })
        {
            var vms = client.VirtualMachines.ListAll();
        }
    }

    private static string GetAuthorizationHeader()
    {
        AuthenticationResult result = null;

        var context = new AuthenticationContext("https://login.windows.net/" + ConfigurationManager.AppSettings["tenantId"]);

        string clientId = ConfigurationManager.AppSettings["clientId"];
        string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
        ClientCredential clientCred = new ClientCredential(clientId, clientSecret);

        var thread = new Thread(() =>
        {
            result = context.AcquireToken(
              "https://management.core.windows.net/",
              clientCred);
        });

        thread.SetApartmentState(ApartmentState.STA);
        thread.Name = "AquireTokenThread";
        thread.Start();
        thread.Join();

        if (result == null)
        {
            throw new InvalidOperationException("Failed to obtain the JWT token");
        }

        string token = result.AccessToken;
        return token;
    }
}

1 个答案:

答案 0 :(得分:3)

我相信您正在寻找的套餐是Microsoft.Azure.Management.Resources 3.4.0-preview。您可以在此处找到Azure Resource Manager的完整源代码:https://github.com/Azure/azure-sdk-for-net/tree/master/src/ResourceManagement