Skip to content

Commit

Permalink
fix(validation): validate requests when deleting resource pool and cr…
Browse files Browse the repository at this point in the history
…eating backup policy
  • Loading branch information
powerfooI committed Mar 14, 2024
1 parent 800aa39 commit 65adde0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
6 changes: 5 additions & 1 deletion internal/dashboard/business/oceanbase/obtenant_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ func DeleteTenantPool(ctx context.Context, nn param.TenantPoolName) (bool, error
if err != nil {
return false, err
}
if len(tenantCR.Spec.Pools) == 1 {
switch len(tenantCR.Spec.Pools) {
case 1:
return false, oberr.NewBadRequest("at least one pool is required")
case 2:
return false, oberr.NewBadRequest("Error 4179 (HY000): forbidden to delete 1 of 2 units due to locality principal")
default:
}
remainPools := make([]v1alpha1.ResourcePoolSpec, 0, len(tenantCR.Spec.Pools)-1)
for i, pool := range tenantCR.Spec.Pools {
Expand Down
28 changes: 22 additions & 6 deletions internal/dashboard/business/oceanbase/obtenantbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func dayToNumber(day string) int {
return n
}

func setScheduleDatesToPolicy(policy *v1alpha1.OBTenantBackupPolicy, p param.ScheduleBase) {
func setScheduleDatesToPolicy(policy *v1alpha1.OBTenantBackupPolicy, p *param.ScheduleBase) error {
hourMinutes := strings.Split(p.ScheduleTime, ":")
crontabParts := fmt.Sprintf("%s %s", hourMinutes[1], hourMinutes[0])

Expand All @@ -65,6 +65,9 @@ func setScheduleDatesToPolicy(policy *v1alpha1.OBTenantBackupPolicy, p param.Sch
incrementalCrontabWeekdays = append(incrementalCrontabWeekdays, fmt.Sprint(date.Day%7))
}
}
if len(fullCrontabWeekdays) == 0 {
return oberr.NewBadRequest("At least one full backup day is required")
}
policy.Spec.DataBackup.FullCrontab = crontabParts + " " + strings.Join(fullCrontabWeekdays, ",")
if len(incrementalCrontabWeekdays) > 0 {
policy.Spec.DataBackup.IncrementalCrontab = crontabParts + " " + strings.Join(incrementalCrontabWeekdays, ",")
Expand All @@ -81,13 +84,17 @@ func setScheduleDatesToPolicy(policy *v1alpha1.OBTenantBackupPolicy, p param.Sch
incrementalCrontabMonthdays = append(incrementalCrontabMonthdays, fmt.Sprint(date.Day))
}
}
if len(fullCrontabMonthdays) == 0 {
return oberr.NewBadRequest("At least one full backup day is required")
}
policy.Spec.DataBackup.FullCrontab = strings.Join([]string{crontabParts, strings.Join(fullCrontabMonthdays, ","), "* *"}, " ")
if len(incrementalCrontabMonthdays) > 0 {
policy.Spec.DataBackup.IncrementalCrontab = strings.Join([]string{crontabParts, strings.Join(incrementalCrontabMonthdays, ","), "* *"}, " ")
} else {
policy.Spec.DataBackup.IncrementalCrontab = strings.Join([]string{crontabParts, "*", "* *"}, " ")
}
}
return nil
}

func getScheduleDatesFromPolicy(p *v1alpha1.OBTenantBackupPolicy) param.ScheduleBase {
Expand Down Expand Up @@ -167,7 +174,7 @@ func getScheduleDatesFromPolicy(p *v1alpha1.OBTenantBackupPolicy) param.Schedule
return res
}

func buildBackupPolicyApiType(nn types.NamespacedName, obcluster string, p *param.CreateBackupPolicy) *v1alpha1.OBTenantBackupPolicy {
func buildBackupPolicyApiType(nn types.NamespacedName, obcluster string, p *param.CreateBackupPolicy) (*v1alpha1.OBTenantBackupPolicy, error) {
policy := &v1alpha1.OBTenantBackupPolicy{}
policy.Name = nn.Name + "-backup-policy"
policy.Namespace = nn.Namespace
Expand Down Expand Up @@ -198,9 +205,12 @@ func buildBackupPolicyApiType(nn types.NamespacedName, obcluster string, p *para
},
}

setScheduleDatesToPolicy(policy, p.ScheduleBase)
err := setScheduleDatesToPolicy(policy, &p.ScheduleBase)
if err != nil {
return nil, err
}

return policy
return policy, nil
}

func buildBackupPolicyModelType(p *v1alpha1.OBTenantBackupPolicy) *response.BackupPolicy {
Expand Down Expand Up @@ -309,7 +319,10 @@ func CreateTenantBackupPolicy(ctx context.Context, nn types.NamespacedName, p *p
if p.PieceIntervalDays == 0 {
p.PieceIntervalDays = 1
}
backupPolicy := buildBackupPolicyApiType(nn, tenant.Spec.ClusterName, p)
backupPolicy, err := buildBackupPolicyApiType(nn, tenant.Spec.ClusterName, p)
if err != nil {
return nil, err
}

if p.DestType == "OSS" && p.OSSAccessID != "" && p.OSSAccessKey != "" {
ossSecretName := nn.Name + "-backup-oss-secret-" + rand.String(6)
Expand Down Expand Up @@ -398,7 +411,10 @@ func UpdateTenantBackupPolicy(ctx context.Context, nn types.NamespacedName, p *p
if schedule.ScheduleDates != nil {
overlaySchedule.ScheduleDates = schedule.ScheduleDates
}
setScheduleDatesToPolicy(policy, overlaySchedule)
err := setScheduleDatesToPolicy(policy, &overlaySchedule)
if err != nil {
return nil, err
}
}

np, err := oceanbase.UpdateTenantBackupPolicy(ctx, policy)
Expand Down
6 changes: 4 additions & 2 deletions internal/dashboard/business/oceanbase/obtenantbackup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ var _ = Describe("OBTenantBackup", func() {
},
},
}
policy := buildBackupPolicyApiType(types.NamespacedName{Name: "t1", Namespace: "default"}, "fake-cluster", &p)
policy, err := buildBackupPolicyApiType(types.NamespacedName{Name: "t1", Namespace: "default"}, "fake-cluster", &p)
Expect(err).To(BeNil())
Expect("t1-backup-policy").To(Equal(policy.Name))
Expect("00 04 * * 1,5").To(Equal(policy.Spec.DataBackup.FullCrontab))
Expect("00 04 * * 2,3,4").To(Equal(policy.Spec.DataBackup.IncrementalCrontab))
Expand Down Expand Up @@ -116,7 +117,8 @@ var _ = Describe("OBTenantBackup", func() {
},
},
}
policy := buildBackupPolicyApiType(types.NamespacedName{Name: "t1", Namespace: "default"}, "fake-cluster", &p)
policy, err := buildBackupPolicyApiType(types.NamespacedName{Name: "t1", Namespace: "default"}, "fake-cluster", &p)
Expect(err).To(BeNil())
Expect("t1-backup-policy").To(Equal(policy.Name))
Expect("00 04 1,5,15,21,31 * *").To(Equal(policy.Spec.DataBackup.FullCrontab))
Expect("00 04 2,3,4,16,24 * *").To(Equal(policy.Spec.DataBackup.IncrementalCrontab))
Expand Down

0 comments on commit 65adde0

Please sign in to comment.