【问题标题】:How to create Azure Blueprint using REST API in .NET如何在 .NET 中使用 REST API 创建 Azure 蓝图
【发布时间】:2020-02-11 12:11:56
【问题描述】:

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

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

【问题讨论】:

标签: .net azure


【解决方案1】:

根据我的测试,如果你想在控制台应用中使用sdkMicrosoft.Azure.Management.Blueprint来管理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

  1. 代码
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);
}

【讨论】:

    猜你喜欢
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2020-05-06
    • 2023-03-14
    • 2021-01-11
    相关资源
    最近更新 更多