Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQ] Support objects in parameters.json file #296

Open
pim-simons opened this issue Oct 25, 2022 · 7 comments · Fixed by #297
Open

[FEATURE REQ] Support objects in parameters.json file #296

pim-simons opened this issue Oct 25, 2022 · 7 comments · Fixed by #297
Labels
enhancement New feature or request

Comments

@pim-simons
Copy link
Contributor

Please describe the feature or suggestion.

We are using quite a lot of objects in our parameters.json files to nicely combine several parameters, also described here, for example:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "infra": {
      "value": {
        "environment": {
          "resourcePrefix": "dummy",
          "resourceGroupName": "dummy"
        }
      }
    }
  }
}

We can then access that value from our Bicep file like this infra.environment.resourcePrefix.

When running the Template Analyzer tool this does not seem to be supported as I run into these kind of errors:
The template variable 'appSettings' is not valid: The language expression property 'environment' doesn't exist, available properties are 'property1'...

It seems like using objects in the parameters file are not supported, or am I doing something wrong? Searched within the documentation and issues but couldn't really find anything.
Would be great if this could be supported!

Additional context

Entire error:

Warning: An exception occurred when processing the template language expressions
Exception details:
Azure.Deployments.Templates.Exceptions.TemplateValidationException: The template variable 'appSettings' is not valid: The language expression property 'environment' doesn't exist, available properties are 'property1'.. Please see https://aka.ms/arm-template-expressions for usage details.
 ---> Azure.Deployments.Core.Exceptions.ExpressionException: The language expression property 'environment' doesn't exist, available properties are 'property1'.
   at Azure.Deployments.Expression.Expressions.FunctionExpression.SelectObjectProperty(JObject root, JToken token, TemplateErrorAdditionalInfo additionalInfo)
   at Azure.Deployments.Expression.Expressions.FunctionExpression.SelectProperty(JToken root, JToken property, TemplateErrorAdditionalInfo additionalInfo)
   at Azure.Deployments.Expression.Expressions.FunctionExpression.EvaluateExpression(ExpressionEvaluationContext context, TemplateErrorAdditionalInfo additionalInfo)
   at Azure.Deployments.Expression.Expressions.FunctionExpression.<>c__DisplayClass23_0.<EvaluateParameters>b__0(LanguageExpression parameter)
   at System.Array.ConvertAll[TInput,TOutput](TInput[] array, Converter`2 converter)
   at Microsoft.WindowsAzure.ResourceStack.Common.Extensions.IEnumerableExtensions.SelectArray[TSource,TElement](TSource[] source, Converter`2 elementSelector)
   at Azure.Deployments.Expression.Expressions.FunctionExpression.EvaluateParameters(ExpressionEvaluationContext context, TemplateErrorAdditionalInfo additionalInfo)
   at Azure.Deployments.Expression.Expressions.FunctionExpression.EvaluateExpression(ExpressionEvaluationContext context, TemplateErrorAdditionalInfo additionalInfo)
   at Azure.Deployments.Expression.Engines.ExpressionsEngine.EvaluateLanguageExpressionsInternal(JToken root, ExpressionEvaluationContext evaluationContext, Boolean ignoreExceptions, TemplateErrorAdditionalInfo additionalInfo, InsensitiveHashSet skipEvaluationPaths)
   at Azure.Deployments.Expression.Engines.ExpressionsEngine.EvaluateLanguageExpressionsRecursive(JToken root, ExpressionEvaluationContext evaluationContext, TemplateErrorAdditionalInfo additionalInfo, InsensitiveHashSet skipEvaluationPaths)
   at Azure.Deployments.Templates.Engines.TemplatePreprocessingEngine.ProcessTemplateVariableLanguageExpressionsRecursive(Template template, String variableName, InsensitiveDictionary`1 recursionState, ExpressionEvaluationContext evaluationContext, TemplateErrorAdditionalInfo additionalInfo)
   --- End of inner exception stack trace ---
   at Azure.Deployments.Templates.Engines.TemplatePreprocessingEngine.ProcessTemplateVariableLanguageExpressionsRecursive(Template template, String variableName, InsensitiveDictionary`1 recursionState, ExpressionEvaluationContext evaluationContext, TemplateErrorAdditionalInfo additionalInfo)
   at Azure.Deployments.Templates.Engines.TemplatePreprocessingEngine.ProcessTemplateVariablesLanguageExpressions(Template template, String apiVersion, InsensitiveDictionary`1 functionEvaluationOverwrites)
   at Azure.Deployments.Templates.Engines.TemplatePreprocessingEngine.ProcessTemplateLanguageExpressions(String managementGroupName, String subscriptionId, String resourceGroupName, Template template, String apiVersion, InsensitiveDictionary`1 functionEvaluationOverwrites)
   at Azure.Deployments.Templates.Engines.TemplateEngine.ProcessTemplateLanguageExpressions(String managementGroupName, String subscriptionId, String resourceGroupName, Template template, String apiVersion, InsensitiveDictionary`1 functionEvaluationOverwrites)
   at Microsoft.Azure.Templates.Analyzer.TemplateProcessor.ArmTemplateProcessor.ParseAndValidateTemplate(InsensitiveDictionary`1 parameters, InsensitiveDictionary`1 metadata) in C:\temp\template-analyzer-0.3.1\src\Analyzer.TemplateProcessor\ArmTemplateProcessor.cs:line 131
Warning: An exception occurred while evaluating the properties of a resource.
@pim-simons pim-simons added enhancement New feature or request 🔍 needs-triage labels Oct 25, 2022
@VeraBE
Copy link
Contributor

VeraBE commented Oct 26, 2022

@pim-simons, can you share a template that triggers an error message like that? I tried but couldn't reproduce it, this works fine:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "anObjectParam": {
        "type": "object",
        "defaultValue": {
          "environment": {
            "location": "aLocation"
          }
        }
      }
    },
    "resources": [
        {
            "type": "Microsoft.MachineLearningServices/workspaces",
            "name": "aWorkspace",
            "apiVersion": "2016-12-01",
            "location": "[parameters('anObjectParam').environment.location]",
            "properties": {}
        }
    ]
}

@JohnathonMohr
Copy link
Contributor

I believe the issue is when a template doesn't define a default value for the object. The value used during analysis is a mocked object, which will not have the expected properties during template expansion, and fail to parse.

@JohnathonMohr
Copy link
Contributor

@pim-simons a possible workaround is to analyze templates individually and specify the parameters file to use with -p on the command line. Auto-discovery of parameters when analyzing a directory isn't supported right now.

@pim-simons
Copy link
Contributor Author

I am indeed using a separate parameters json file.
@VeraBE here is an example of a bicep file and a separate parameters json file causing the issue:

@description('Provide the object that contains all info about the common infrastructure.')
param infra object

resource applicationInsight 'microsoft.insights/components@2020-02-02' = {
  name: 'test'
  location: infra.environment.location
  kind: 'other'
}
{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "infra": {
      "value": {
        "environment": {
          "location": "West Europe"
        }
      }
    }
  }
}

When I use the TemplateAnalyzer.exe analyze-directory command this fails with the error The language expression property 'environment' doesn't exist, available properties are 'property1'...
When I use the TemplateAnalyzer.exe analyze-template with the -p command then it works fine indeed 👍🏻:

Parameters File: D:\temp\template.parameters.json

        Rules passed: 0

Execution summary:
        The execution completed successfully

@JohnathonMohr thanks for the workaround!!
Is the auto-discovery of parameters when analyzing a directory on the roadmap?

@VeraBE
Copy link
Contributor

VeraBE commented Oct 27, 2022

@pim-simons, auto-discovery of parameters files is not a high priority for us yet

@pim-simons
Copy link
Contributor Author

@pim-simons, auto-discovery of parameters files is not a high priority for us yet

No worries, thanks for the heads up!
In the meantime, I figured I'd try to contribute and created a PR for this 😅

@pamelafox
Copy link
Member

I believe I've run into this issue or a similar issue, just want to confirm that I shouldn't open another issue.

Github workflow run:

https://github.com/pamelafox/simple-flask-server-example/actions/runs/4492404425/jobs/7902240790?pr=3

Errors on functions.bicep, which has this line:

param appSettings object = {}

This particular Bicep file isn't used in this infra, so there are no parameters being specified in a parameters.json. The template-analyzer errors with:

 Error: An exception occurred while analyzing template /home/runner/work/simple-flask-server-example/simple-flask-server-example/infra/core/host/functions.bicep
    Exception details:
    Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzerException: Error while evaluating rules.
     ---> Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzerException: Error while processing template.
     ---> Azure.Deployments.Templates.Exceptions.TemplateValidationException: Template parameter 'appSettings' was provided an invalid value. Expected a value of type 'Object', but received a value of type 'String'. Please see https://aka.ms/resource-manager-parameter-files for usage details.
       at Azure.Deployments.Templates.Engines.TemplateValidationEngine.ValidateParameterJTokenType(String parameterName, JTokenType parameterJTokenType, TemplateErrorAdditionalInfo additionalInfo, JTokenType[] expectedJTokenTypes)
       at Azure.Deployments.Templates.Engines.TemplateValidationEngine.ValidateParameterValue(String parameterName, TemplateParameter parameter)
       at Azure.Deployments.Templates.Engines.TemplateParameterizationEngine.ParameterizeInputParameter(String parameterName, TemplateInputParameter parameter, InsensitiveDictionary`1 inputParameters, ExpressionEvaluationContext evaluationContext, Boolean ignoreEvaluationExceptionsForWhatIf)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants