MicrosoftDocs / azure-docs

Open source documentation of Microsoft Azure
https://docs.microsoft.com/azure
Creative Commons Attribution 4.0 International
10.22k stars 21.38k forks source link

Need sample to use "copy" syntax to create array of scalar values (instead of structures) #20735

Closed oleksandr-bilyk closed 5 years ago

oleksandr-bilyk commented 5 years ago

I need to construct array of integers. All what I may to create is array of structures like { "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "hostedServices": { "type": "array", "defaultValue": [ { "name": "name1", "httpPort": 8995 }, { "name": "name2", "httpPort": 8901 } ] } }, "variables": { "hostedServicesHttpPorts": { "copy": [ { "name": "hostedServicesHttpPorts", "count": "[length(parameters('hostedServices'))]", "input": { "value": "[parameters('hostedServices')[copyIndex('hostedServicesHttpPorts')].httpPort]" } } ] } }, "resources": [], "outputs": { "hostedServicesHttpPorts": { "value": "[variables('hostedServicesHttpPorts')]", "type": "object" } } }

How may I project input array of objects into array of integers? Following syntax is not correct:

{ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "hostedServices": { "type": "array", "defaultValue": [ { "name": "name1", "httpPort": 8995 }, { "name": "name2", "httpPort": 8901 } ] } }, "variables": { "hostedServicesHttpPorts": { "copy": [ { "name": "hostedServicesHttpPorts", "count": "[length(parameters('hostedServices'))]", "input": { "value": "[parameters('hostedServices')[copyIndex('hostedServicesHttpPorts')].httpPort]" } } ] } }, "resources": [], "outputs": { "hostedServicesHttpPorts": { "value": "[variables('hostedServicesHttpPorts')]", "type": "object" } } }


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

tfitzmac commented 5 years ago

@oleksandr-bilyk - it looks like you are trying to take a property from an array of objects, and create a new array from the values for that property. Is that right? I don't see an easy way to do that. If you know the number of elements in the array, you could use createarray and just hardcode the array index values.

Copy doesn't work because input is an object so you will always get the values wrapped in an object.

Can you pass in the parameter values differently? You could have the http ports in a separate parameter of type array.

oleksandr-bilyk commented 5 years ago

@tfitzmac Thank you for reply. Creating additional parameter will cause ports duplications because we need two duplicating structures

  1. list of objects with host, http, https ports for Public IP addresses, Load balancer rules and probes.
  2. Array of ports just for Networks Security group. Currently we already using such solution as you described. However, please, consider following features:
  3. Implement output not only array of objects but array of any type as well using syntax [parameters('hostedServices')[copyIndex('hostedServicesHttpPorts')].httpPort]"
  4. Implement special function that will make object array projection to get array of properties like C# Linq select e.g. [select(inputArray, 'propertyNameToProject'). It may be very useful feature!
tfitzmac commented 5 years ago

@bmoore-msft - do you know of a way to create an array of integers from one property on an array of objects? Copy doesn't seem to work because it creates an array of objects. createarray works but would require a hardcoded number of elements.

bmoore-msft commented 5 years ago

Unfortunately no good way to create a dynamic array in the language today... it's on the backlog.

tfitzmac commented 5 years ago

@oleksandr-bilyk - thanks for this example and feedback. At this point, there is not an update we can make to the documentation, but we will update it when the capabilities change.

tfitzmac commented 5 years ago

please-close

oleksandr-bilyk commented 5 years ago

Thank you @tfitzmac and @bmoore-msft