From c5da31730d19963617fc808d3318ffe5b98703d9 Mon Sep 17 00:00:00 2001 From: "pavel.larkin" Date: Tue, 6 Feb 2024 17:34:15 -0800 Subject: [PATCH] Add kopia CLI repository set-parameters command --- pkg/kopia/cli/internal/command/commands.go | 9 +-- .../repository/repository_set_parameters.go | 45 ++++++++++++++ pkg/kopia/cli/repository/repository_test.go | 62 +++++++++++++++++++ 3 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 pkg/kopia/cli/repository/repository_set_parameters.go diff --git a/pkg/kopia/cli/internal/command/commands.go b/pkg/kopia/cli/internal/command/commands.go index f5d6ce2a5c..73ed075440 100644 --- a/pkg/kopia/cli/internal/command/commands.go +++ b/pkg/kopia/cli/internal/command/commands.go @@ -21,10 +21,11 @@ var ( // Repository commands. var ( - Repository = Command{"repository"} - Create = Command{"create"} - Connect = Command{"connect"} - Server = Command{"server"} + Repository = Command{"repository"} + Create = Command{"create"} + Connect = Command{"connect"} + Server = Command{"server"} + SetParameters = Command{"set-parameters"} ) // Repository storage sub commands. diff --git a/pkg/kopia/cli/repository/repository_set_parameters.go b/pkg/kopia/cli/repository/repository_set_parameters.go new file mode 100644 index 0000000000..80bc98d6bb --- /dev/null +++ b/pkg/kopia/cli/repository/repository_set_parameters.go @@ -0,0 +1,45 @@ +// Copyright 2024 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repository + +import ( + "time" + + "github.com/kanisterio/safecli" + + "github.com/kanisterio/kanister/pkg/log" + + "github.com/kanisterio/kanister/pkg/kopia/cli" + "github.com/kanisterio/kanister/pkg/kopia/cli/internal/command" + flagrepo "github.com/kanisterio/kanister/pkg/kopia/cli/internal/flag/repository" +) + +// SetParametersArgs defines the arguments for the `kopia repository set-parameters ...` command. +type SetParametersArgs struct { + cli.CommonArgs + + RetentionMode string // retention mode for supported storage backends + RetentionPeriod time.Duration // retention period for supported storage backends + + Logger log.Logger +} + +// SetParameters creates a new `kopia repository set-parameters ...` command. +func SetParameters(args SetParametersArgs) (safecli.CommandBuilder, error) { + return command.NewKopiaCommandBuilder(args.CommonArgs, + command.Repository, command.SetParameters, + flagrepo.BlobRetention(args.RetentionMode, args.RetentionPeriod), + ) +} diff --git a/pkg/kopia/cli/repository/repository_test.go b/pkg/kopia/cli/repository/repository_test.go index 76b3418bc4..0e4aba9b1a 100644 --- a/pkg/kopia/cli/repository/repository_test.go +++ b/pkg/kopia/cli/repository/repository_test.go @@ -381,3 +381,65 @@ var _ = check.Suite(test.NewCommandSuite([]test.CommandTest{ }, }, })) + +// Test Repository Set Parameters command +var _ = check.Suite(test.NewCommandSuite([]test.CommandTest{ + { + Name: "repository set-parameters with default retention", + CLI: func() (safecli.CommandBuilder, error) { + args := SetParametersArgs{ + CommonArgs: test.CommonArgs, + } + return SetParameters(args) + }, + ExpectedCLI: []string{"kopia", + "--config-file=path/kopia.config", + "--log-level=error", + "--log-dir=cache/log", + "--password=encr-key", + "repository", + "set-parameters", + }, + }, + { + Name: "repository set-parameters with custom retention args", + CLI: func() (safecli.CommandBuilder, error) { + args := SetParametersArgs{ + CommonArgs: test.CommonArgs, + RetentionMode: retentionMode, + RetentionPeriod: retentionPeriod, + } + return SetParameters(args) + }, + ExpectedCLI: []string{"kopia", + "--config-file=path/kopia.config", + "--log-level=error", + "--log-dir=cache/log", + "--password=encr-key", + "repository", + "set-parameters", + "--retention-mode=Locked", + "--retention-period=15m0s", + }, + }, + { + Name: "repository set-parameters with custom retention mode only", + CLI: func() (safecli.CommandBuilder, error) { + args := SetParametersArgs{ + CommonArgs: test.CommonArgs, + RetentionMode: retentionMode, + } + return SetParameters(args) + }, + ExpectedCLI: []string{"kopia", + "--config-file=path/kopia.config", + "--log-level=error", + "--log-dir=cache/log", + "--password=encr-key", + "repository", + "set-parameters", + "--retention-mode=Locked", + "--retention-period=0s", + }, + }, +}))