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

Inccorrect existing resource reference when using user defined types feature #9642

Closed
slavizh opened this issue Jan 25, 2023 · 5 comments
Closed
Labels
intermediate language Related to the intermediate language story: symbolic names

Comments

@slavizh
Copy link
Contributor

slavizh commented Jan 25, 2023

Bicep version
v0.13.1

Describe the bug
When using the following experimental feature:

"experimentalFeaturesEnabled": {
        "userDefinedTypes": true
    }

When referencing resource in another resource group incorrect resource is being referenced. For example, if I have defined existing resource like this:

resource monitorWorkspace 'Microsoft.Monitor/accounts@2021-06-03-preview' existing = {
  name: azureMonitorWorkspace.name
  scope: resourceGroup(azureMonitorWorkspace.subscriptionId, azureMonitorWorkspace.resourceGroup)
}

and using later monitorWorkspace.id on another property it tries to look for the resource in the resource group where deployment is happening rather in the resource group that is defined in azureMonitorWorkspace.resourceGroup
When looking at the generated ARM template the code is the following:

...
"resources": {
        "monitorWorkspace": {
            "type": "Microsoft.Monitor/accounts",
            "apiVersion": "2021-06-03-preview",
            "name": "[parameters('azureMonitorWorkspace').name]",
            "existing": true,
            "subscriptionId": "[parameters('azureMonitorWorkspace').subscriptionId]",
            "resourceGroup": "[parameters('azureMonitorWorkspace').resourceGroup]"
        },
        "ruleGroupRes": {
            ...
            "properties": {
               ....
                "scopes": [
                    "[resourceInfo('monitorWorkspace').id]"
                ]
                ...
            }
        }
    }

When you do deployment you can clearly see that what-if reports that scope value will be changed and the only difference is that the resource group name where the resource is located is the one where the deployment is happening not the one defined via input parameters. Same code works fine when experimental feature is not enabled.

To Reproduce
Steps to reproduce the behavior:

Additional context
Add any other context about the problem here.

@slavizh
Copy link
Contributor Author

slavizh commented Mar 28, 2023

@jeskew This seems fixed in latest bicep version. Not sure which change fixed it but I have tested it. ARM code that is generated now is:

"scopes": [
                          "[extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', parameters('azureMonitorWorkspace').subscriptionId, parameters('azureMonitorWorkspace').resourceGroup), 'Microsoft.Monitor/accounts', parameters('azureMonitorWorkspace').name)]"
                        ],

@jeskew
Copy link
Contributor

jeskew commented Mar 28, 2023

The resourceInfo function isn't used in code generation since #9458, but we still need to address the issue you ran into in the ARM runtime.

@alex-frankel alex-frankel modified the milestones: v0.16, v0.17 Apr 5, 2023
@alex-frankel alex-frankel modified the milestones: v0.17, v0.18 May 8, 2023
@github-project-automation github-project-automation bot moved this to Todo in Bicep May 8, 2023
@stephaniezyen stephaniezyen modified the milestones: v0.18, v0.19 May 24, 2023
@Kittoes0124
Copy link

Kittoes0124 commented May 31, 2023

A similar regression happens with the following pattern. Works just fine when UDTs are disabled, but blows up with "The language expression property 'networkSecurityGroup' doesn't exist" or "The language expression property 'publicIpAddresses' doesn't exist" when enabled.

param location string = resourceGroup().location
param name string
param properties object
param tags object = {}

var isNetworkSecurityGroupNotEmpty = !empty(properties.?networkInterface ?? {})
var resourceGroupName = resourceGroup().name
var subscriptionId = subscription().subscriptionId

resource networkInterface 'Microsoft.Network/networkInterfaces@2022-11-01' = {
  location: location
  name: name
  properties: {
    dnsSettings: {
      dnsServers: (properties.?dnsServers ?? [])
    }
    disableTcpStateTracking: !(properties.?isTcpStateTrackingEnabled ?? true)
    enableAcceleratedNetworking: (properties.?isAcceleratedNetworkingEnabled ?? true)
    enableIPForwarding: (properties.?isIpForwardingEnabled ?? null)
    ipConfigurations: [for (configuration, index) in properties.ipConfigurations: {
      name: (configuration.?name ?? index)
      properties: {
        primary: (configuration.?isPrimary ?? (0 == index))
        privateIPAddress: (configuration.privateIpAddress.?value ?? null)
        privateIPAddressVersion: (configuration.privateIpAddress.?version ?? 'IPv4')
        privateIPAllocationMethod: (contains(configuration.privateIpAddress, 'value') ? 'Static' : 'Dynamic')
        publicIPAddress: (empty(configuration.?publicIpAddress ?? {}) ? null : { id: publicIpAddresses[index].id })
        subnet: { id: subnets[index].id }
      }
    }]
    networkSecurityGroup: (isNetworkSecurityGroupNotEmpty ? { id: networkSecurityGroup.id } : null)
    nicType: 'Standard'
  }
  tags: tags
}
resource networkSecurityGroup 'Microsoft.Compute/availabilitySets@2023-03-01' existing = if (isNetworkSecurityGroupNotEmpty) {
  name: properties.networkSecurityGroup.name
  scope: resourceGroup((properties.networkSecurityGroup.?subscriptionId ?? subscriptionId), (properties.networkSecurityGroup.?resourceGroupName ?? resourceGroupName))
}
resource publicIpAddresses 'Microsoft.Network/publicIPAddresses@2022-11-01' existing = [for configuration in properties.ipConfigurations: {
  name: configuration.publicIpAddress.name
  scope: resourceGroup((configuration.publicIpAddress.?subscriptionId ?? subscriptionId), (configuration.publicIpAddress.?resourceGroupName ?? resourceGroupName))
}]
resource subnets 'Microsoft.Network/virtualNetworks/subnets@2022-11-01' existing = [for configuration in properties.ipConfigurations: {
  name: '${configuration.privateIpAddress.subnet.virtualNetworkName}/${configuration.privateIpAddress.subnet.name}'
  scope: resourceGroup((configuration.privateIpAddress.?subscriptionId ?? subscriptionId), (configuration.privateIpAddress.?resourceGroupName ?? resourceGroupName))
}]

@slavizh
Copy link
Contributor Author

slavizh commented Dec 1, 2023

@jeskew most likely can be closed?

@jeskew
Copy link
Contributor

jeskew commented Dec 1, 2023

A fix for the underlying issue with resourceInfo was included in the w48 ARM release.

@jeskew jeskew closed this as completed Dec 1, 2023
@github-project-automation github-project-automation bot moved this from In Review to Done in Bicep Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
intermediate language Related to the intermediate language story: symbolic names
Projects
Archived in project
Development

No branches or pull requests

6 participants