Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/stackit_postgresflex.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ stackit postgresflex [flags]
### SEE ALSO

* [stackit](./stackit.md) - Manage STACKIT resources using the command line
* [stackit postgresflex backup](./stackit_postgresflex_backup.md) - Provides functionality for PostgreSQL Flex instance backups
* [stackit postgresflex instance](./stackit_postgresflex_instance.md) - Provides functionality for PostgreSQL Flex instances
* [stackit postgresflex options](./stackit_postgresflex_options.md) - Lists PostgreSQL Flex options
* [stackit postgresflex user](./stackit_postgresflex_user.md) - Provides functionality for PostgreSQL Flex users
Expand Down
35 changes: 35 additions & 0 deletions docs/stackit_postgresflex_backup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## stackit postgresflex backup

Provides functionality for PostgreSQL Flex instance backups

### Synopsis

Provides functionality for PostgreSQL Flex instance backups.

```
stackit postgresflex backup [flags]
```

### Options

```
-h, --help Help for "stackit postgresflex backup"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit postgresflex](./stackit_postgresflex.md) - Provides functionality for PostgreSQL Flex
* [stackit postgresflex backup describe](./stackit_postgresflex_backup_describe.md) - Shows details of a backup for a PostgreSQL Flex instance
* [stackit postgresflex backup list](./stackit_postgresflex_backup_list.md) - Lists all backups which are available for a PostgreSQL Flex instance
* [stackit postgresflex backup update-schedule](./stackit_postgresflex_backup_update-schedule.md) - Updates backup schedule for a PostgreSQL Flex instance

43 changes: 43 additions & 0 deletions docs/stackit_postgresflex_backup_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## stackit postgresflex backup describe

Shows details of a backup for a PostgreSQL Flex instance

### Synopsis

Shows details of a backup for a PostgreSQL Flex instance.

```
stackit postgresflex backup describe BACKUP_ID [flags]
```

### Examples

```
Get details of a backup with ID "xxx" for a PostgreSQL Flex instance with ID "yyy"
$ stackit postgresflex backup describe xxx --instance-id yyy

Get details of a backup with ID "xxx" for a PostgreSQL Flex instance with ID "yyy" in a table format
$ stackit postgresflex backup describe xxx --instance-id yyy --output-format pretty
```

### Options

```
-h, --help Help for "stackit postgresflex backup describe"
--instance-id string Instance ID
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit postgresflex backup](./stackit_postgresflex_backup.md) - Provides functionality for PostgreSQL Flex instance backups

47 changes: 47 additions & 0 deletions docs/stackit_postgresflex_backup_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## stackit postgresflex backup list

Lists all backups which are available for a PostgreSQL Flex instance

### Synopsis

Lists all backups which are available for a PostgreSQL Flex instance.

```
stackit postgresflex backup list [flags]
```

### Examples

```
List all backups of instance with ID "xxx"
$ stackit postgresflex backup list --instance-id xxx

List all backups of instance with ID "xxx" in JSON format
$ stackit postgresflex backup list --instance-id xxx --output-format json

List up to 10 backups of instance with ID "xxx"
$ stackit postgresflex backup list --instance-id xxx --limit 10
```

### Options

```
-h, --help Help for "stackit postgresflex backup list"
--instance-id string Instance ID
--limit int Maximum number of entries to list
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit postgresflex backup](./stackit_postgresflex_backup.md) - Provides functionality for PostgreSQL Flex instance backups

41 changes: 41 additions & 0 deletions docs/stackit_postgresflex_backup_update-schedule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## stackit postgresflex backup update-schedule

Updates backup schedule for a PostgreSQL Flex instance

### Synopsis

Updates backup schedule for a PostgreSQL Flex instance.

```
stackit postgresflex backup update-schedule [flags]
```

### Examples

```
Update the backup schedule of a PostgreSQL Flex instance with ID "xxx"
$ stackit postgresflex backup update-schedule --instance-id xxx --schedule '6 6 * * *'
```

### Options

```
-h, --help Help for "stackit postgresflex backup update-schedule"
--instance-id string Instance ID
--schedule string Backup schedule, in the cron scheduling system format e.g. '0 0 * * *'
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit postgresflex backup](./stackit_postgresflex_backup.md) - Provides functionality for PostgreSQL Flex instance backups

