Skip to content

Commit

Permalink
Add RepositorySetParametersCommand (#2429)
Browse files Browse the repository at this point in the history
* add RepositorySetParametersCommand

* fix test

* hardcode values like other parts

* Update pkg/kopia/command/repository.go

Co-authored-by: Pavan Navarathna <[email protected]>

* 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 <[email protected]>
  • Loading branch information
KastenMike and pavannd1 authored Nov 1, 2023
1 parent c436b25 commit 832319e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pkg/kopia/command/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const (
serverSubCommand = "server"
startSubCommand = "start"
statusSubCommand = "status"
setParametersSubCommand = "set-parameters"
userSubCommand = "user"
addressFlag = "--address"
redirectToDevNull = "> /dev/null 2>&1"
Expand All @@ -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
Expand Down
39 changes: 34 additions & 5 deletions pkg/kopia/command/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
65 changes: 65 additions & 0 deletions pkg/kopia/command/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package command
import (
"strings"
"testing"
"time"

"github.com/go-openapi/strfmt"
"gopkg.in/check.v1"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
})
}

0 comments on commit 832319e

Please sign in to comment.