如何在.NET中使用REST API创建Azure蓝图

约瑟夫

我想通过REST API创建Azure蓝图。有人可以就此澄清一下。我以为我们可以使用ASP.NET WEBAPI之类的REST API创建蓝图,但是我的假设似乎是错误的。我没有找到使用REST API .Net代码创建蓝图的任何解决方案。

即使在有关Azure蓝图的Microsoft官方文档中,他们也使用了powershell,但没有.NET代码来创建蓝图。

Jim Xu

根据我的测试,如果要Microsoft.Azure.Management.Blueprint在控制台应用程序中使用sdk来管理Azure蓝图,请参考以下步骤

  1. 创建服务主体
az login
# it will create a service pricipal and assign a contributor rolen to the sp
az ad sp create-for-rbac -n "MyApp"  --scope "/subscriptions/<subscription id>" --sdk-auth

在此处输入图片说明

private static string tenantId = "<the tenantId you copy >";
        private static string clientId = "<the clientId you copy>";
        private static string clientSecret= "<the clientSecre you copy>";
private static string subscriptionId = "<the subscription id you copy>";

 static async Task Main(string[] args){
    /*
      install sdk Microsoft.Azure.Management.Blueprint and Microsoft.Identity.Client

*/
   //get token
            var app = ConfidentialClientApplicationBuilder.Create(clientId)
                       .WithAuthority(AzureCloudInstance.AzurePublic, tenantId)
                       .WithClientSecret(clientSecret)
                       .Build();
            string[] scopes = { "https://management.azure.com/.default" };
            var result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
            TokenCredentials tokenCredentials = new TokenCredentials(result.AccessToken, "Bearer");
            BlueprintManagementClient client = new BlueprintManagementClient(tokenCredentials);
            var blueprint = new BlueprintModel()
            {

                DisplayName = "Sample Blueprint from Unittest",
                TargetScope = Constants.BlueprintTargetScopes.Subscription,
                ResourceGroups = new OrdinalStringDictionary<ResourceGroupDefinition>
                {
                    { "vNicResourceGroup", new ResourceGroupDefinition{ DisplayName="virtual-network-rg", Description = "All virutal network should save under this resource group." } }
                },
                Parameters = new OrdinalStringDictionary<ParameterDefinition>
                {
                    { "vNetName", new ParameterDefinition { Type = Constants.ParameterDefinitionTypes.String, DisplayName="default vNet for this subscription.", Description = "default vNet created by this blueprint.", DefaultValue = "defaultPublicVNet" } },
                    { "defaultLocation", new ParameterDefinition { Type = Constants.ParameterDefinitionTypes.String, DisplayName="default Location", Description = "default location of resource created by this blueprint.", DefaultValue = "East US" } },
                    { "defaultCostCenter", new ParameterDefinition { Type = Constants.ParameterDefinitionTypes.String, DisplayName="default CostCenter", Description = "default CostCenter for this subscription.", DefaultValue = "Contoso/IT/PROD/123456" } }
                }

            };

            // create 
            var blueprintName = "subBPfromDotnetSDK";
            Console.WriteLine("create Blueprint");
            await client.Blueprints.CreateOrUpdateInSubscriptionAsync(subscriptionId, blueprintName, blueprint);
            //get
            var blueprintGet = await client.Blueprints.GetInSubscriptionAsync(subscriptionId, blueprintName);

            Console.WriteLine(blueprintGet.DisplayName);
}

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章