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

Added changes to allow non-DTC values for name in existing resources. #6778

Merged
merged 5 commits into from
May 11, 2022

Conversation

davidcho23
Copy link
Contributor

@davidcho23 davidcho23 commented May 6, 2022

Fixes #3169

When the "name" property for an existing resource has a value that cannot be calculated at deploy time, the current behavior in Bicep is that we block it. However, upon further investigation, it makes more sense to allow the existing resource use this non-DTC (non-DeployTimeConstant) value, and also allow other existing resources in the template to reference this non-DTC value. However, it is also necessary to make sure this non-DTC flow is managed; a non-existing resource cannot reference/use this non-DTC value because non-existing resources must require a DTC value.

Take this for example:

resource newStg 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  name: 'test'
  kind: 'StorageV2'
  location: resourceGroup().location
  sku: {
    name: 'Standard_LRS'
  }
}

resource existingStg 'Microsoft.Storage/storageAccounts@2021-04-01' existing = {
  name: newStg.properties.accessTier
}

resource foo 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  name: existingStg.name
  kind: 'StorageV2'
  location: resourceGroup().location
  sku: {
    name: 'Standard_LRS'
  }
}

A resource newStg is created and has a string "test" as its name value. String literals are DTC values so this is allowed.
An existing resource is declared with the name referencing newStg.properties.accessTier. This value is not able to be calculated at deploy time, thus it is a non-DTC value. This is ok, as existing resources should be allowed to have a non-DTC value.

The problem lies with the resource foo that is declared with the existingStg.name. Because foo is not an existing resource, its "name" property must have a DTC value, like how newStg does. However, foo wants to use a non-DTC value, so we block it.

The fix
The fix here is that for all existing resources whose "name" property have a non-DTC value, remove the ReadableAtDeployTime flag from the "name" property. This flag is used to determine if a value is a DTC value or not. After removing the ReadableAtDeployTime flag, any existing resources that reference the value will know that they themselves must be non-DTC as well and also remove the ReadableAtDeployTime flag from their "name" property.

We also need to remove the DeployTimeConstant flag from these "name" properties of existing resources so that they are marked as non-DTC values.

Finally, an override method is needed in the DeployTimeConstantContainerVisitor. After our new logic, the DeployTimeConstantContainerVisitor picks up all the syntaxes as containers, and these containers are checked for DTC errors. In order to ensure that existing resources are not checked for non-DTC values and throw errors when it should not, the override method ensures that existing resources are not picked up as containers.

Microsoft Reviewers: Open in CodeFlow

@davidcho23 davidcho23 requested a review from shenglol May 6, 2022 22:55
Copy link
Contributor

@shenglol shenglol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

@davidcho23 davidcho23 merged commit 76684d4 into main May 11, 2022
@davidcho23 davidcho23 deleted the davidcho23/allowNameAssignmentForExistingResource branch May 11, 2022 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

existing resource name assignment blocks non-deploy-time-constants
2 participants