Skip to content

Commit

Permalink
feat(schedule_service): implement DeleteBackupSchedule
Browse files Browse the repository at this point in the history
  • Loading branch information
ulya-sidorina committed Oct 4, 2024
1 parent f5b96b1 commit 9552990
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 109 deletions.
2 changes: 1 addition & 1 deletion internal/connectors/db/yql/schema/create_tables.yql
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ CREATE TABLE BackupSchedules (
database String NOT NULL,
endpoint String NOT NULL,
name String,
active Bool NOT NULL,
status String,

crontab String NOT NULL,
ttl Interval,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (s *BackupScheduleService) UpdateBackupSchedule(
schedule := schedules[0]
ctx = xlog.With(ctx, zap.String("ContainerID", schedule.ContainerID))
// TODO: Need to check access to backup schedule not by container id?
subject, err := auth.CheckAuth(ctx, s.auth, auth.PermissionBackupGet, schedule.ContainerID, "")
subject, err := auth.CheckAuth(ctx, s.auth, auth.PermissionBackupCreate, schedule.ContainerID, "")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -307,6 +307,58 @@ func (s *BackupScheduleService) ToggleBackupSchedule(
return nil, status.Error(codes.Internal, "not implemented")
}

func (s *BackupScheduleService) DeleteBackupSchedule(
ctx context.Context, request *pb.DeleteBackupScheduleRequest,
) (*pb.BackupSchedule, error) {
ctx = grpcinfo.WithGRPCInfo(ctx)

scheduleID := request.GetId()
ctx = xlog.With(ctx, zap.String("BackupScheduleID", scheduleID))

xlog.Debug(ctx, "DeleteBackupSchedule", zap.Stringer("request", request))

schedules, err := s.driver.SelectBackupSchedulesWithRPOInfo(
ctx, queries.NewReadTableQuery(
queries.WithRawQuery(GetScheduleQuery),
queries.WithParameters(
table.ValueParam("$schedule_id", table_types.StringValueFromString(scheduleID)),
),
),
)

if err != nil {
xlog.Error(ctx, "error getting backup schedule", zap.Error(err))
return nil, status.Error(codes.Internal, "error getting backup schedule")
}
if len(schedules) == 0 {
xlog.Error(ctx, "backup schedule not found")
return nil, status.Error(codes.NotFound, "backup schedule not found")
}

schedule := schedules[0]
ctx = xlog.With(ctx, zap.String("ContainerID", schedule.ContainerID))
// TODO: Need to check access to backup schedule not by container id?
subject, err := auth.CheckAuth(ctx, s.auth, auth.PermissionBackupCreate, schedule.ContainerID, "")
if err != nil {
return nil, err
}
ctx = xlog.With(ctx, zap.String("SubjectID", subject))

// TODO: set status to deleted

err = s.driver.ExecuteUpsert(ctx, queries.NewWriteTableQuery().WithUpdateBackupSchedule(*schedule))
if err != nil {
xlog.Error(
ctx, "can't delete backup schedule", zap.String("backup schedule", schedule.Proto().String()),
zap.Error(err),
)
return nil, status.Error(codes.Internal, "can't delete backup schedule")
}

xlog.Info(ctx, "DeleteBackupSchedule was completed successfully", zap.Stringer("schedule", schedule))
return schedule.Proto(), nil
}

func (s *BackupScheduleService) Register(server server.Server) {
pb.RegisterBackupScheduleServiceServer(server.GRPCServer(), s)
}
Expand Down
11 changes: 11 additions & 0 deletions internal/types/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import (
"github.com/google/uuid"
)

var (
BackupStateUnknown = pb.Backup_STATUS_UNSPECIFIED.String()
BackupStatePending = pb.Backup_PENDING.String()
BackupStateRunning = pb.Backup_RUNNING.String()
BackupStateAvailable = pb.Backup_AVAILABLE.String()
BackupStateError = pb.Backup_ERROR.String()
BackupStateCancelled = pb.Backup_CANCELLED.String()
BackupStateDeleting = pb.Backup_DELETING.String()
BackupStateDeleted = pb.Backup_DELETED.String()
)

func GenerateObjectID() string {
return uuid.New().String()
}
Expand Down
14 changes: 11 additions & 3 deletions internal/types/backup_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
)

var (
ScheduleStateUnknown = pb.BackupSchedule_STATUS_UNSPECIFIED.String()
ScheduleStateActive = pb.BackupSchedule_ACTIVE.String()
ScheduleStateInactive = pb.BackupSchedule_INACTIVE.String()
ScheduleStateDeleted = pb.BackupSchedule_DELETED.String()
)

type BackupSchedule struct {
ID string
ContainerID string
Expand All @@ -20,7 +27,7 @@ type BackupSchedule struct {
SourcePathsToExclude []string
Audit *pb.AuditInfo
Name *string
Active bool
Status string
ScheduleSettings *pb.BackupScheduleSettings
NextLaunch *time.Time
LastBackupID *string
Expand Down Expand Up @@ -55,7 +62,7 @@ func (b *BackupSchedule) Proto() *pb.BackupSchedule {
DatabaseName: b.DatabaseName,
Endpoint: b.DatabaseEndpoint,
Audit: b.Audit,
Active: b.Active,
Status: pb.BackupSchedule_Status(pb.BackupSchedule_Status_value[b.Status]),
ScheduleSettings: b.ScheduleSettings,
SourcePaths: b.SourcePaths,
SourcePathsToExclude: b.SourcePathsToExclude,
Expand All @@ -70,12 +77,13 @@ func (b *BackupSchedule) Proto() *pb.BackupSchedule {

func (b *BackupSchedule) String() string {
return fmt.Sprintf(
"ID: %s, Name: %s, ContainerID: %s, DatabaseEndpoint: %s, DatabaseName: %s",
"ID: %s, Name: %s, ContainerID: %s, DatabaseEndpoint: %s, DatabaseName: %s, Status: %s",
b.ID,
*b.Name,
b.ContainerID,
b.DatabaseEndpoint,
b.DatabaseName,
b.Status,
)
}

Expand Down
9 changes: 0 additions & 9 deletions internal/types/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,6 @@ var (
OperationStateCancelling = OperationState(pb.Operation_CANCELLING.String())
OperationStateCancelled = OperationState(pb.Operation_CANCELED.String())
OperationStateStartCancelling = OperationState(pb.Operation_START_CANCELLING.String())

BackupStateUnknown = pb.Backup_STATUS_UNSPECIFIED.String()
BackupStatePending = pb.Backup_PENDING.String()
BackupStateRunning = pb.Backup_RUNNING.String()
BackupStateAvailable = pb.Backup_AVAILABLE.String()
BackupStateError = pb.Backup_ERROR.String()
BackupStateCancelled = pb.Backup_CANCELLED.String()
BackupStateDeleting = pb.Backup_DELETING.String()
BackupStateDeleted = pb.Backup_DELETED.String()
)

const (
Expand Down
153 changes: 108 additions & 45 deletions pkg/proto/ydbcp/v1alpha1/backup_schedule.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion pkg/proto/ydbcp/v1alpha1/backup_schedule.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ message ScheduledBackupInfo {
}

message BackupSchedule {
enum Status {
STATUS_UNSPECIFIED = 0;
ACTIVE = 1;
INACTIVE = 2;
DELETED = 3;
}

//backup settings
string id = 1;
string container_id = 2;
Expand All @@ -37,7 +44,7 @@ message BackupSchedule {
ydbcp.v1alpha1.AuditInfo audit = 7;

string schedule_name = 8;
bool active = 9;
Status status = 9;

BackupScheduleSettings schedule_settings = 10;

Expand Down
Loading

0 comments on commit 9552990

Please sign in to comment.