Skip to content

Commit 1ffc75d

Browse files
skyfox149marco
authored andcommitted
feat: add backup schedule for shared databases
1 parent c23dc90 commit 1ffc75d

File tree

6 files changed

+39
-11
lines changed

6 files changed

+39
-11
lines changed

create/mysqldatabase.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import (
1717

1818
type mysqlDatabaseCmd struct {
1919
resourceCmd
20-
Location meta.LocationName `placeholder:"${mysqldatabase_location_default}" help:"Where the MySQL database is created. Available locations are: ${mysqldatabase_location_options}"`
21-
MysqlDatabaseVersion storage.MySQLVersion `placeholder:"${mysqldatabase_version_default}" help:"Version of the MySQL database. Available versions: ${mysqldatabase_versions}"`
22-
CharacterSet string `placeholder:"${mysqldatabase_characterset_default}" help:"Character set for the MySQL database. Available character sets: ${mysqldatabase_characterset_options}"`
20+
Location meta.LocationName `placeholder:"${mysqldatabase_location_default}" help:"Where the MySQL database is created. Available locations are: ${mysqldatabase_location_options}"`
21+
MysqlDatabaseVersion storage.MySQLVersion `placeholder:"${mysqldatabase_version_default}" help:"Version of the MySQL database. Available versions: ${mysqldatabase_versions}"`
22+
CharacterSet string `placeholder:"${mysqldatabase_characterset_default}" help:"Character set for the MySQL database. Available character sets: ${mysqldatabase_characterset_options}"`
23+
BackupSchedule storage.DatabaseBackupScheduleCalendar `placeholder:"${mysqldatabase_backupschedule_default}" help:"Backup schedule for the MySQL database. Available schedules: ${mysqldatabase_backupschedule_options}"`
2324
}
2425

2526
func (cmd *mysqlDatabaseCmd) Run(ctx context.Context, client *api.Client) error {
@@ -76,6 +77,7 @@ func (cmd *mysqlDatabaseCmd) newMySQLDatabase(namespace string) *storage.MySQLDa
7677
CharacterSet: storage.MySQLCharacterSet{
7778
Name: cmd.CharacterSet,
7879
},
80+
BackupSchedule: cmd.BackupSchedule,
7981
},
8082
},
8183
}
@@ -93,6 +95,8 @@ func MySQLDatabaseKongVars() kong.Vars {
9395
result["mysqldatabase_versions"] = strings.Join(stringSlice(storage.MySQLDatabaseVersions), ", ")
9496
result["mysqldatabase_characterset_default"] = "utf8mb4"
9597
result["mysqldatabase_characterset_options"] = strings.Join([]string{"utf8mb4"}, ", ")
98+
result["mysqldatabase_backupschedule_default"] = string(storage.DatabaseBackupScheduleCalendarDaily)
99+
result["mysqldatabase_backupschedule_options"] = strings.Join(stringSlice(storage.DatabaseBackupScheduleCalendars), ", ")
96100

97101
return result
98102
}

create/postgresdatabase.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import (
1717

1818
type postgresDatabaseCmd struct {
1919
resourceCmd
20-
Location meta.LocationName `placeholder:"${postgresdatabase_location_default}" help:"Where the PostgreSQL database is created. Available locations are: ${postgresdatabase_location_options}"`
21-
PostgresDatabaseVersion storage.PostgresVersion `placeholder:"${postgresdatabase_version_default}" help:"Release version with which the PostgreSQL database is created. Available versions: ${postgresdatabase_versions}"`
20+
Location meta.LocationName `placeholder:"${postgresdatabase_location_default}" help:"Where the PostgreSQL database is created. Available locations are: ${postgresdatabase_location_options}"`
21+
PostgresDatabaseVersion storage.PostgresVersion `placeholder:"${postgresdatabase_version_default}" help:"Release version with which the PostgreSQL database is created. Available versions: ${postgresdatabase_versions}"`
22+
BackupSchedule storage.DatabaseBackupScheduleCalendar `placeholder:"${postgresdatabase_backupschedule_default}" help:"Backup schedule for the PostgreSQL database. Available schedules: ${postgresdatabase_backupschedule_options}"`
2223
}
2324

2425
func (cmd *postgresDatabaseCmd) Run(ctx context.Context, client *api.Client) error {
@@ -73,8 +74,9 @@ func (cmd *postgresDatabaseCmd) newPostgresDatabase(namespace string) *storage.P
7374
},
7475
},
7576
ForProvider: storage.PostgresDatabaseParameters{
76-
Location: cmd.Location,
77-
Version: cmd.PostgresDatabaseVersion,
77+
Location: cmd.Location,
78+
Version: cmd.PostgresDatabaseVersion,
79+
BackupSchedule: cmd.BackupSchedule,
7880
},
7981
},
8082
}
@@ -90,6 +92,8 @@ func PostgresDatabaseKongVars() kong.Vars {
9092
result["postgresdatabase_location_options"] = strings.Join(stringSlice(storage.PostgresDatabaseLocationOptions), ", ")
9193
result["postgresdatabase_version_default"] = string(storage.PostgresDatabaseVersionDefault)
9294
result["postgresdatabase_versions"] = strings.Join(stringSlice(storage.PostgresDatabaseVersions), ", ")
95+
result["postgresdatabase_backupschedule_default"] = string(storage.DatabaseBackupScheduleCalendarDaily)
96+
result["postgresdatabase_backupschedule_options"] = strings.Join(stringSlice(storage.DatabaseBackupScheduleCalendars), ", ")
9397

9498
return result
9599
}

