Skip to content

Commit

Permalink
Implement describe backup in CLI (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
posriniv authored Mar 7, 2024
1 parent dd53809 commit 65e8eb5
Show file tree
Hide file tree
Showing 12 changed files with 528 additions and 9 deletions.
39 changes: 39 additions & 0 deletions cmd/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,42 @@ var deleteBackupCmd = &cobra.Command{
},
}

var describeBackupCmd = &cobra.Command{
Use: "describe",
Short: "Describe backup for a cluster in YugabyteDB Managed",
Long: "Describe backup for a cluster in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
backupID, _ := cmd.Flags().GetString("backup-id")

authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}
authApi.GetInfo("", "")
backupResp, r, err := authApi.GetBackup(backupID).Execute()
if err != nil {
logrus.Debugf("Full HTTP response: %v", r)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}

if viper.GetString("output") == "table" {
fullBackupContext := *formatter.NewFullBackupContext()
fullBackupContext.Output = os.Stdout
fullBackupContext.Format = formatter.NewFullBackupFormat(viper.GetString("output"))
fullBackupContext.SetFullBackup(backupResp.GetData())
fullBackupContext.Write()
return
}

backupCtx := formatter.Context{
Output: os.Stdout,
Format: formatter.NewFullBackupFormat(viper.GetString("output")),
}

formatter.SingleBackupWrite(backupCtx, backupResp.GetData())
},
}

func init() {
BackupCmd.AddCommand(listBackupCmd)
listBackupCmd.Flags().String("cluster-name", "", "[OPTIONAL] Name of the cluster to fetch backups.")
Expand All @@ -246,4 +282,7 @@ func init() {
BackupCmd.AddCommand(policy.PolicyCmd)
}

BackupCmd.AddCommand(describeBackupCmd)
describeBackupCmd.Flags().String("backup-id", "", "[REQUIRED] The backup ID.")
describeBackupCmd.MarkFlagRequired("backup-id")
}
51 changes: 43 additions & 8 deletions cmd/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ import (
var _ = Describe("Backup", func() {

var (
server *ghttp.Server
statusCode int
args []string
responseAccount openapi.AccountListResponse
responseProject openapi.AccountListResponse
responseBackup openapi.BackupListResponse
server *ghttp.Server
statusCode int
args []string
responseAccount openapi.AccountListResponse
responseProject openapi.AccountListResponse
responseBackupList openapi.BackupListResponse
responseBackup openapi.BackupResponse

//cbr *cobra.Command
)

Expand All @@ -56,12 +58,12 @@ var _ = Describe("Backup", func() {

It("should return list of available backup", func() {
statusCode = 200
err := loadJson("./test/fixtures/backups.json", &responseBackup)
err := loadJson("./test/fixtures/backups.json", &responseBackupList)
Expect(err).ToNot(HaveOccurred())
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodGet, "/api/public/v1/accounts/340af43a-8a7c-4659-9258-4876fd6a207b/projects/78d4459c-0f45-47a5-899a-45ddf43eba6e/backups"),
ghttp.RespondWithJSONEncodedPtr(&statusCode, responseBackup),
ghttp.RespondWithJSONEncodedPtr(&statusCode, responseBackupList),
),
)
cmd := exec.Command(compiledCLIPath, "backup", "list")
Expand All @@ -74,6 +76,39 @@ faaca956-b542-49ee-92a8-9f1e138d1311 %s 🟡 %s mirthful-mole ✅
session.Kill()
})

It("should describe the given backup", func() {
statusCode = 200
err := loadJson("./test/fixtures/backup.json", &responseBackup)
Expect(err).ToNot(HaveOccurred())
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodGet, "/api/public/v1/accounts/340af43a-8a7c-4659-9258-4876fd6a207b/projects/78d4459c-0f45-47a5-899a-45ddf43eba6e/backups/5574d58f-68f1-4762-baa1-c2421cdb38b0"),
ghttp.RespondWithJSONEncodedPtr(&statusCode, responseBackup),
),
)
cmd := exec.Command(compiledCLIPath, "backup", "describe", "--backup-id", "5574d58f-68f1-4762-baa1-c2421cdb38b0")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait(2)
expected := fmt.Sprintf(`General
ID Created On Inc Clusters State
5574d58f-68f1-4762-baa1-c2421cdb38b0 %s 🟡 mirthful-mole ✅
Type Size(bytes) Expire On Duration
🧑 1176035 %s 2 mins
Databases/Keyspaces
Database/Keyspace API Type
yugabyte YSQL
my_keyspace YCQL
`, formatter.FormatDate("2024-03-07T17:56:14.553Z"), formatter.FormatDateAndAddDays("2024-03-07T17:56:14.553Z", 1))

o := string(session.Out.Contents()[:])
Expect(o).Should(Equal(expected))
session.Kill()

})
})

