Home > Blockchain >  Azure Bicep: setting a null value if the property doesn't exist
Azure Bicep: setting a null value if the property doesn't exist

Time:09-22

I am deploying schedules for the azure update management service through bicep. My code is as below:

param parSchedules array = [
  {
    name: 'mysched1'
    monthlyOccurrencesDay: null
    monthlyOccurrencesOccurence: null
    DaysOfWeek: 'Wednesday'
    StartTime: '${parBaseTimeForUpdateSchedules}T19:00:00'
    tag: {
      Update: [
        'tag1'
      ]
    }
  }
  {
    name: 'mysched2'
    monthlyOccurrencesDay: 'Wednesday'
    monthlyOccurrencesOccurence: 1
    DaysOfWeek: 'Wednesday'
    StartTime: '${parBaseTimeForUpdateSchedules}T07:00:00'
    tag: {
      Update: [
        'tag2'
      ]
    }
  }
]


resource resUpdateschedule 'Microsoft.Automation/automationAccounts/softwareUpdateConfigurations@2019-06-01' = [for schedule in parSchedules: {
  name: schedule.name
  parent: resAutomationAccount
  properties: {
    scheduleInfo: {
      advancedSchedule: {
        monthlyOccurrences: [
          {
            day: schedule.monthlyOccurrencesDay
            occurrence: schedule.monthlyOccurrencesOccurence
          }
        ]
        weekDays: [
          schedule.DaysOfWeek
        ]
      }
      description: ''
      frequency: 'Week'
      interval: 1
      isEnabled: true
      startTime: schedule.StartTime
      timeZone: 'UTC'
    }
    updateConfiguration: {
      duration: 'PT2H'
      operatingSystem: 'Windows'
      targets: {
        azureQueries: [
          {
            locations: [
              parLocation
            ]
            scope: [
              subscription().id
            ]
            tagSettings: {
              tags: schedule.tag
            }
          }
        ]
      }
      windows: {
        includedUpdateClassifications: 'Critical, Security'
        rebootSetting: 'IfRequired'
      }
    }
  }
}]

I am getting error with this because null value is not accepted for monthlyOccurrencesDay and monthlyOccurrencesOccurence. So what I want is to be able to use the same list containing different types of schedules (with and without monthlyOccurrencesOccurence) by looping the same resource. Like if the values of monthlyOccurrencesOccurence is null, it shouldn't take into consideration this property. Is that possible at all?

CodePudding user response:

You should be able to conditionally set the monthly occurence like that:

resource resUpdateschedule 'Microsoft.Automation/automationAccounts/softwareUpdateConfigurations@2019-06-01' = [for schedule in parSchedules: {
  name: schedule.name
  ...
  properties: {
    scheduleInfo: {
      advancedSchedule: {
        monthlyOccurrences: schedule.monthlyOccurrencesOccurence != null ? [
          {
            day: schedule.monthlyOccurrencesDay
            occurrence: schedule.monthlyOccurrencesOccurence
          }
        ] : []
        ...
      }
      ...
    }
    ...
  }
}]

  • Related