update/mysqldatabase.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
type mysqlDatabaseCmd struct {
1414
resourceCmd
15+
BackupSchedule *storage.DatabaseBackupScheduleCalendar `help:"Backup schedule for the MySQL database. Available schedules: ${mysqldatabase_backupschedule_options}"`
1516
}
1617

1718
func (cmd *mysqlDatabaseCmd) Run(ctx context.Context, client *api.Client) error {
@@ -35,6 +36,8 @@ func (cmd *mysqlDatabaseCmd) Run(ctx context.Context, client *api.Client) error
3536
return upd.Update(ctx)
3637
}
3738

38-
func (cmd *mysqlDatabaseCmd) applyUpdates(_ *storage.MySQLDatabase) {
39-
cmd.Warningf("there are no attributes for mysqldatabase which can be updated after creation. Applying update without any changes.")
39+
func (cmd *mysqlDatabaseCmd) applyUpdates(db *storage.MySQLDatabase) {
40+
if cmd.BackupSchedule != nil {
41+
db.Spec.ForProvider.BackupSchedule = *cmd.BackupSchedule
42+
}
4043
}

update/mysqldatabase_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/ninech/nctl/api"
1111
"github.com/ninech/nctl/internal/format"
1212
"github.com/ninech/nctl/internal/test"
13+
"k8s.io/utils/ptr"
1314
)
1415

1516
func TestMySQLDatabase(t *testing.T) {
@@ -30,6 +31,12 @@ func TestMySQLDatabase(t *testing.T) {
3031
update: mysqlDatabaseCmd{},
3132
wantErr: false,
3233
},
34+
{
35+
name: "update-backup-schedule",
36+
create: storage.MySQLDatabaseParameters{BackupSchedule: storage.DatabaseBackupScheduleCalendarDaily},
37+
update: mysqlDatabaseCmd{BackupSchedule: ptr.To(storage.DatabaseBackupScheduleCalendarDisabled)},
38+
want: storage.MySQLDatabaseParameters{BackupSchedule: storage.DatabaseBackupScheduleCalendarDisabled},
39+
},
3340
}
3441
for _, tt := range tests {
3542
t.Run(tt.name, func(t *testing.T) {

update/postgresdatabase.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
type postgresDatabaseCmd struct {
1414
resourceCmd
15+
BackupSchedule *storage.DatabaseBackupScheduleCalendar `help:"Backup schedule for the PostgreSQL database. Available schedules: ${postgresdatabase_backupschedule_options}"`
1516
}
1617

1718
func (cmd *postgresDatabaseCmd) Run(ctx context.Context, client *api.Client) error {
@@ -35,6 +36,8 @@ func (cmd *postgresDatabaseCmd) Run(ctx context.Context, client *api.Client) err
3536
return upd.Update(ctx)
3637
}
3738

38-
func (cmd *postgresDatabaseCmd) applyUpdates(_ *storage.PostgresDatabase) {
39-
cmd.Warningf("there are no attributes for postgresdatabase which can be updated after creation. Applying update without any changes.")
39+
func (cmd *postgresDatabaseCmd) applyUpdates(db *storage.PostgresDatabase) {
40+
if cmd.BackupSchedule != nil {
41+
db.Spec.ForProvider.BackupSchedule = *cmd.BackupSchedule
42+
}
4043
}

update/postgresdatabase_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/ninech/nctl/api"
1111
"github.com/ninech/nctl/internal/format"
1212
"github.com/ninech/nctl/internal/test"
13+
"k8s.io/utils/ptr"
1314
)
1415

1516
func TestPostgresDatabase(t *testing.T) {
@@ -30,6 +31,12 @@ func TestPostgresDatabase(t *testing.T) {
3031
update: postgresDatabaseCmd{},
3132
wantErr: false,
3233
},
34+
{
35+
name: "update-backup-schedule",
36+
create: storage.PostgresDatabaseParameters{BackupSchedule: storage.DatabaseBackupScheduleCalendarDisabled},
37+
update: postgresDatabaseCmd{BackupSchedule: ptr.To(storage.DatabaseBackupScheduleCalendarDaily)},
38+
want: storage.PostgresDatabaseParameters{BackupSchedule: storage.DatabaseBackupScheduleCalendarDaily},
39+
},
3340
}
3441
for _, tt := range tests {
3542
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)