Skip to content

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

@pczar1

Description

@pczar1

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions