skip to Main Content

I have this bicep code to apply two rules to a storage account policy on an Azure Storage Account:

@description('Name of the storage account')
param name string

@description('Number of days before moving from Hot to Cool storage')
param hotToCoolDays int

@description('Number of days to housekeep blob storage')
param houseKeepDays int

// the storage account to apply the policy to
resource stg 'Microsoft.Storage/storageAccounts@2021-08-01' existing = {
  name: name
}

resource sapolicy 'Microsoft.Storage/storageAccounts/managementPolicies@2021-02-01' = {
  name: '${name}/default'
  dependsOn: [
    stg
  ]
  properties: {
    policy: {
      rules: [ 
        {
          enabled: true
          name: 'hotToCoolAfter${hotToCoolDays}Days'
          type: 'Lifecycle'
          definition: {
            actions: {
              baseBlob: {
                tierToCool: {
                  daysAfterModificationGreaterThan: hotToCoolDays
                }
              }
            }
            filters: {
              blobTypes: [
                'blockBlob'
              ]
            }
          }
        }
        {
          enabled: true
          name: 'housekeepAfter${houseKeepDays}Days'
          type: 'Lifecycle'
          definition: {
            actions: {
              baseBlob: {
                delete: {
                  daysAfterModificationGreaterThan: houseKeepDays
                }
              }
            }
            filters: {
              blobTypes: [
                'blockBlob'
              ]
            }
          }
        }
      ]
    }
  }
}

I want to omit the rule if either variable hotToCoolDays or houseKeepDays is 0. I’ve tried using if statement and the ternary operator but can’t get either to work. Any ideas?

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out with

    @description('Number of days before moving from Cool to Cold storage')
    param coolToColdDays int = 0
    
    @description('Name of the storage account')
    param name string
    
    @description('Number of days before moving from Hot to Cool storage')
    param hotToCoolDays int = 0
    
    @description('Number of days to housekeep blob storage')
    param houseKeepDays int = 0
    
    var hotToCoolRule = [{
        name: 'hotToCoolRule'
        enabled: true
        type: 'Lifecycle'
        definition: {
          actions: {
            baseBlob: {
              tierToCool: {
                daysAfterModificationGreaterThan: hotToCoolDays
              }
            }
          }
          filters: {
            blobTypes: [
              'blockBlob'
            ]
          }
        }
      }]
    
      var coolToColdRule = [{
        name: 'coolToColdRule'
        enabled: true
        type: 'Lifecycle'
        definition: {
          actions: {
            baseBlob: {
              tierToCold: {
                daysAfterModificationGreaterThan: coolToColdDays
              }
            }
          }
          filters: {
            blobTypes: [
              'blockBlob'
            ]
          }
        }
      }]
    
    var houseKeepRule = [{
        name: 'housekeepRule'
        enabled: true
        type: 'Lifecycle'
        definition: {
          actions: {
            baseBlob: {
              delete: {
                daysAfterModificationGreaterThan: houseKeepDays
              }
            }
          }
          filters: {
            blobTypes: [
              'blockBlob'
            ]
          }
        }
      }]
    
    var rules = concat(
      (hotToCoolDays > 0) ? hotToCoolRule : [],
      // (coolToColdDays > 0) ? coolToColdRule : [], this line is commented out as it's not available yet (PREVIEW)
      (houseKeepDays > 0) ? houseKeepRule : []
    )
    
    resource stg 'Microsoft.Storage/storageAccounts@2021-08-01' existing = {
      name: name
    }
    
    resource sapolicy 'Microsoft.Storage/storageAccounts/managementPolicies@2022-09-01' = if (! empty(rules)) {
      name: 'default'
      parent: stg
      properties: {
        policy: {
          rules: rules
        }
      }
    }
    

  2. You can check the documentation: Conditional deployment in Bicep.

    For you it should look like that:

    resource sapolicy 'Microsoft.Storage/storageAccounts/managementPolicies@2021-02-01' = if (!(hotToCoolDays == 0 || houseKeepDays == 0)) {
      ...
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search