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

fix: [CCM-14715] Modified days attribute in fixed schedule of AutoStopping #731

Merged
merged 14 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/731.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Days attribute of AutoStopping fixed schedule has been modified to be list of weekdays instead of string for brevity
```
2 changes: 1 addition & 1 deletion docs/data-sources/autostopping_schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ Data source for retrieving a fixed schedule for Harness AutoStopping rule

Read-Only:

- `days` (String)
- `days` (List of String)
- `end_time` (String)
- `start_time` (String)
18 changes: 9 additions & 9 deletions docs/resources/autostopping_schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ resource "harness_autostopping_schedule" "MondayWholeDayUp" {
time_zone = "UTC"

repeats {
days = "MON"
days = ["MON"]
}

rules = [123]
Expand All @@ -31,7 +31,7 @@ resource "harness_autostopping_schedule" "MondayUpTill4:30pm" {
time_zone = "UTC"

repeats {
days = "MON"
days = ["MON"]
end_time = "16:30"
}

Expand All @@ -44,7 +44,7 @@ resource "harness_autostopping_schedule" "MondayThroughFridayUptimeFrom9amTo6pm"
time_zone = "UTC"

repeats {
days = "MON, TUE, WED, THU, FRI"
days = ["MON", "TUE", "WED", "THU", "FRI"]
start_time = "09:00"
end_time = "18:00"
}
Expand All @@ -60,7 +60,7 @@ resource "harness_autostopping_schedule" "MondayThroughFridayUptimeFrom9amTo6pmS
starting_from = "2023-01-02 15:04:05"

repeats {
days = "MON, TUE, WED, THU, FRI"
days = ["MON", "TUE", "WED", "THU", "FRI"]
start_time = "09:00"
end_time = "18:00"
}
Expand All @@ -77,7 +77,7 @@ resource "harness_autostopping_schedule" "DownTimeForISTLuchOnEveryFridayInBetwe
ending_on = "2023-02-02 15:04:05"

repeats {
days = "FRI"
days = ["FRI"]
start_time = "12:30"
end_time = "14:30"
}
Expand All @@ -91,7 +91,7 @@ resource "harness_autostopping_schedule" "DowntimeEveryFridayAfterNoonTillEOD" {
time_zone = "Asia/Calcutta"

repeats {
days = "FRI"
days = ["FRI"]
start_time = "17:30"
}

Expand All @@ -104,7 +104,7 @@ resource "harness_autostopping_schedule" "CompleteDownTimeOnWeekEnd" {
time_zone = "UTC"

repeats {
days = "SUN, SAT"
days = ["SUN", "SAT"]
}

rules = [123]
Expand All @@ -126,7 +126,7 @@ resource "harness_autostopping_schedule" "OverNightDowntimeOnWeekDays" {
time_zone = "Asia/Calcutta"

repeats {
days = "MON, TUE, WED, THU, FRI"
days = ["MON", "TUE", "WED", "THU", "FRI"]
start_time = "18:00"
end_time = "07:00"
}
Expand Down Expand Up @@ -161,7 +161,7 @@ resource "harness_autostopping_schedule" "OverNightDowntimeOnWeekDays" {

Required:

- `days` (String) Days on which schedule need to be active. Comma separated values of `SUN`, `MON`, `TUE`, `WED`, `THU`, `FRI` and `SAT`. Eg : `MON,TUE,WED,THU,FRI` for Mon through Friday
- `days` (List of String) List of days on which schedule need to be active. Valid values are SUN, MON, TUE, WED, THU, FRI and SAT.

Optional:

Expand Down
16 changes: 8 additions & 8 deletions examples/resources/harness_autostopping_schedule/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resource "harness_autostopping_schedule" "MondayWholeDayUp" {
time_zone = "UTC"

repeats {
days = "MON"
days = ["MON"]
}

rules = [123]
Expand All @@ -16,7 +16,7 @@ resource "harness_autostopping_schedule" "MondayUpTill4:30pm" {
time_zone = "UTC"

repeats {
days = "MON"
days = ["MON"]
end_time = "16:30"
}

Expand All @@ -29,7 +29,7 @@ resource "harness_autostopping_schedule" "MondayThroughFridayUptimeFrom9amTo6pm"
time_zone = "UTC"

repeats {
days = "MON, TUE, WED, THU, FRI"
days = ["MON", "TUE", "WED", "THU", "FRI"]
start_time = "09:00"
end_time = "18:00"
}
Expand All @@ -45,7 +45,7 @@ resource "harness_autostopping_schedule" "MondayThroughFridayUptimeFrom9amTo6pmS
starting_from = "2023-01-02 15:04:05"

repeats {
days = "MON, TUE, WED, THU, FRI"
days = ["MON", "TUE", "WED", "THU", "FRI"]
start_time = "09:00"
end_time = "18:00"
}
Expand All @@ -62,7 +62,7 @@ resource "harness_autostopping_schedule" "DownTimeForISTLuchOnEveryFridayInBetwe
ending_on = "2023-02-02 15:04:05"

repeats {
days = "FRI"
days = ["FRI"]
start_time = "12:30"
end_time = "14:30"
}
Expand All @@ -76,7 +76,7 @@ resource "harness_autostopping_schedule" "DowntimeEveryFridayAfterNoonTillEOD" {
time_zone = "Asia/Calcutta"

repeats {
days = "FRI"
days = ["FRI"]
start_time = "17:30"
}

Expand All @@ -89,7 +89,7 @@ resource "harness_autostopping_schedule" "CompleteDownTimeOnWeekEnd" {
time_zone = "UTC"

repeats {
days = "SUN, SAT"
days = ["SUN", "SAT"]
}

rules = [123]
Expand All @@ -111,7 +111,7 @@ resource "harness_autostopping_schedule" "OverNightDowntimeOnWeekDays" {
time_zone = "Asia/Calcutta"

repeats {
days = "MON, TUE, WED, THU, FRI"
days = ["MON", "TUE", "WED", "THU", "FRI"]
start_time = "18:00"
end_time = "07:00"
}
Expand Down
91 changes: 37 additions & 54 deletions internal/service/platform/autostopping/schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,33 @@ func ResourceVMRule() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
daysAttribute: {
Description: "Days on which schedule need to be active. Comma separated values of `SUN`, `MON`, `TUE`, `WED`, `THU`, `FRI` and `SAT`. Eg : `MON,TUE,WED,THU,FRI` for Mon through Friday",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: daysValidationFunc,
Description: "List of days on which schedule need to be active. Valid values are SUN, MON, TUE, WED, THU, FRI and SAT.",
Required: true,
Type: schema.TypeList,
MinItems: 1,
MaxItems: 7,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics {
diags := diag.Diagnostics{}
iv, ok := i.(string)
if !ok {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Day should be string value. Valid values are SUN, MON, TUE, WED, THU, FRI and SAT",
})
return diags
}
if _, ok := dayIndex[iv]; !ok {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Valid values are SUN, MON, TUE, WED, THU, FRI and SAT",
})
return diags
}
return nil
},
},
},
startTimeAttribute: {
Description: "Starting time of schedule action on the day. Defaults to 00:00Hrs unless specified. Accepted format is HH:MM. Eg : 13:15 for 01:15pm",
Expand Down Expand Up @@ -229,48 +252,6 @@ func timeValidation(i interface{}, p cty.Path) diag.Diagnostics {
return diags
}

func daysValidationFunc(i interface{}, p cty.Path) diag.Diagnostics {
diags := diag.Diagnostics{}
v, ok := i.(string)
if !ok {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Value is mandatory and should be string",
})
return diags
}
parts := strings.Split(v, ",")
unique := map[string]struct{}{}
for _, p := range parts {
vp := strings.TrimSpace(p)
if _, checked := unique[vp]; checked {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Day `%s` repeats in days", vp),
})
return diags
}
unique[vp] = struct{}{}
match := false
for vd := range dayIndex {
match = match || strings.EqualFold(vp, vd)
}
if !match {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Valid input is comma separated values of `SUN`, `MON`, `TUE`, `WED`, `THU`, `FRI` and `SAT`. Eg : `MON,TUE,WED,THU,FRI` for Mon through Friday ",
})
}
}
if len(parts) < 1 || len(parts) > 7 {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "At-least one and at-most seven days can be specified",
})
}
return diags
}

func resourceScheduleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
schedule, diags := parseSchedule(d, c.AccountId)
Expand Down Expand Up @@ -363,14 +344,16 @@ func parseSchedule(d *schema.ResourceData, accountId string) (*nextgen.FixedSche
days := []float64{}
daysInf, ok := periodicityObj[daysAttribute]
if ok {
daysCsv, ok := daysInf.(string)
daysInf, ok := daysInf.([]interface{})
if ok {
dayParts := strings.Split(daysCsv, ",")
for _, dp := range dayParts {
dv := strings.TrimSpace(dp)
i, ok := dayIndex[strings.ToUpper(dv)]
for _, dayInf := range daysInf {
dp, ok := dayInf.(string)
if ok {
days = append(days, float64(i))
dv := strings.TrimSpace(dp)
i, ok := dayIndex[strings.ToUpper(dv)]
if ok {
days = append(days, float64(i))
}
}
}
}
Expand Down Expand Up @@ -522,7 +505,7 @@ func setSchedule(d *schema.ResourceData, schedule *nextgen.FixedSchedule) diag.D
}
identifier := strconv.Itoa(int(schedule.Id))
d.SetId(identifier)
d.Set("identifier", identifier)
d.Set("identifier", float64(schedule.Id))
d.Set(nameAttribute, schedule.Name)
scheduleType := uptimeSchedule
schedDet := schedule.Details.Uptime
Expand Down Expand Up @@ -567,7 +550,7 @@ func setSchedule(d *schema.ResourceData, schedule *nextgen.FixedSchedule) diag.D
periodicity[endTimeAttribute] = endTime
}
}
d.Set(repetitionAttribute, periodicity)
d.Set(repetitionAttribute, []interface{}{periodicity})
}
return diags
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ func DataSourceFixedSchedule() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
daysAttribute: {
Description: "Days on which schedule need to be active. Comma separated values of `SUN`, `MON`, `TUE`, `WED`, `THU`, `FRI` and `SAT`. Eg : `MON,TUE,WED,THU,FRI` for Mon through Friday",
Type: schema.TypeString,
Description: "List of days on which schedule need to be active. Valid values are SUN, MON, TUE, WED, THU, FRI and SAT.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
startTimeAttribute: {
Description: "Starting time of schedule action on the day. Accepted format is HH:MM. Eg : 13:15 for 01:15pm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func testSchedule(name string) string {
}

periodicity {
days = "MON, THU, TUE"
days = ["MON", "THU", "TUE"]
start_time = "09:30"
end_time = "16:50"
}
Expand Down