简体   繁体   中英

How to validate ARM Template using azure .net SDK or Fluent API?

How to validate uploaded ARM Template using azure .net SDK or Fluent API? I want to validate my uploaded ARM template like azure portal do using azure .net SDK or Fluent API? For reference please see below image azure is showing message if ARM template not valid so same thing i want to implement using any .net API or REST API.

在此处输入图像描述

@Jim Below error I am getting:

在此处输入图像描述

If you want to validate your arm template, please refer to the following steps

  1. Create a service principal and assign Contributor role to the sp
az ad sp create-for-rbac -n "MyApp"
  1. Install Package
Install-Package Microsoft.Azure.Management.ResourceManager.Fluent -Version 1.34.0
  1. Code
 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) {
               // 
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM