From c7269b8d582f16433e4dd76afaf1bd9b069ec4dd Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 21 Feb 2024 20:07:31 +0800 Subject: [PATCH] fix(dashboard): fix bugs that creates empty-name operations --- .../internal/business/oceanbase/obtenant.go | 33 +++++++++++-------- .../internal/handler/obtenant_handler.go | 9 +++-- .../internal/model/param/backup_param.go | 4 +-- .../internal/model/param/obtenant_param.go | 17 ++++------ distribution/dashboard/pkg/errors/error.go | 2 ++ 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/distribution/dashboard/internal/business/oceanbase/obtenant.go b/distribution/dashboard/internal/business/oceanbase/obtenant.go index 6ff5dea45..18df7ad27 100644 --- a/distribution/dashboard/internal/business/oceanbase/obtenant.go +++ b/distribution/dashboard/internal/business/oceanbase/obtenant.go @@ -346,7 +346,7 @@ func ModifyOBTenantRootPassword(ctx context.Context, nn types.NamespacedName, ro return buildDetailFromApiType(tenant), nil } -func ReplayStandbyLog(ctx context.Context, nn types.NamespacedName, timestamp string) (*response.OBTenantDetail, error) { +func ReplayStandbyLog(ctx context.Context, nn types.NamespacedName, param *param.ReplayStandbyLog) (*response.OBTenantDetail, error) { var err error tenant, err := oceanbase.GetOBTenant(ctx, nn) if err != nil { @@ -357,13 +357,14 @@ func ReplayStandbyLog(ctx context.Context, nn types.NamespacedName, timestamp st } replayLogOp := v1alpha1.OBTenantOperation{ ObjectMeta: v1.ObjectMeta{ - GenerateName: nn.Name + "-replay-log-" + rand.String(6), - Namespace: nn.Namespace, + Name: nn.Name + "-replay-log-" + rand.String(6), + Namespace: nn.Namespace, }, Spec: v1alpha1.OBTenantOperationSpec{ Type: apiconst.TenantOpReplayLog, ReplayUntil: &v1alpha1.RestoreUntilConfig{ - Timestamp: ×tamp, + Timestamp: param.Timestamp, + Unlimited: param.Unlimited, }, TargetTenant: &nn.Name, }, @@ -386,8 +387,8 @@ func UpgradeTenantVersion(ctx context.Context, nn types.NamespacedName) (*respon } upgradeOp := v1alpha1.OBTenantOperation{ ObjectMeta: v1.ObjectMeta{ - GenerateName: nn.Name + "-upgrade-" + rand.String(6), - Namespace: nn.Namespace, + Name: nn.Name + "-upgrade-" + rand.String(6), + Namespace: nn.Namespace, }, Spec: v1alpha1.OBTenantOperationSpec{ Type: apiconst.TenantOpUpgrade, @@ -407,26 +408,30 @@ func ChangeTenantRole(ctx context.Context, nn types.NamespacedName, p *param.Cha if err != nil { return nil, err } - if tenant.Status.TenantRole == apitypes.TenantRole(p.TenantRole) { - return nil, oberr.NewBadRequest("The tenant is already " + string(p.TenantRole)) + if tenant.Status.TenantRole == apiconst.TenantRolePrimary && p.Failover { + return nil, oberr.NewBadRequest("The tenant is already PRIMARY") } if p.Switchover && (tenant.Status.Source == nil || tenant.Status.Source.Tenant == nil) { return nil, oberr.NewBadRequest("The tenant has no primary tenant") } changeRoleOp := v1alpha1.OBTenantOperation{ ObjectMeta: v1.ObjectMeta{ - GenerateName: nn.Name + "-change-role-" + rand.String(6), - Namespace: nn.Namespace, + Name: nn.Name + "-change-role-" + rand.String(6), + Namespace: nn.Namespace, }, Spec: v1alpha1.OBTenantOperationSpec{}, } if p.Switchover { changeRoleOp.Spec.Type = apiconst.TenantOpSwitchover - changeRoleOp.Spec.Switchover.PrimaryTenant = *tenant.Status.Source.Tenant - changeRoleOp.Spec.Switchover.StandbyTenant = nn.Name - } else { + changeRoleOp.Spec.Switchover = &v1alpha1.OBTenantOpSwitchoverSpec{ + PrimaryTenant: *tenant.Status.Source.Tenant, + StandbyTenant: nn.Name, + } + } else if p.Failover { changeRoleOp.Spec.Type = apiconst.TenantOpFailover - changeRoleOp.Spec.Failover.StandbyTenant = nn.Name + changeRoleOp.Spec.Failover = &v1alpha1.OBTenantOpFailoverSpec{ + StandbyTenant: nn.Name, + } } _, err = oceanbase.CreateOBTenantOperation(ctx, &changeRoleOp) if err != nil { diff --git a/distribution/dashboard/internal/handler/obtenant_handler.go b/distribution/dashboard/internal/handler/obtenant_handler.go index 326f715cd..60e780b2f 100644 --- a/distribution/dashboard/internal/handler/obtenant_handler.go +++ b/distribution/dashboard/internal/handler/obtenant_handler.go @@ -247,13 +247,13 @@ func ReplayStandbyLog(c *gin.Context) (*response.OBTenantDetail, error) { if err != nil { return nil, httpErr.NewBadRequest(err.Error()) } - if logReplayParam.Timestamp == nil { - return nil, httpErr.NewBadRequest("timestamp is required") + if !logReplayParam.Unlimited && logReplayParam.Timestamp == nil { + return nil, httpErr.NewBadRequest("timestamp is required if the restore is limited") } tenant, err := oceanbase.ReplayStandbyLog(c, types.NamespacedName{ Name: nn.Name, Namespace: nn.Namespace, - }, *logReplayParam.Timestamp) + }, logReplayParam) if err != nil { return nil, httpErr.NewInternal(err.Error()) } @@ -316,6 +316,9 @@ func ChangeTenantRole(c *gin.Context) (*response.OBTenantDetail, error) { if err != nil { return nil, httpErr.NewBadRequest(err.Error()) } + if !p.Failover != p.Switchover { + return nil, httpErr.NewBadRequest("one and only one of failover and switchover can be true") + } tenant, err := oceanbase.ChangeTenantRole(c, types.NamespacedName{ Name: nn.Name, Namespace: nn.Namespace, diff --git a/distribution/dashboard/internal/model/param/backup_param.go b/distribution/dashboard/internal/model/param/backup_param.go index d76449879..5df945a6b 100644 --- a/distribution/dashboard/internal/model/param/backup_param.go +++ b/distribution/dashboard/internal/model/param/backup_param.go @@ -12,8 +12,8 @@ type ScheduleBase struct { type BackupPolicyBase struct { // Enum: NFS, OSS DestType BackupDestType `json:"destType" binding:"required" example:"NFS"` - ArchivePath string `json:"archivePath"` - BakDataPath string `json:"bakDataPath"` + ArchivePath string `json:"archivePath" binding:"required"` + BakDataPath string `json:"bakDataPath" binding:"required"` ScheduleBase `json:",inline"` diff --git a/distribution/dashboard/internal/model/param/obtenant_param.go b/distribution/dashboard/internal/model/param/obtenant_param.go index 3a59b1b23..15bf1a91b 100644 --- a/distribution/dashboard/internal/model/param/obtenant_param.go +++ b/distribution/dashboard/internal/model/param/obtenant_param.go @@ -34,9 +34,9 @@ type TenantSourceSpec struct { type RestoreSourceSpec struct { // Enum: OSS, NFS - Type BackupDestType `json:"type"` - ArchiveSource string `json:"archiveSource"` - BakDataSource string `json:"bakDataSource"` + Type BackupDestType `json:"type" binding:"required"` + ArchiveSource string `json:"archiveSource" binding:"required"` + BakDataSource string `json:"bakDataSource" binding:"required"` OSSAccessID string `json:"ossAccessId,omitempty"` OSSAccessKey string `json:"ossAccessKey,omitempty"` @@ -54,14 +54,10 @@ type UnitConfig struct { } type RestoreUntilConfig struct { - Timestamp *string `json:"timestamp,omitempty"` + Timestamp *string `json:"timestamp,omitempty" example:"2024-02-23 17:47:00"` Unlimited bool `json:"unlimited,omitempty"` } -type ModifyUnitNumber struct { - UnitNumber int `json:"unitNum" binding:"required"` -} - type ChangeUserPassword struct { // Description: The user name of the database account, only root is supported now. User string `json:"user" binding:"required"` @@ -71,9 +67,8 @@ type ChangeUserPassword struct { type ReplayStandbyLog RestoreUntilConfig type ChangeTenantRole struct { - // Enum: Primary, Standby - TenantRole TenantRole `json:"tenantRole" binding:"required"` - Switchover bool `json:"switchover,omitempty"` + Failover bool `json:"failover,omitempty"` + Switchover bool `json:"switchover,omitempty"` } type PatchUnitConfig struct { diff --git a/distribution/dashboard/pkg/errors/error.go b/distribution/dashboard/pkg/errors/error.go index d2261d9f1..976e6bcbc 100644 --- a/distribution/dashboard/pkg/errors/error.go +++ b/distribution/dashboard/pkg/errors/error.go @@ -11,6 +11,8 @@ type httpErr struct { children []*httpErr } +var _ ObError = &httpErr{} + func (e *httpErr) Error() string { return fmt.Sprintf("Error %s: %s", e.errorType, e.message) }