【问题标题】:How to validate ARM Template using azure .net SDK or Fluent API?如何使用 azure .net SDK 或 Fluent API 验证 ARM 模板?
【发布时间】:2021-04-21 15:04:03
【问题描述】:

如何使用 azure .net SDK 或 Fluent API 验证上传的 ARM 模板? 我想使用 azure .net SDK 或 Fluent API 验证我上传的 ARM 模板(如 azure 门户网站)吗? 作为参考,请参阅下面的图片如果 ARM 模板无效,Azure 将显示消息,所以我想使用任何 .net API 或 REST API 实现相同的东西。

@Jim 我收到以下错误:

【问题讨论】:

标签: c# azure arm-template azure-sdk-.net


【解决方案1】:

如果你想验证你的arm模板,请参考以下步骤

  1. 创建服务主体并将贡献者角色分配给 sp
az ad sp create-for-rbac -n "MyApp"
  1. 安装包
Install-Package Microsoft.Azure.Management.ResourceManager.Fluent -Version 1.34.0
  1. 代码
 string clientId = "23****9c";
            string clientSecret = "?s****/k";
            string tenantDomain = "";
            string subscription = "";
            var creds= SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantDomain, AzureEnvironment.AzureGlobalCloud);
            var restClient = RestClient.Configure()
                .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
                .WithCredentials(creds)
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders)
                .Build();


            ResourceManagementClient managementClient = new ResourceManagementClient(restClient);
            managementClient.SubscriptionId = subscription;

            //Validates whether the specified template is syntactically correct and will be accepted by Azure Resource Manager..
            DeploymentValidateResultInner res = await managementClient.Deployments.ValidateAsync("<groupName>", "<deployName>", new DeploymentInner()
            {
                Location = "",
                Properties = new DeploymentProperties()
                {
                    ParametersLink = new ParametersLink("uri"),
                    TemplateLink = new TemplateLink("")
                }
            });

            Console.WriteLine(res.Error.Message);

            // get changes that will be made by the deployment if executed at the scope of resource group
            WhatIfOperationResultInner res1 = await  managementClient.Deployments.WhatIfAsync("<groupName>", "<deployName>", new DeploymentWhatIf() { 
                  Location="",
                   Properties= new DeploymentWhatIfProperties() {
                       ParametersLink = new ParametersLink("uri"),
                       TemplateLink = new TemplateLink("")
                   }
            });

            foreach (var change in res1.Changes) {
               // 
            }

【讨论】:

  • 感谢您的回复,我有 ARM 模板和参数文件,我直接传递的不是 ParametersLink , TemplateLink 但得到的请求内容无效,无法反序列化。
  • @DnyaneshSurya 能否请您详细提供错误信息?
  • 请求内容无效,无法反序列化:'错误转换值 "{"$schema":"schema.management.azure.com/schemas/2015-01-01/…{"type":"Microsoft.DevTestLab/schedules","apiVersion" :"2017-04-26-preview","name":"[ipient":"[parameters('autoShutdownNotificationEmail')]"},"targetResourceId":"[resourceId('Microsoft.Compute/virtualMachines', parameters( 'vmName'))]"}}]}" 输入'Azure.Deployments.Templates.Schema.Template'。路径'properties.template',第4行,位置1419。'。
  • 我已经更新了我的问题并附加了一张图片,以便您从中获取错误详细信息。
  • @DnyaneshSurya 请尝试将模板和参数定义为 Jobject。更多详情请参考gist.github.com/ramnov/2c3fae176435e3b5bff19a905657c6a2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
相关资源
最近更新 更多