AfterEach(func() {
Expand Down
44 changes: 44 additions & 0 deletions cmd/test/fixtures/backup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"data": {
"spec": {
"cluster_id": "d6954ca7-fc4f-4454-9ce2-8c2e10ed9deb",
"retention_period_in_days": 1,
"backup_type": "FULL",
"description": null
},
"info": {
"id": "5574d58f-68f1-4762-baa1-c2421cdb38b0",
"cluster_name": "mirthful-mole",
"state": "SUCCEEDED",
"action_type": "MANUAL",
"keyspace_info": [
{
"table_type": "YSQL",
"keyspaces": [
"yugabyte"
]
},
{
"table_type": "YCQL",
"keyspaces": [
"my_keyspace"
]
}
],
"size_in_bytes": 1176035,
"deleted_on": "2024-03-08T17:58:28.606890Z",
"completed_on": "2024-03-07T17:58:28.606907Z",
"num_incremental_backups": 0,
"last_incremental_backup_completed_on": null,
"last_full_backup_completed_on": "2024-03-06T20:56:34.214975Z",
"backup_chain_size_in_bytes": 1176035,
"backup_current_chain_size_in_bytes": 1176035,
"tablespaces": [],
"base_backup_id": null,
"metadata": {
"created_on": "2024-03-07T17:56:14.553Z",
"updated_on": "2024-03-07T17:58:29.113Z"
}
}
}
}
1 change: 1 addition & 0 deletions docs/ybm.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ybm [flags]
* [ybm backup](ybm_backup.md) - Manage backup operations of a cluster
* [ybm cluster](ybm_cluster.md) - Manage cluster operations
* [ybm completion](ybm_completion.md) - Generate the autocompletion script for the specified shell
* [ybm db-audit-logs-exporter](ybm_db-audit-logs-exporter.md) - Manage DB Audit Logs
* [ybm metrics-exporter](ybm_metrics-exporter.md) - Manage Metrics Exporter
* [ybm network-allow-list](ybm_network-allow-list.md) - Manage Network Allow Lists
* [ybm permission](ybm_permission.md) - View available permissions for roles
Expand Down
1 change: 1 addition & 0 deletions docs/ybm_backup.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ybm backup [flags]
* [ybm](ybm.md) - ybm - Effortlessly manage your DB infrastructure on YugabyteDB Managed (DBaaS) from command line!
* [ybm backup create](ybm_backup_create.md) - Create backup for a cluster in YugabyteDB Managed
* [ybm backup delete](ybm_backup_delete.md) - Delete backup for a cluster in YugabyteDB Managed
* [ybm backup describe](ybm_backup_describe.md) - Describe backup for a cluster in YugabyteDB Managed
* [ybm backup list](ybm_backup_list.md) - List existing backups available for a cluster in YugabyteDB Managed
* [ybm backup policy](ybm_backup_policy.md) - Manage backup policy of a cluster
* [ybm backup restore](ybm_backup_restore.md) - Restore backups into a cluster in YugabyteDB Managed
Expand Down
36 changes: 36 additions & 0 deletions docs/ybm_backup_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## ybm backup describe

Describe backup for a cluster in YugabyteDB Managed

### Synopsis

Describe backup for a cluster in YugabyteDB Managed

```
ybm backup describe [flags]
```

### Options

```
--backup-id string [REQUIRED] The backup ID.
-h, --help help for describe
```

### Options inherited from parent commands

```
-a, --apiKey string YBM Api Key
--config string config file (default is $HOME/.ybm-cli.yaml)
--debug Use debug mode, same as --logLevel debug
-l, --logLevel string Select the desired log level format(info). Default to info
--no-color Disable colors in output , default to false
-o, --output string Select the desired output format (table, json, pretty). Default to table
--timeout duration Wait command timeout, example: 5m, 1h. (default 168h0m0s)
--wait Wait until the task is completed, otherwise it will exit immediately, default to false
```

### SEE ALSO

* [ybm backup](ybm_backup.md) - Manage backup operations of a cluster

37 changes: 37 additions & 0 deletions docs/ybm_db-audit-logs-exporter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## ybm db-audit-logs-exporter

Manage DB Audit Logs

### Synopsis

Manage DB Audit Logs

```
ybm db-audit-logs-exporter [flags]
```

### Options

```
-h, --help help for db-audit-logs-exporter
```

### Options inherited from parent commands

```
-a, --apiKey string YBM Api Key
--config string config file (default is $HOME/.ybm-cli.yaml)
--debug Use debug mode, same as --logLevel debug
-l, --logLevel string Select the desired log level format(info). Default to info
--no-color Disable colors in output , default to false
-o, --output string Select the desired output format (table, json, pretty). Default to table
--timeout duration Wait command timeout, example: 5m, 1h. (default 168h0m0s)
--wait Wait until the task is completed, otherwise it will exit immediately, default to false
```

### SEE ALSO

* [ybm](ybm.md) - ybm - Effortlessly manage your DB infrastructure on YugabyteDB Managed (DBaaS) from command line!
* [ybm db-audit-logs-exporter assign](ybm_db-audit-logs-exporter_assign.md) - Assign DB Audit
* [ybm db-audit-logs-exporter list](ybm_db-audit-logs-exporter_list.md) - List DB Audit Logs Export Config

43 changes: 43 additions & 0 deletions docs/ybm_db-audit-logs-exporter_assign.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## ybm db-audit-logs-exporter assign

Assign DB Audit

### Synopsis

Assign DB Audit Logs to a Cluster

```
ybm db-audit-logs-exporter assign [flags]
```

### Options

```
--telemetry-provider-id string [REQUIRED] The ID of the telemetry provider
--ysql-config stringToString [REQUIRED] The ysql config to setup DB auditting
Please provide key value pairs as follows:
log_catalog=<boolean>,log_level=<LOG_LEVEL> (default [])
--statement_classes string [REQUIRED] The ysql config statement classes
Please provide key value pairs as follows:
statement_classes=READ,WRITE,MISC
--cluster-id string [REQUIRED] The cluster ID to assign DB auditting
-h, --help help for assign
```

### Options inherited from parent commands

```
-a, --apiKey string YBM Api Key
--config string config file (default is $HOME/.ybm-cli.yaml)
--debug Use debug mode, same as --logLevel debug
-l, --logLevel string Select the desired log level format(info). Default to info
--no-color Disable colors in output , default to false
-o, --output string Select the desired output format (table, json, pretty). Default to table
--timeout duration Wait command timeout, example: 5m, 1h. (default 168h0m0s)
--wait Wait until the task is completed, otherwise it will exit immediately, default to false
```

### SEE ALSO

* [ybm db-audit-logs-exporter](ybm_db-audit-logs-exporter.md) - Manage DB Audit Logs

36 changes: 36 additions & 0 deletions docs/ybm_db-audit-logs-exporter_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## ybm db-audit-logs-exporter list

List DB Audit Logs Export Config

### Synopsis

List DB Audit Logs Export Config

```
ybm db-audit-logs-exporter list [flags]
```

### Options

```
--cluster-id string [REQUIRED] The cluster ID to list DB audit export config
-h, --help help for list
```

### Options inherited from parent commands

```
-a, --apiKey string YBM Api Key
--config string config file (default is $HOME/.ybm-cli.yaml)
--debug Use debug mode, same as --logLevel debug
-l, --logLevel string Select the desired log level format(info). Default to info
--no-color Disable colors in output , default to false
-o, --output string Select the desired output format (table, json, pretty). Default to table
--timeout duration Wait command timeout, example: 5m, 1h. (default 168h0m0s)
--wait Wait until the task is completed, otherwise it will exit immediately, default to false
```

### SEE ALSO

* [ybm db-audit-logs-exporter](ybm_db-audit-logs-exporter.md) - Manage DB Audit Logs

34 changes: 34 additions & 0 deletions internal/backup/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to Yugabyte, Inc. under one or more contributor license
// agreements. See the NOTICE file distributed with this work for
// additional information regarding copyright ownership. Yugabyte
// licenses this file to you 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 backup

import (
ybmclient "github.com/yugabyte/yugabytedb-managed-go-client-internal"
)

// This struct is an attempt to consilidate Backup information
type FullBackup struct {
Backup ybmclient.BackupData
Databases []ybmclient.BackupKeyspaceInfo
}

func NewFullBackup(backupData ybmclient.BackupData) *FullBackup {
fb := &FullBackup{
Backup: backupData,
Databases: backupData.Info.GetKeyspaceInfo(),
}
return fb
}
Loading

0 comments on commit 65e8eb5

Please sign in to comment.