Azure/template-analyzer

[BUG] - Error while processing template - An item with the same key has already been added

pczar1 opened this issue · 2 comments

pczar1 commented

Describe the bug

When running analyzer against following bicep template I'm getting 'An item with the same key has already been added' exception.

Bicep template:

@description('EventHub Namespace name')
param namespace string

@description('Specifies the Azure location for all resources.')
param location string = resourceGroup().location

@description('Specifies the messaging tier for Event Hub Namespace.')
@allowed([
  'Basic'
  'Standard'
])
param eventHubSku string = 'Standard'

param storageAccountName string

param additionalTags object = { ROLE_PURPOSE: 'Test Event Hub' }

@description('Capacity')
param capacity int = 1
param messageRetention int = 7

param hubs array

var eventHubNamespaceName = namespace

resource eventHubNamespace 'Microsoft.EventHub/namespaces@2022-10-01-preview' = {
  name: eventHubNamespaceName
  location: location
  tags: union(resourceGroup().tags, additionalTags)
  sku: {
    name: eventHubSku
    tier: eventHubSku
    capacity: capacity
  }
  properties: {
    isAutoInflateEnabled: false
    maximumThroughputUnits: 0
  }
}

resource eventHub 'Microsoft.EventHub/namespaces/eventhubs@2022-10-01-preview' = [for hub in hubs: {
  parent: eventHubNamespace
  name: hub.name
  properties: {
    messageRetentionInDays: messageRetention
    partitionCount: hub.partitionCount
  }
}]

resource consumerGroup 'Microsoft.EventHub/namespaces/eventhubs/consumergroups@2022-10-01-preview' = [for (hub, i) in hubs: {
  parent: eventHub[i]
  name: hub.consumerGroupName
  properties: {}
}]

resource container 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-01-01' = [for hub in hubs: {
  name: '${storageAccountName}/default/${hub.offsetContainerName}'
}]

Exception details:

Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzerException: Error while processing template.
 ---> System.ArgumentException: An item with the same key has already been added. Key: [format('{0}/{1}', variables('eventHubNamespaceName'), parameters('hubs')[copyIndex()].name)] Microsoft.EventHub/namespaces/eventhubs
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at Microsoft.Azure.Templates.Analyzer.TemplateProcessor.ArmTemplateProcessor.SaveFlattenedResources(TemplateResource[] resources, String parentName, String parentType, String parentExpandedPath)
   at Microsoft.Azure.Templates.Analyzer.TemplateProcessor.ArmTemplateProcessor.ProcessResourcesAndOutputs(Template template)
   at Microsoft.Azure.Templates.Analyzer.TemplateProcessor.ArmTemplateProcessor.ParseAndValidateTemplate(InsensitiveDictionary`1 parameters, InsensitiveDictionary`1 metadata)
   at Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzer.AnalyzeAllIncludedTemplates(String populatedTemplate, String parameters, String templateFilePath, TemplateContext parentContext, String pathPrefix)
   --- End of inner exception stack trace ---
   at Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzer.AnalyzeAllIncludedTemplates(String populatedTemplate, String parameters, String templateFilePath, TemplateContext parentContext, String pathPrefix)
   at Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzer.AnalyzeTemplate(String template, String templateFilePath, String parameters)
   at Microsoft.Azure.Templates.Analyzer.Cli.CommandLineParser.AnalyzeTemplate(TemplateAndParams templateAndParameters)

Expected behavior

Bicep template should be analyzed without throwing exception.

Reproduction Steps

  1. Run analyze-template command against bicep template attached in 'Describe the bug' section.

TemplateAnalyzer.exe analyze-template C:\eventhub.bicep -v

Environment

Template Analyzer Version: 0.5.2
.NET version: 7.0.102
IDE: Visual Studio 2022 (17.4.4)

nonik0 commented

Thanks for the input, we will investigate.

@pczar1
The error for you is caused by the 'hubs' array never being populated with any objects / data. If you do populate it like the example below, the error does not occur as you described. HOWEVER, there will be a new error relating to the tags property you are trying to use with the 'eventHubNamespace' resource.

param hubs array = [ { name: 'defaultName1' offsetContainerName: 'defaultOffsetContainerName1' consumerGroupName: 'defaultConsumerGroupName1' partitionCount: 2 } { name: 'defaultName2' offsetContainerName: 'defaultOffsetContainerName2' consumerGroupName: 'defaultConsumerGroupName2' partitionCount: 2 } ]

I came here because of an error with the same exception, however, after troubleshooting the code from @pczar1 in this issue, it is not the same. I will create a new issue that is more targeted. The issue I am experiencing is the TemplateAnlyzer failing to reference an object property when that object is passed to a bicep module. The error I'm experiencing is occurring when the anlayzer attempts to flatten the objects.