3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/jedib0t/go-pretty/v6 v6.5.8
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf
github.com/jedib0t/go-pretty/v6 v6.5.6
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf h1:FtEj8sfIcaaBfAKrE1Cwb61YDtYq9JxChK1c7AKce7s=
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf/go.mod h1:yrqSXGoD/4EKfF26AOGzscPOgTTJcyAwM2rpixWT+t4=
github.com/jedib0t/go-pretty/v6 v6.5.6 h1:nKXVLqPfAwY7sWcYXdNZZZ2fjqDpAtj9UeWupgfUxSg=
github.com/jedib0t/go-pretty/v6 v6.5.6/go.mod h1:5LQIxa52oJ/DlDSLv0HEkWOFMDGoWkJb9ss5KqPpJBg=
github.com/jedib0t/go-pretty/v6 v6.5.8 h1:8BCzJdSvUbaDuRba4YVh+SKMGcAAKdkcF3SVFbrHAtQ=
github.com/jedib0t/go-pretty/v6 v6.5.8/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
30 changes: 30 additions & 0 deletions internal/cmd/postgresflex/backup/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package backup

import (
"github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/backup/describe"
"github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/backup/list"
updateschedule "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/backup/update-schedule"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
)

func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: "backup",
Short: "Provides functionality for PostgreSQL Flex instance backups",
Long: "Provides functionality for PostgreSQL Flex instance backups.",
Args: args.NoArgs,
Run: utils.CmdHelp,
}
addSubcommands(cmd, p)
return cmd
}

func addSubcommands(cmd *cobra.Command, p *print.Printer) {
cmd.AddCommand(list.NewCmd(p))
cmd.AddCommand(describe.NewCmd(p))
cmd.AddCommand(updateschedule.NewCmd(p))
}
142 changes: 142 additions & 0 deletions internal/cmd/postgresflex/backup/describe/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package describe

import (
"context"
"encoding/json"
"fmt"

"time"

"github.com/inhies/go-bytesize"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
)

const (
backupIdArg = "BACKUP_ID"

instanceIdFlag = "instance-id"

backupExpireYearOffset = 0
backupExpireMonthOffset = 0
backupExpireDayOffset = 30
)

type inputModel struct {
*globalflags.GlobalFlagModel

InstanceId string
BackupId string
}

func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("describe %s", backupIdArg),
Short: "Shows details of a backup for a PostgreSQL Flex instance",
Long: "Shows details of a backup for a PostgreSQL Flex instance.",
Example: examples.Build(
examples.NewExample(
`Get details of a backup with ID "xxx" for a PostgreSQL Flex instance with ID "yyy"`,
"$ stackit postgresflex backup describe xxx --instance-id yyy"),
examples.NewExample(
`Get details of a backup with ID "xxx" for a PostgreSQL Flex instance with ID "yyy" in a table format`,
"$ stackit postgresflex backup describe xxx --instance-id yyy --output-format pretty"),
),
Args: args.SingleArg(backupIdArg, nil),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(p, cmd, args)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(p)
if err != nil {
return err
}

// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()

if err != nil {
return fmt.Errorf("describe backup for PostgreSQL Flex instance: %w", err)
}

return outputResult(p, cmd, model.OutputFormat, *resp.Item)
},
}
configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")

err := flags.MarkFlagsRequired(cmd, instanceIdFlag)
cobra.CheckErr(err)
}

func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
backupId := inputArgs[0]

globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}

return &inputModel{
GlobalFlagModel: globalFlags,
InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag),
BackupId: backupId,
}, nil
}

func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresflex.APIClient) postgresflex.ApiGetBackupRequest {
req := apiClient.GetBackup(ctx, model.ProjectId, model.InstanceId, model.BackupId)
return req
}

func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, backup postgresflex.Backup) error {
backupStartTime, err := time.Parse(time.RFC3339, *backup.StartTime)
if err != nil {
return fmt.Errorf("parse backup start time: %w", err)
}
backupExpireDate := backupStartTime.AddDate(backupExpireYearOffset, backupExpireMonthOffset, backupExpireDayOffset).Format(time.DateOnly)

switch outputFormat {
case print.PrettyOutputFormat:
table := tables.NewTable()
table.AddRow("ID", *backup.Id)
table.AddSeparator()
table.AddRow("START TIME", *backup.StartTime)
table.AddSeparator()
table.AddRow("EXPIRES AT", backupExpireDate)
table.AddSeparator()
table.AddRow("BACKUP SIZE", bytesize.New(float64(*backup.Size)))
Comment thread
DiogoFerrao marked this conversation as resolved.

err := table.Display(p)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
default:
details, err := json.MarshalIndent(backup, "", " ")
if err != nil {
return fmt.Errorf("marshal backup for PostgreSQL Flex instance: %w", err)
}
cmd.Println(string(details))

return nil
}
}
Loading