diff --git a/internal/services/datafactory/data_factory_trigger_schedule_resource.go b/internal/services/datafactory/data_factory_trigger_schedule_resource.go index bd1206813877..d233c873fd7d 100644 --- a/internal/services/datafactory/data_factory_trigger_schedule_resource.go +++ b/internal/services/datafactory/data_factory_trigger_schedule_resource.go @@ -53,6 +53,86 @@ func resourceDataFactoryTriggerSchedule() *pluginsdk.Resource { ValidateFunc: validate.DataFactoryName(), }, + "description": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "schedule": { + Type: pluginsdk.TypeList, + Optional: true, + MinItems: 1, + MaxItems: 1, + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + "days_of_month": { + Type: pluginsdk.TypeList, + Optional: true, + Elem: &pluginsdk.Schema{ + Type: pluginsdk.TypeInt, + ValidateFunc: validation.Any( + validation.IntBetween(1, 31), + validation.IntBetween(-31, -1), + ), + }, + }, + + "days_of_week": { + Type: pluginsdk.TypeList, + Optional: true, + MaxItems: 7, + Elem: &pluginsdk.Schema{ + Type: pluginsdk.TypeString, + ValidateFunc: validation.IsDayOfTheWeek(false), + }, + }, + + "hours": { + Type: pluginsdk.TypeList, + Optional: true, + Elem: &pluginsdk.Schema{ + Type: pluginsdk.TypeInt, + ValidateFunc: validation.IntBetween(0, 24), + }, + }, + + "minutes": { + Type: pluginsdk.TypeList, + Optional: true, + Elem: &pluginsdk.Schema{ + Type: pluginsdk.TypeInt, + ValidateFunc: validation.IntBetween(0, 60), + }, + }, + + "monthly": { + Type: pluginsdk.TypeList, + Optional: true, + MinItems: 1, + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + "weekday": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.IsDayOfTheWeek(false), + }, + + "week": { + Type: pluginsdk.TypeInt, + Optional: true, + ValidateFunc: validation.Any( + validation.IntBetween(1, 5), + validation.IntBetween(-5, -1), + ), + }, + }, + }, + }, + }, + }, + }, + // This time can only be represented in UTC. // An issue has been filed in the SDK for the timezone attribute that doesn't seem to work // https://github.com/Azure/azure-sdk-for-go/issues/6244 @@ -148,6 +228,7 @@ func resourceDataFactoryTriggerScheduleCreateUpdate(d *pluginsdk.ResourceData, m Recurrence: &datafactory.ScheduleTriggerRecurrence{ Frequency: datafactory.RecurrenceFrequency(d.Get("frequency").(string)), Interval: utils.Int32(int32(d.Get("interval").(int))), + Schedule: expandDataFactorySchedule(d.Get("schedule").([]interface{})), }, } @@ -176,6 +257,7 @@ func resourceDataFactoryTriggerScheduleCreateUpdate(d *pluginsdk.ResourceData, m Parameters: d.Get("pipeline_parameters").(map[string]interface{}), }, }, + Description: utils.String(d.Get("description").(string)), } if v, ok := d.GetOk("annotations"); ok { @@ -246,6 +328,10 @@ func resourceDataFactoryTriggerScheduleRead(d *pluginsdk.ResourceData, meta inte } d.Set("frequency", recurrence.Frequency) d.Set("interval", recurrence.Interval) + + if schedule := recurrence.Schedule; schedule != nil { + d.Set("schedule", flattenDataFactorySchedule(schedule)) + } } if pipelines := scheduleTriggerProps.Pipelines; pipelines != nil { @@ -262,6 +348,8 @@ func resourceDataFactoryTriggerScheduleRead(d *pluginsdk.ResourceData, meta inte if err := d.Set("annotations", annotations); err != nil { return fmt.Errorf("setting `annotations`: %+v", err) } + + d.Set("description", scheduleTriggerProps.Description) } return nil @@ -285,3 +373,65 @@ func resourceDataFactoryTriggerScheduleDelete(d *pluginsdk.ResourceData, meta in return nil } + +func expandDataFactorySchedule(input []interface{}) *datafactory.RecurrenceSchedule { + if len(input) == 0 || input[0] == nil { + return nil + } + value := input[0].(map[string]interface{}) + weekDays := make([]datafactory.DaysOfWeek, 0) + for _, v := range value["days_of_week"].([]interface{}) { + weekDays = append(weekDays, datafactory.DaysOfWeek(v.(string))) + } + monthlyOccurrences := make([]datafactory.RecurrenceScheduleOccurrence, 0) + for _, v := range value["monthly"].([]interface{}) { + value := v.(map[string]interface{}) + monthlyOccurrences = append(monthlyOccurrences, datafactory.RecurrenceScheduleOccurrence{ + Day: datafactory.DayOfWeek(value["weekday"].(string)), + Occurrence: utils.Int32(int32(value["week"].(int))), + }) + } + return &datafactory.RecurrenceSchedule{ + Minutes: utils.ExpandInt32Slice(value["minutes"].([]interface{})), + Hours: utils.ExpandInt32Slice(value["hours"].([]interface{})), + WeekDays: &weekDays, + MonthDays: utils.ExpandInt32Slice(value["days_of_month"].([]interface{})), + MonthlyOccurrences: &monthlyOccurrences, + } +} + +func flattenDataFactorySchedule(schedule *datafactory.RecurrenceSchedule) []interface{} { + if schedule == nil { + return []interface{}{} + } + value := make(map[string]interface{}) + if schedule.Minutes != nil { + value["minutes"] = utils.FlattenInt32Slice(schedule.Minutes) + } + if schedule.Hours != nil { + value["hours"] = utils.FlattenInt32Slice(schedule.Hours) + } + if schedule.WeekDays != nil { + weekDays := make([]interface{}, 0) + for _, v := range *schedule.WeekDays { + weekDays = append(weekDays, string(v)) + } + value["days_of_week"] = weekDays + } + if schedule.MonthDays != nil { + value["days_of_month"] = utils.FlattenInt32Slice(schedule.MonthDays) + } + if schedule.MonthlyOccurrences != nil { + monthlyOccurrences := make([]interface{}, 0) + for _, v := range *schedule.MonthlyOccurrences { + occurrence := make(map[string]interface{}) + occurrence["weekday"] = string(v.Day) + if v.Occurrence != nil { + occurrence["week"] = *v.Occurrence + } + monthlyOccurrences = append(monthlyOccurrences, occurrence) + } + value["monthly"] = monthlyOccurrences + } + return []interface{}{value} +} diff --git a/internal/services/datafactory/data_factory_trigger_schedule_resource_test.go b/internal/services/datafactory/data_factory_trigger_schedule_resource_test.go index d8ecbf3f3fc2..5223340bce35 100644 --- a/internal/services/datafactory/data_factory_trigger_schedule_resource_test.go +++ b/internal/services/datafactory/data_factory_trigger_schedule_resource_test.go @@ -56,6 +56,21 @@ func TestAccDataFactoryTriggerSchedule_complete(t *testing.T) { }) } +func TestAccDataFactoryTriggerSchedule_schedule(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_data_factory_trigger_schedule", "test") + r := TriggerScheduleResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.schedule(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func (t TriggerScheduleResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := azure.ParseAzureResourceID(state.ID) if err != nil { @@ -143,7 +158,7 @@ resource "azurerm_data_factory_trigger_schedule" "test" { data_factory_name = azurerm_data_factory.test.name resource_group_name = azurerm_resource_group.test.name pipeline_name = azurerm_data_factory_pipeline.test.name - + description = "test" pipeline_parameters = azurerm_data_factory_pipeline.test.parameters annotations = ["test5"] frequency = "Day" @@ -152,3 +167,52 @@ resource "azurerm_data_factory_trigger_schedule" "test" { } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, endTime) } + +func (TriggerScheduleResource) schedule(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-df-%d" + location = "%s" +} + +resource "azurerm_data_factory" "test" { + name = "acctestdf%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name +} + +resource "azurerm_data_factory_pipeline" "test" { + name = "acctest%d" + resource_group_name = azurerm_resource_group.test.name + data_factory_name = azurerm_data_factory.test.name + + parameters = { + test = "testparameter" + } +} + +resource "azurerm_data_factory_trigger_schedule" "test" { + name = "acctestdf%d" + data_factory_name = azurerm_data_factory.test.name + resource_group_name = azurerm_resource_group.test.name + pipeline_name = azurerm_data_factory_pipeline.test.name + + annotations = ["test1", "test2", "test3"] + + schedule { + days_of_month = [1, 2, 3] + days_of_week = ["Monday", "Tuesday"] + hours = [0, 12, 24] + minutes = [0, 30, 60] + monthly { + weekday = "Monday" + week = 1 + } + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger) +} diff --git a/website/docs/r/data_factory_trigger_schedule.html.markdown b/website/docs/r/data_factory_trigger_schedule.html.markdown index 0a2039482543..0d8f4a3216c6 100644 --- a/website/docs/r/data_factory_trigger_schedule.html.markdown +++ b/website/docs/r/data_factory_trigger_schedule.html.markdown @@ -53,18 +53,44 @@ The following arguments are supported: * `pipeline_name` - (Required) The Data Factory Pipeline name that the trigger will act on. +* `description` - (Optional) The Schedule Trigger's description. + +* `schedule` - (Optional) A `schedule` block as defined below, which further specifies the recurrence schedule for the trigger. A schedule is capable of limiting or increasing the number of trigger executions specified by the `frequency` and `interval` properties. + * `start_time` - (Optional) The time the Schedule Trigger will start. This defaults to the current time. The time will be represented in UTC. * `end_time` - (Optional) The time the Schedule Trigger should end. The time will be represented in UTC. * `interval` - (Optional) The interval for how often the trigger occurs. This defaults to 1. -* `frequency` - (Optional) The trigger freqency. Valid values include `Minute`, `Hour`, `Day`, `Week`, `Month`. Defaults to `Minute`. +* `frequency` - (Optional) The trigger frequency. Valid values include `Minute`, `Hour`, `Day`, `Week`, `Month`. Defaults to `Minute`. * `pipeline_parameters` - (Optional) The pipeline parameters that the trigger will act upon. * `annotations` - (Optional) List of tags that can be used for describing the Data Factory Schedule Trigger. +--- + +A `schedule` block supports the following: + +* `days_of_month` - (Optional) Day(s) of the month on which the trigger is scheduled. This value can be specified with a monthly frequency only. + +* `days_of_week` - (Optional) Days of the week on which the trigger is scheduled. This value can be specified only with a weekly frequency. + +* `hours` - (Optional) Hours of the day on which the trigger is scheduled. + +* `minutes` - (Optional) Minutes of the hour on which the trigger is scheduled. + +* `monthly` - (Optional) A `monthly` block as documented below, which specifies the days of the month on which the trigger is scheduled. The value can be specified only with a monthly frequency. + +--- + +A `monthly` block supports the following: + +* `weekday` - (Required) The day of the week on which the trigger runs. For example, a `monthly` property with a `weekday` value of `Sunday` means every Sunday of the month. + +* `week` - (Optional) The occurrence of the specified day during the month. For example, a `monthly` property with `weekday` and `week` values of `Sunday, -1` means the last Sunday of the month. + ## Attributes Reference The following attributes are exported: