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

User defined type causes invalid resources loop generation #14365

Closed
yfakariya opened this issue Jun 18, 2024 · 2 comments
Closed

User defined type causes invalid resources loop generation #14365

yfakariya opened this issue Jun 18, 2024 · 2 comments
Labels
Needs: Author Feedback Awaiting feedback from the author of the issue

Comments

@yfakariya
Copy link

Bicep version

0.28.1

Describe the bug

Loop for user defined array should be treated as built-in array.
That is, following two bicep sources should be built as same:

param p arrray
resource r 'Microsoft.Storage/storageAccounts@2023-05-01' = [
  for item in p : {
    name: item.name
  }
]

and

param p object[]
resource r 'Microsoft.Storage/storageAccounts@2023-05-01' = [
  for item in p : {
    name: item.name
  }
]

should be built as identically except parameter type declaration.
However, 2nd source is built as invalid ARM template as following:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "languageVersion": "2.0",
  "contentVersion": "1.0.0.0",
  "metadata": {...},
  "parameters": {
    "p": {
      "type": "array",
      "items": {
        "type": "object"
      }
    }
  },
  "resources": {
    "r": {
      "copy": {
        "name": "r",
        "count": "[length(parameters('p'))]"
      },
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2023-05-01",
      "name": "[parameters('p')[copyIndex()].name]"
    }
  }
}

The resources must be array with objects as following:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {...},
  "parameters": {
    "p": {
      "type": "array"
    }
  },
  "resources": [
    {
      "copy": {
        "name": "r",
        "count": "[length(parameters('p'))]"
      },
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2023-05-01",
      "name": "[parameters('p')[copyIndex()].name]"
    }
  ]
}

More strangely, just declaring user defined array type causes such breaking for resources:

param p array
type objArray = object[]
resource r 'Microsoft.Storage/storageAccounts@2023-05-01' = [
  for item in p : {
    name: item.name
  }
]

It is built as:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "languageVersion": "2.0",
  "contentVersion": "1.0.0.0",
  "metadata": {...},
  "definitions": {
    "objArray": {
      "type": "array",
      "items": {
        "type": "object"
      }
    }
  },
  "parameters": {
    "p": {
      "type": "array"
    }
  },
  "resources": {
    "r": {
      "copy": {
        "name": "r",
        "count": "[length(parameters('p'))]"
      },
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2023-05-01",
      "name": "[parameters('p')[copyIndex()].name]"
    }
  }
}

It looks that declaration of user defined array causes this bug.

To Reproduce

Run az bicep build for above example, then check result json.

Additional context

(none)

@jeskew
Copy link
Contributor

jeskew commented Jun 18, 2024

Hi @yfakariya,

The example with a user-defined type is built as a "symbolic name" template, which requires that the value of the resources property be an object rather than an array. User-defined types in ARM require the use of language version 2.0, which in turn requires the "symbolic name" (object-based) syntax for resources.

Did you encounter any errors deploying the template compiled with user-defined types? I don't see any errors in the built ARM artifacts, but I may be missing something.

@jeskew jeskew added Needs: Author Feedback Awaiting feedback from the author of the issue and removed Needs: Triage 🔍 labels Jun 18, 2024
@yfakariya
Copy link
Author

@jeskew

Thank you for quick and detailed response. As you say, I missed out languageVersion and symbllic name. It was absolutely my misunderstanding. Sorry for noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Author Feedback Awaiting feedback from the author of the issue
Projects
Archived in project
Development

No branches or pull requests

2 participants