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

condition makes the resource not included in template.json #5461

Open
santo2 opened this issue Dec 17, 2021 · 7 comments
Open

condition makes the resource not included in template.json #5461

santo2 opened this issue Dec 17, 2021 · 7 comments
Labels
bug Something isn't working

Comments

@santo2
Copy link

santo2 commented Dec 17, 2021

Bicep version
0.4.1008

Describe the bug
Given the following template, when param deployServerlessCosmosDb is true, then the container is not scripted into the json template. How can I reconstruct this to have it included without having to duplicate?

resource cosmosDbServer 'Microsoft.DocumentDB/databaseAccounts@2021-07-01-preview' = {
  kind: 'GlobalDocumentDB'
  name: cosmosDbServerName
  location: resourceGroup().location
  tags: tags
  properties: {
    createMode: 'Default'
    locations: [
      {
        locationName: resourceGroup().location
        failoverPriority: 0
      }
    ]
    enableAnalyticalStorage: shouldEnableAnalyticalStorage
    analyticalStorageConfiguration: cosmosDbServer_AnalyticalStorageConfiguration
    databaseAccountOfferType: 'Standard'
    consistencyPolicy: {
      defaultConsistencyLevel: 'Session'
      maxIntervalInSeconds: 5
      maxStalenessPrefix: 100
    }
    diagnosticLogSettings: {
      enableFullTextQuery: 'None'
    }
    capabilities: cosmosDbServer_capabilites
  }
}

resource PassDb 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2021-07-01-preview' = {
  parent: cosmosDbServer
  name: 'PassDb'
  properties: {
    resource: {
      id: 'PassDb'
    }
  }
}

resource QPDB 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2021-07-01-preview' = if(!deployServerlessCosmosDb) {
  parent: cosmosDbServer
  name: 'QPDB'
  properties: {
    resource: {
      id: 'QPDB'
    }
  }
}

resource container_ActorColdStorage 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-07-01-preview' = {
  parent: deployServerlessCosmosDb? PassDb: QPDB
  name: 'ActorColdStorage'
  properties: {
    resource: {
      id: 'ActorColdStorage'
      partitionKey: {
        paths: [
          '/Type'
        ]
        kind: 'Hash'
      }
      conflictResolutionPolicy: {
        mode: 'LastWriterWins'
        conflictResolutionPath: '/_ts'
      }
    }
  }
}
@ghost ghost added the Needs: Triage 🔍 label Dec 17, 2021
@alex-frankel
Copy link
Collaborator

I'm not sure if I am following what the issue is. Can you include the generated JSON and where you see a problem? Is this also causing an undesirable behavior in the deployment? IOW, even if the JSON does not look the way you expect, is the final deployment result incorrect/failing?

@santo2
Copy link
Author

santo2 commented Dec 22, 2021

Hi Alex, that will be hard, the code advanced already to make it work. I did however change the script so you can run the az bicep build command on the example.

the container resource has parent: deployServerlessCosmosDb? PassDb: QPDB
I expect because of this, and that there is an if(!deployServerlessCosmosDb) condition on the QPDB resource, the container resource won't get transpiled into ARM template.

Run az bicep build on template below, and the created JSON ARM template won't have the container resource.

var deployServerlessCosmosDb = true

resource cosmosDbServer 'Microsoft.DocumentDB/databaseAccounts@2021-07-01-preview' = {
  kind: 'GlobalDocumentDB'
  name: 'cosmosDbName'
  location: resourceGroup().location
  properties: {
    createMode: 'Default'
    locations: [
      {
        locationName: resourceGroup().location
        failoverPriority: 0
      }
    ]
    databaseAccountOfferType: 'Standard'
    consistencyPolicy: {
      defaultConsistencyLevel: 'Session'
      maxIntervalInSeconds: 5
      maxStalenessPrefix: 100
    }
    diagnosticLogSettings: {
      enableFullTextQuery: 'None'
    }
  }
}

resource PassDb 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2021-07-01-preview' = {
  parent: cosmosDbServer
  name: 'PassDb'
  properties: {
    resource: {
      id: 'PassDb'
    }
  }
}

resource QPDB 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2021-07-01-preview' = if(!deployServerlessCosmosDb) {
  parent: cosmosDbServer
  name: 'QPDB'
  properties: {
    resource: {
      id: 'QPDB'
    }
  }
}

resource container_ActorColdStorage 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-07-01-preview' = {
  parent: deployServerlessCosmosDb? PassDb: QPDB
  name: 'ActorColdStorage'
  properties: {
    resource: {
      id: 'ActorColdStorage'
      partitionKey: {
        paths: [
          '/Type'
        ]
        kind: 'Hash'
      }
      conflictResolutionPolicy: {
        mode: 'LastWriterWins'
        conflictResolutionPath: '/_ts'
      }
    }
  }
}

@alex-frankel
Copy link
Collaborator

Got it. I am able to repro. I'm actually surprised that this compiles without errors in the first place. I didn't think conditionally setting the parent was allowed because we need to be able to generate full resource IDs at the start of the deployment..

We will take a look.

@santo2
Copy link
Author

santo2 commented Dec 28, 2021

Ok, thanks for the response. Is there a reasonable solution without having to duplicate the collection?

@alex-frankel
Copy link
Collaborator

No, I think you will need two instances of the Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-07-01-preview resource and conditionally deploy those based on the value of deployServerlessCosmosDb

@alex-frankel alex-frankel added bug Something isn't working and removed Needs: Triage 🔍 labels Jan 12, 2022
@alex-frankel alex-frankel added this to the v0.5 milestone Jan 12, 2022
@anthony-c-martin
Copy link
Member

I think that this is most likely something we are missing validation for. We should either:

  1. See if we can support the scenario properly with codegen. Feels unlikely as generally a resource scope must be deterministic at deploy-time, but it may be possible to support.
  2. (If not) Block it with additional validation.

@stephaniezyen stephaniezyen modified the milestones: v0.5, v1.0 Mar 21, 2022
@stephaniezyen stephaniezyen moved this to Todo in Bicep Mar 24, 2022
@stephaniezyen stephaniezyen modified the milestones: v1.0, v0.7 Mar 30, 2022
@stephaniezyen stephaniezyen modified the milestones: v0.7, v0.8 Apr 22, 2022
@stephaniezyen stephaniezyen modified the milestones: v0.8, v0.7 Apr 22, 2022
@stephaniezyen stephaniezyen moved this from Todo to In Progress in Bicep May 19, 2022
@stephaniezyen stephaniezyen moved this from In Progress to In Review in Bicep Jun 2, 2022
@anthony-c-martin anthony-c-martin moved this from In Review to Blocked in Bicep Jun 7, 2022
@anthony-c-martin
Copy link
Member

Leaving this item open to track adding actual support for expressions in the parent property.

Created #7154 to track adding validation to block the unsafe behavior.

@alex-frankel alex-frankel modified the milestones: v0.7, v0.8 Jun 16, 2022
@alex-frankel alex-frankel modified the milestones: v0.8, v0.9 Jul 5, 2022
@stephaniezyen stephaniezyen modified the milestones: v0.9, v0.10 Aug 3, 2022
@anthony-c-martin anthony-c-martin removed their assignment Sep 8, 2022
@alex-frankel alex-frankel modified the milestones: v0.10, v0.11 Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Blocked
Development

Successfully merging a pull request may close this issue.

5 participants