From 832319eb92a418faec925f509eb22e25ea872ae4 Mon Sep 17 00:00:00 2001 From: Mike McKay-Dirden <93532247+KastenMike@users.noreply.github.com> Date: Wed, 1 Nov 2023 09:43:41 +0100 Subject: [PATCH] Add RepositorySetParametersCommand (#2429) * add RepositorySetParametersCommand * fix test * hardcode values like other parts * Update pkg/kopia/command/repository.go Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com> * check `RetentionPeriod` is set * revert to only checking for mode * add checker for create repo * Fix spacing after resolving conflict * fix test --------- Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com> --- pkg/kopia/command/const.go | 3 ++ pkg/kopia/command/repository.go | 39 ++++++++++++++--- pkg/kopia/command/repository_test.go | 65 ++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 5 deletions(-) diff --git a/pkg/kopia/command/const.go b/pkg/kopia/command/const.go index 1ae9c4f0be..366f569503 100644 --- a/pkg/kopia/command/const.go +++ b/pkg/kopia/command/const.go @@ -62,6 +62,7 @@ const ( serverSubCommand = "server" startSubCommand = "start" statusSubCommand = "status" + setParametersSubCommand = "set-parameters" userSubCommand = "user" addressFlag = "--address" redirectToDevNull = "> /dev/null 2>&1" @@ -85,6 +86,8 @@ const ( pointInTimeConnectionFlag = "--point-in-time" urlFlag = "--url" readOnlyFlag = "--readonly" + retentionModeFlag = "--retention-mode" + retentionPeriodFlag = "--retention-period" ) // List of possible modifications to a policy, expressed as the kopia flag that will modify it diff --git a/pkg/kopia/command/repository.go b/pkg/kopia/command/repository.go index 604deb7e20..8ce69f7dee 100644 --- a/pkg/kopia/command/repository.go +++ b/pkg/kopia/command/repository.go @@ -28,11 +28,16 @@ import ( type RepositoryCommandArgs struct { *CommandArgs CacheArgs - CacheDirectory string - Hostname string - Username string - RepoPathPrefix string - ReadOnly bool + CacheDirectory string + Hostname string + ContentCacheMB int + MetadataCacheMB int + Username string + RepoPathPrefix string + ReadOnly bool + // Only for CreateCommand + RetentionMode string + RetentionPeriod time.Duration // PITFlag is only effective if set while repository connect PITFlag strfmt.DateTime Location map[string][]byte @@ -87,6 +92,12 @@ func RepositoryCreateCommand(cmdArgs RepositoryCommandArgs) ([]string, error) { args = args.AppendLoggableKV(overrideUsernameFlag, cmdArgs.Username) } + // During creation, both should be set. Technically RetentionPeriod should be >= 24 * time.Hour + if cmdArgs.RetentionMode != "" && cmdArgs.RetentionPeriod > 0 { + args = args.AppendLoggableKV(retentionModeFlag, cmdArgs.RetentionMode) + args = args.AppendLoggableKV(retentionPeriodFlag, cmdArgs.RetentionPeriod.String()) + } + bsArgs, err := storage.KopiaStorageArgs(&storage.StorageCommandParams{ Location: cmdArgs.Location, RepoPathPrefix: cmdArgs.RepoPathPrefix, @@ -166,3 +177,21 @@ func RepositoryStatusCommand(cmdArgs RepositoryStatusCommandArgs) []string { return stringSliceCommand(args) } + +type RepositorySetParametersCommandArgs struct { + *CommandArgs + RetentionMode string + RetentionPeriod time.Duration +} + +// RepositorySetParametersCommand to cover https://kopia.io/docs/reference/command-line/common/repository-set-parameters/ +func RepositorySetParametersCommand(cmdArgs RepositorySetParametersCommandArgs) []string { + args := commonArgs(cmdArgs.CommandArgs) + args = args.AppendLoggable(repositorySubCommand, setParametersSubCommand) + // RetentionPeriod can be 0 when wanting to disable blob retention or when changing the mode only + if cmdArgs.RetentionMode != "" { + args = args.AppendLoggableKV(retentionModeFlag, cmdArgs.RetentionMode) + args = args.AppendLoggableKV(retentionPeriodFlag, cmdArgs.RetentionPeriod.String()) + } + return stringSliceCommand(args) +} diff --git a/pkg/kopia/command/repository_test.go b/pkg/kopia/command/repository_test.go index eeae98d8ba..9166159db7 100644 --- a/pkg/kopia/command/repository_test.go +++ b/pkg/kopia/command/repository_test.go @@ -17,6 +17,7 @@ package command import ( "strings" "testing" + "time" "github.com/go-openapi/strfmt" "gopkg.in/check.v1" @@ -29,6 +30,8 @@ type RepositoryUtilsSuite struct{} var _ = check.Suite(&RepositoryUtilsSuite{}) func (s *RepositoryUtilsSuite) TestRepositoryCreateUtil(c *check.C) { + retentionMode := "Locked" + retentionPeriod := 10 * time.Second for _, tc := range []struct { cmdArg RepositoryCommandArgs location map[string]string @@ -94,6 +97,46 @@ func (s *RepositoryUtilsSuite) TestRepositoryCreateUtil(c *check.C) { "--path=/mnt/data/test-prefix/test-path/prefix/", }, }, + { + cmdArg: RepositoryCommandArgs{ + CommandArgs: &CommandArgs{ + RepoPassword: "pass123", + ConfigFilePath: "/tmp/config.file", + LogDirectory: "/tmp/log.dir", + }, + CacheDirectory: "/tmp/cache.dir", + Hostname: "test-hostname", + ContentCacheMB: 0, + MetadataCacheMB: 0, + Username: "test-username", + RepoPathPrefix: "test-path/prefix", + Location: map[string][]byte{ + "prefix": []byte("test-prefix"), + "type": []byte("filestore"), + }, + RetentionMode: retentionMode, + RetentionPeriod: retentionPeriod, + }, + Checker: check.IsNil, + expectedCmd: []string{"kopia", + "--log-level=error", + "--config-file=/tmp/config.file", + "--log-dir=/tmp/log.dir", + "--password=pass123", + "repository", + "create", + "--no-check-for-updates", + "--cache-directory=/tmp/cache.dir", + "--content-cache-size-limit-mb=0", + "--metadata-cache-size-limit-mb=0", + "--override-hostname=test-hostname", + "--override-username=test-username", + "--retention-mode=Locked", + "--retention-period=10s", + "filesystem", + "--path=/mnt/data/test-prefix/test-path/prefix/", + }, + }, } { cmd, err := RepositoryCreateCommand(tc.cmdArg) c.Assert(err, tc.Checker) @@ -255,3 +298,25 @@ func (kRepoStatus *RepositoryUtilsSuite) TestRepositoryStatusCommand(c *check.C) c.Check(cmd, check.Equals, tc.expectedLog) } } + +func (s *RepositoryUtilsSuite) TestRepositorySetParametersCommand(c *check.C) { + retentionMode := "Locked" + retentionPeriod := 10 * time.Second + cmd := RepositorySetParametersCommand(RepositorySetParametersCommandArgs{ + CommandArgs: &CommandArgs{ + ConfigFilePath: "path/kopia.config", + LogDirectory: "cache/log", + }, + RetentionMode: retentionMode, + RetentionPeriod: retentionPeriod, + }) + c.Assert(cmd, check.DeepEquals, []string{"kopia", + "--log-level=error", + "--config-file=path/kopia.config", + "--log-dir=cache/log", + "repository", + "set-parameters", + "--retention-mode=Locked", + "--retention-period=10s", + }) +}