c# - 如何使用 azure .net SDK 或 Fluent API 验证 ARM 模板?

标签 c# azure azure-rm-template azure-sdk-.net

如何使用 azure .net SDK 或 Fluent API 验证上传的 ARM 模板? 我想像 azure 门户一样使用 azure .net SDK 或 Fluent API 验证我上传的 ARM 模板? 作为引用,请参阅下图,如果 ARM 模板无效,azure 将显示消息,因此我想使用任何 .net API 或 REST API 实现同样的事情。

enter image description here

@Jim 我收到以下错误:

enter image description here

最佳答案

如果您想验证您的 ARM 模板,请引用以下步骤

  1. 创建服务主体并将贡献者角色分配给 sp
az ad sp create-for-rbac -n "MyApp"
  • 安装包
  • Install-Package Microsoft.Azure.Management.ResourceManager.Fluent -Version 1.34.0
    
  • 代码
  •  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) {
                   // 
                }
    

    关于c# - 如何使用 azure .net SDK 或 Fluent API 验证 ARM 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67198474/

    相关文章:

    c# - 转换导致不同的 GetType

    c# - SQL盲注

    c# - 使用 XSD 正确验证 XML 文档

    android - 如何修复 Module 依赖于一个或多个 Android Libraries 但在构建 Cordova App 时是一个 Jar

    Azure 资源模板 : language expression is not supported

    c# - 使用 yield 的方法不允许调用自己

    asp.net - ARM 对 Azure WebApp 的 IP 限制(Azure 资源管理器)

    azure - Bicep 模块引用作为另一个资源中的父级

    azure - 使用New-AzureRmDeployment和链接的ARM模板创建资源组和资源时如何获取资源组名称?

    azure - Remove-AzureRmRouteConfig 不能像 Get-AzureRmRouteConfig 那样与 where-object 一起使用