-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/968/maintenance-listing
- Loading branch information
Showing
65 changed files
with
16,258 additions
and
3,380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package addons | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Scalingo/cli/config" | ||
"github.com/Scalingo/cli/utils" | ||
"github.com/Scalingo/go-scalingo/v6" | ||
"github.com/Scalingo/go-utils/errors/v2" | ||
) | ||
|
||
type UpdateAddonConfigOpts struct { | ||
MaintenanceWindowDay *string | ||
MaintenanceWindowHour *int | ||
} | ||
|
||
func UpdateConfig(ctx context.Context, app, addon string, options UpdateAddonConfigOpts) error { | ||
weekdayNameToWeekdayOrderMap := map[string]time.Weekday{ | ||
strings.ToLower(time.Sunday.String()): time.Sunday, | ||
strings.ToLower(time.Monday.String()): time.Monday, | ||
strings.ToLower(time.Tuesday.String()): time.Tuesday, | ||
strings.ToLower(time.Wednesday.String()): time.Wednesday, | ||
strings.ToLower(time.Thursday.String()): time.Thursday, | ||
strings.ToLower(time.Friday.String()): time.Friday, | ||
strings.ToLower(time.Saturday.String()): time.Saturday, | ||
} | ||
|
||
c, err := config.ScalingoClient(ctx) | ||
if err != nil { | ||
return errors.Notef(ctx, err, "get Scalingo client to update addon config") | ||
} | ||
|
||
// If addon does not contain a UUID, we consider it contains an addon type (e.g. MongoDB) | ||
if !strings.HasPrefix(addon, "ad-") { | ||
addon, err = utils.GetAddonUUIDFromType(ctx, app, addon) | ||
if err != nil { | ||
return errors.Notef(ctx, err, "fail to get the addon UUID based on its type") | ||
} | ||
} | ||
|
||
// fetching the current database maintenance window allows | ||
// to only overload the specified options | ||
db, err := c.DatabaseShow(ctx, app, addon) | ||
if err != nil { | ||
return errors.Notef(ctx, err, "get database information") | ||
} | ||
|
||
weekdayLocal, startingHourLocal := utils.ConvertDayAndHourToTimezone( | ||
time.Weekday(db.MaintenanceWindow.WeekdayUTC), | ||
db.MaintenanceWindow.StartingHourUTC, | ||
time.UTC, | ||
time.Local, | ||
) | ||
|
||
if options.MaintenanceWindowDay != nil { | ||
day, ok := weekdayNameToWeekdayOrderMap[strings.ToLower(*options.MaintenanceWindowDay)] | ||
if !ok { | ||
return errors.Notef(ctx, err, "invalid weekday '%s'", *options.MaintenanceWindowDay) | ||
} | ||
weekdayLocal = day | ||
} | ||
|
||
if options.MaintenanceWindowHour != nil { | ||
if *options.MaintenanceWindowHour < 0 || *options.MaintenanceWindowHour > 23 { | ||
return errors.Notef(ctx, err, "invalid starting hour '%d': it must be between 0 and 23", *options.MaintenanceWindowHour) | ||
} | ||
startingHourLocal = *options.MaintenanceWindowHour | ||
} | ||
|
||
weekdayUTC, startingHourUTC := utils.ConvertDayAndHourToTimezone(weekdayLocal, startingHourLocal, time.Local, time.UTC) | ||
|
||
_, err = c.DatabaseUpdateMaintenanceWindow(ctx, app, addon, scalingo.MaintenanceWindowParams{ | ||
WeekdayUTC: utils.IntPtr(int(weekdayUTC)), | ||
StartingHourUTC: utils.IntPtr(startingHourUTC), | ||
}) | ||
if err != nil { | ||
return errors.Notef(ctx, err, "update the database maintenance window") | ||
} | ||
|
||
fmt.Println("Addon config updated.") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package utils | ||
|
||
func Int64Ptr(r int64) *int64 { | ||
return &r | ||
} | ||
|
||
func IntPtr(r int) *int { | ||
return &r | ||
} | ||
|
||
func Float64Ptr(r float64) *float64 { | ||
return &r | ||
} | ||
|
||
func StringPtr(r string) *string { | ||
return &r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,57 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/Scalingo/go-scalingo/v6" | ||
) | ||
|
||
const ( | ||
TimeFormat = "2006/01/02 15:04:05" | ||
) | ||
|
||
func FormatMaintenanceWindowWithTimezone(maintenanceWindow scalingo.MaintenanceWindow, location *time.Location) string { | ||
weekdayUTC := time.Weekday(maintenanceWindow.WeekdayUTC) | ||
weekdayLocal, startingHourLocal := ConvertDayAndHourToTimezone( | ||
weekdayUTC, | ||
maintenanceWindow.StartingHourUTC, | ||
time.UTC, | ||
location, | ||
) | ||
|
||
return fmt.Sprintf("%ss at %02d:00 (%02d hours)", | ||
weekdayLocal.String(), | ||
startingHourLocal, | ||
maintenanceWindow.DurationInHour, | ||
) | ||
} | ||
|
||
func ConvertDayAndHourToTimezone(weekday time.Weekday, hour int, inputLocation *time.Location, outputLocation *time.Location) (time.Weekday, int) { | ||
newTimezoneDate := beginningOfWeek(time.Now().In(inputLocation)) | ||
|
||
newTimezoneDate = newTimezoneDate.AddDate(0, 0, int(weekday)-1) | ||
newTimezoneDate = newTimezoneDate.Add(time.Duration(hour) * time.Hour) | ||
|
||
newTimezoneDate = newTimezoneDate.In(outputLocation) | ||
|
||
return newTimezoneDate.Weekday(), newTimezoneDate.Hour() | ||
} | ||
|
||
func beginningOfWeek(t time.Time) time.Time { | ||
t = beginningOfDay(t) | ||
weekday := int(t.Weekday()) | ||
|
||
weekStartDayInt := int(time.Monday) | ||
|
||
if weekday < weekStartDayInt { | ||
weekday = weekday + 7 - weekStartDayInt | ||
} else { | ||
weekday = weekday - weekStartDayInt | ||
} | ||
return t.AddDate(0, 0, -weekday) | ||
} | ||
|
||
func beginningOfDay(t time.Time) time.Time { | ||
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) | ||
} |
Oops, something went wrong.