-1

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?

Thomas
  • 24,234
  • 6
  • 81
  • 125
Mark Allison
  • 6,838
  • 33
  • 102
  • 151
  • `if (!(hotToCoolDays == 0 || houseKeepDays == 0))` should work. Are you getting any error ? – Thomas Jun 01 '23 at 13:51
  • Question is where does that code go? If `hotToCoolDays ==0` then I don't want to add the rule. If `houseKeepDays ==0` I don't want to run that rule. As I said I already tried an if statement on the line preceding `enabled: true` – Mark Allison Jun 01 '23 at 14:28

1 Answers1

0

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
    }
  }
}
Mark Allison
  • 6,838
  • 33
  • 102
  • 151