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

Deployment fails with incorrect segment lengths error when using the name property of the parent resource for a module to deploy its child object. #5269

Closed
doubleyouvdb opened this issue Nov 26, 2021 · 5 comments
Labels
Needs: Author Feedback Awaiting feedback from the author of the issue Needs: Triage 🔍 revisit

Comments

@doubleyouvdb
Copy link

doubleyouvdb commented Nov 26, 2021

Bicep version
Bicep CLI version 0.4.1 (e238759)
Bicep Visual Studio Code Plugin: v0.4.1008 Preview

Describe the bug
Deployment fails with incorrect segment lengths error when using the name property of the parent resource as a parameter for a module to deploy a child object.

An error occurred while deploying this offering. Error: 2021-11-25T11:56:09.2892079Z �[96m | �[91m11:56:08 - The deployment 'azDeploy-211125-1154-lz-cosmos-db' 2021-11-25T11:56:09.2892979Z �[96m | �[91mfailed with error(s). Showing 2 out of 2 error(s). Status 2021-11-25T11:56:09.2893872Z �[96m | �[91mMessage: At least one resource deployment operation failed. 2021-11-25T11:56:09.2894766Z �[96m | �[91mPlease list deployment operations for details. Please see 2021-11-25T11:56:09.2895697Z �[96m | �[91mhttps://aka.ms/DeployOperations for usage details. (Code: 2021-11-25T11:56:09.2896965Z �[96m | �[91mDeploymentFailed) - { "error": { "code": 2021-11-25T11:56:09.2897922Z �[96m | �[91m"InvalidTemplate", "message": "Deployment template 2021-11-25T11:56:09.2898792Z �[96m | �[91mvalidation failed: 'The template resource 2021-11-25T11:56:09.2918460Z �[96m | �[91m'sql-bdcaujwcntg7etnwr1/sql-bdcaujwcntg7etnwr1/documents/nothrougput' for type 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers' at line '1' and column '1585' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name. Please see https://aka.ms/arm-template/#resources for usage details.'.", "additionalInfo": [ { "type": "TemplateViolation", "info": { "lineNumber": 1, "linePosition": 1585, "path": "properties.template.resources[0].type" } } ] } } (Code:BadRequest) - { "error": { "code": "InvalidTemplate", "message": "Deployment template validation failed: 'The template resource 'sql-bdcaujwcntg7etnwr1/sql-bdcaujwcntg7etnwr1/documents/operations' for type 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers' at line '1' and column '1586' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name. Please see https://aka.ms/arm-template/#resources for usage details.'.", "additionalInfo": [ { "type": "TemplateViolation", "info": { "lineNumber": 1, "linePosition": 1586, "path": "properties.template.resources[0].type" } } ] } } (Code:BadRequest) Status Message: At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details. (Code: DeploymentFailed) - { "status": "Failed", "error": { "code": "ResourceDeploymentFailure", "message": "The resource operation completed with terminal provisioning state 'Failed'.", "details": [ { "code": "DeploymentFailed", "message": "At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.", "details": [ { "code": "Conflict", "message": "{ "status": "Failed", "error": { "code": "ResourceDeploymentFailure", "message": "The resource operation completed with terminal provisioning state 'Failed'.", "details": [ { "code": "DeploymentFailed", "message": "At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.", "details": [ { "code": "BadRequest", "message": "{ "error": { "code": "InvalidTemplate", "message": "Deployment template validation failed: 'The template resource 'sql-bdcaujwcntg7etnwr1/sql-bdcaujwcntg7etnwr1/documents/nothrougput' for type 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers' at line '1' and column '1585' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name. Please see https://aka.ms/arm-template/#resources for usage details.'.", "additionalInfo": [ { "type": "TemplateViolation", "info": { "lineNumber": 1, "linePosition": 1585, "path": "properties.template.resources[0].type" } } ] } }" }, { "code": "BadRequest", "message": "{ "error": { "code": "InvalidTemplate", "message": "Deployment template validation failed: 'The template resource 'sql-bdcaujwcntg7etnwr1/sql-bdcaujwcntg7etnwr1/documents/operations' for type 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers' at line '1' and column '1586' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name. Please see https://aka.ms/arm-template/#resources for usage details.'.", "additionalInfo": [ { "type": "TemplateViolation", "info": { "lineNumber": 1, "linePosition": 1586, "path": "properties.template.resources[0].type" } } ] } }" } ] } ] } }" } ] } ] } } (Code:Conflict) CorrelationId: a151ed8f-dff0-42ff-9257-34c96161d601

To Reproduce
In the parent bicep file, which calls the module for the child object:

resource sqlDatabaseRes 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2021-07-01-preview' = {
  name: '${cosmosDatabaseName}/${sqlDatabase.name}'
  location: resourceGroup().location
  tags: tags
  properties: {
    resource: {
      id: sqlDatabase.name
    }
    options: sqlDatabase.throughput == 0 ? null : sqlDatabase.autopilot ? {
      autoscaleSettings: {
        maxThroughput: sqlDatabase.throughput
      }
    } : {
      autoscaleSettings : {
        throughput: sqlDatabase.throughput
      }
    }
  }
}

module sqlDatabaseContainer 'sql-database-container.bicep' = [for (container, i) in sqlDatabase.containers: {
  name: 'sqlDatabaseContainer-${uniqueString(sqlDatabase.name, cosmosDatabaseName)}-${i}'
  params: {
    container: union(defaultContainerObject, container)
    cosmosDatabaseName: cosmosDatabaseName
    sqlDatabaseName: sqlDatabaseRes.name
    tags: tags
  }
}]

Content of the module for the child object:

param container object
param sqlDatabaseName string
param cosmosDatabaseName string
param tags object

resource containerRes 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-07-01-preview' = {
  name: '${cosmosDatabaseName}/${sqlDatabaseName}/${container.name}'
  location: resourceGroup().location
  tags: tags
  properties: {}

Deploying of the child object will result in the incorrect segment error above, as name will be actually '${cosmosDatabaseName}/${cosmosDatabaseName}/${sqlDatabase.name}' , which is incorrect.

To workaround
Workaround is to create additional existing resource 'cosmosAccount ' and adapting the name and adding the parent property in 'sqlDatabaseRes '. This way we can set sqlDatabaseRes.name as parameter sqlDatabaseName while also keeping the content the same in the module file.

resource cosmosAccount 'Microsoft.DocumentDB/databaseAccounts@2021-07-01-preview' existing = {
  name: cosmosDatabaseName
}

resource sqlDatabaseRes 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2021-07-01-preview' = {
  name: sqlDatabase.name
  parent: cosmosAccount
  location: resourceGroup().location
  tags: tags
  properties: {
    resource: {
      id: sqlDatabase.name
    }
    options: sqlDatabase.throughput == 0 ? null : sqlDatabase.autopilot ? {
      autoscaleSettings: {
        maxThroughput: sqlDatabase.throughput
      }
    } : {
      autoscaleSettings : {
        throughput: sqlDatabase.throughput
      }
    }
  }
}

module sqlDatabaseContainer 'sql-database-container.bicep' = [for (container, i) in sqlDatabase.containers: {
  name: 'sqlDatabaseContainer-${uniqueString(sqlDatabase.name, cosmosDatabaseName)}-${i}'
  params: {
    container: union(defaultContainerObject, container)
    cosmosDatabaseName: cosmosDatabaseName
    sqlDatabaseName: sqlDatabaseRes.name
    tags: tags
  }
}]
@ghost ghost added the Needs: Triage 🔍 label Nov 26, 2021
@slavizh
Copy link
Contributor

slavizh commented Nov 26, 2021

+1

@alex-frankel
Copy link
Collaborator

Segmented resource names (parentName/childName/grandChildname) are not recommended as it is difficult to validate and error prone. Technically, bicep is doing the right thing in this case since the name you are asking for (as bicep understands it) is the "full" name with segments.

Instead, we'd encourage you to always get a symbolic reference to the resource via the existing syntax whenever you are working with child or extension resources. It is a bit verbose to use existing, but we plan to make this easier with #2245/#2246.

@slavizh
Copy link
Contributor

slavizh commented Nov 30, 2021

@alex-frankel may be create linter rule for this as this breaks without people knowing it will. Returning different name value depending on the hierarchy of the resource is not consistent behavior.

@alex-frankel
Copy link
Collaborator

We agree that this is confusing.

We are actually planning to eventually deprecate support for /-segmented resource names to avoid this type of issue in the future. In order to do that, we need the resource() function (as part of #2245/#2246). At that point, we can/should create linter rules and quick fixes to move people to using a symbolic reference everywhere.

@ghost
Copy link

ghost commented May 24, 2023

Hi doubleyouvdb, this issue has been marked as stale because it was labeled as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thanks for contributing to bicep! 😄 🦾

@github-project-automation github-project-automation bot moved this to Todo in Bicep May 24, 2023
@ghost ghost closed this as completed Jun 3, 2023
@github-project-automation github-project-automation bot moved this from Todo to Done in Bicep Jun 3, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jul 3, 2023
@StephenWeatherford StephenWeatherford added Needs: Author Feedback Awaiting feedback from the author of the issue and removed awaiting response labels Oct 13, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs: Author Feedback Awaiting feedback from the author of the issue Needs: Triage 🔍 revisit
Projects
Archived in project
Development

No branches or pull requests

4 participants