Skip to content

Commit

Permalink
Add the list/describe commands (#93)
Browse files Browse the repository at this point in the history
* wip

* wip

* fix the unit tests for backup as well

* do CDC as well

* typo
  • Loading branch information
cdavid authored Mar 13, 2023
1 parent d3cecc8 commit 7dc4602
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 74 deletions.
15 changes: 15 additions & 0 deletions cmd/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ var getBackupCmd = &cobra.Command{
Use: "get",
Short: "Get list of existing backups available for a cluster in YugabyteDB Managed",
Long: "Get list of existing backups available for a cluster in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
listBackupCmd.Run(cmd, args)
logrus.Warnln("\nThe command `ybm backup get` is deprecated. Please use `ybm backup list` instead.")
},
}

var listBackupCmd = &cobra.Command{
Use: "list",
Short: "List existing backups available for a cluster in YugabyteDB Managed",
Long: "List existing backups available for a cluster in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
Expand Down Expand Up @@ -69,6 +79,8 @@ var getBackupCmd = &cobra.Command{
},
}

// TODO: implement a backup describe command that shows the details of a backup

var restoreBackupCmd = &cobra.Command{
Use: "restore",
Short: "Restore backups into a cluster in YugabyteDB Managed",
Expand Down Expand Up @@ -211,6 +223,9 @@ func init() {
BackupCmd.AddCommand(getBackupCmd)
getBackupCmd.Flags().String("cluster-name", "", "[OPTIONAL] Name of the cluster to fetch backups.")

BackupCmd.AddCommand(listBackupCmd)
listBackupCmd.Flags().String("cluster-name", "", "[OPTIONAL] Name of the cluster to fetch backups.")

BackupCmd.AddCommand(restoreBackupCmd)
restoreBackupCmd.Flags().String("cluster-name", "", "[REQUIRED] Name of the cluster to restore backups.")
restoreBackupCmd.MarkFlagRequired("cluster-name")
Expand Down
2 changes: 1 addition & 1 deletion cmd/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var _ = Describe("Backup", func() {
ghttp.RespondWithJSONEncodedPtr(&statusCode, responseBackup),
),
)
cmd := exec.Command(compiledCLIPath, "backup", "get")
cmd := exec.Command(compiledCLIPath, "backup", "list")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait(2)
Expand Down
14 changes: 8 additions & 6 deletions cmd/cdc/cdc_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ var CDCSinkCmd = &cobra.Command{
},
}

var getCdcSinkCmd = &cobra.Command{
Use: "get",
Short: "Get CDC Sink in YugabyteDB Managed",
Long: `Get CDC Sink in YugabyteDB Managed`,
var listCdcSinkCmd = &cobra.Command{
Use: "list",
Short: "List CDC Sinks in YugabyteDB Managed",
Long: `List CDC Sinks in YugabyteDB Managed`,
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
Expand All @@ -74,6 +74,8 @@ var getCdcSinkCmd = &cobra.Command{
},
}

// TODO: implement describe that shows the details of a sink

var createCdcSinkCmd = &cobra.Command{
Use: "create",
Short: "Create CDC Sink in YugabyteDB Managed",
Expand Down Expand Up @@ -208,8 +210,8 @@ var deleteCdcSinkCmd = &cobra.Command{
func init() {
CdcCmd.AddCommand(CDCSinkCmd)

CDCSinkCmd.AddCommand(getCdcSinkCmd)
getCdcSinkCmd.Flags().String("name", "", "[OPTIONAL] Name of the CDC sink.")
CDCSinkCmd.AddCommand(listCdcSinkCmd)
listCdcSinkCmd.Flags().String("name", "", "[OPTIONAL] Name of the CDC sink.")

CDCSinkCmd.AddCommand(createCdcSinkCmd)
createCdcSinkCmd.Flags().String("name", "", "[REQUIRED] Name of the CDC sink.")
Expand Down
18 changes: 10 additions & 8 deletions cmd/cdc/cdc_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ var CDCStreamCmd = &cobra.Command{
},
}

var getCdcStreamCmd = &cobra.Command{
Use: "get",
Short: "Get CDC Stream in YugabyteDB Managed",
Long: "Get CDC Stream in YugabyteDB Managed",
var listCdcStreamCmd = &cobra.Command{
Use: "list",
Short: "List CDC Streams in YugabyteDB Managed",
Long: "List CDC Streams in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
Expand Down Expand Up @@ -78,6 +78,8 @@ var getCdcStreamCmd = &cobra.Command{
},
}

// TODO: implement a describe command, which will show the details of a CDC stream

var createCdcStreamCmd = &cobra.Command{
Use: "create",
Short: "Create CDC Stream in YugabyteDB Managed",
Expand Down Expand Up @@ -254,10 +256,10 @@ var deleteCdcStreamCmd = &cobra.Command{
func init() {
CdcCmd.AddCommand(CDCStreamCmd)

CDCStreamCmd.AddCommand(getCdcStreamCmd)
getCdcStreamCmd.Flags().String("name", "", "[OPTIONAL] Name of the CDC Stream.")
getCdcStreamCmd.Flags().String("cluster-name", "", "[REQUIRED] Name of the Cluster.")
getCdcStreamCmd.MarkFlagRequired("cluster-name")
CDCStreamCmd.AddCommand(listCdcStreamCmd)
listCdcStreamCmd.Flags().String("name", "", "[OPTIONAL] Name of the CDC Stream.")
listCdcStreamCmd.Flags().String("cluster-name", "", "[REQUIRED] Name of the Cluster.")
listCdcStreamCmd.MarkFlagRequired("cluster-name")

CDCStreamCmd.AddCommand(createCdcStreamCmd)
createCdcStreamCmd.Flags().String("name", "", "[REQUIRED] Name of the CDC Stream.")
Expand Down
69 changes: 69 additions & 0 deletions cmd/cluster/describe_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 cluster

import (
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ybmAuthClient "github.com/yugabyte/ybm-cli/internal/client"
"github.com/yugabyte/ybm-cli/internal/formatter"
)

var describeClusterCmd = &cobra.Command{
Use: "describe",
Short: "Describe a cluster in YugabyteDB Managed",
Long: "Describe a cluster in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("could not initiate api client: %s", err.Error())
}
authApi.GetInfo("", "")
clusterListRequest := authApi.ListClusters()
clusterName, _ := cmd.Flags().GetString("cluster-name")
clusterListRequest = clusterListRequest.Name(clusterName)

resp, r, err := clusterListRequest.Execute()

if err != nil {
logrus.Debugf("Full HTTP response: %v", r)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}

if len(resp.GetData()) > 0 && viper.GetString("output") == "table" {
fullClusterContext := *formatter.NewFullClusterContext()
fullClusterContext.Output = os.Stdout
fullClusterContext.Format = formatter.NewFullClusterFormat(viper.GetString("output"))
fullClusterContext.SetFullCluster(*authApi, resp.GetData()[0])
fullClusterContext.Write()
return
}
clustersCtx := formatter.Context{
Output: os.Stdout,
Format: formatter.NewClusterFormat(viper.GetString("output")),
}
formatter.ClusterWrite(clustersCtx, resp.GetData())
},
}

func init() {
ClusterCmd.AddCommand(describeClusterCmd)
describeClusterCmd.Flags().String("cluster-name", "", "[REQUIRED] The name of the cluster to get details.")
describeClusterCmd.MarkFlagRequired("cluster-name")
}
52 changes: 5 additions & 47 deletions cmd/cluster/get_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,27 @@
package cluster

import (
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ybmAuthClient "github.com/yugabyte/ybm-cli/internal/client"
"github.com/yugabyte/ybm-cli/internal/formatter"
)

// getClusterCmd represents the cluster command
var getClusterCmd = &cobra.Command{
Use: "get",
Short: "Get clusters",
Long: "Get clusters",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("could not initiate api client: %s", err.Error())
}
authApi.GetInfo("", "")
clusterListRequest := authApi.ListClusters()
isGetByName := false
// if user filters by name, add it to the request
clusterName, _ := cmd.Flags().GetString("cluster-name")
if clusterName != "" {
clusterListRequest = clusterListRequest.Name(clusterName)
isGetByName = true
}

resp, r, err := clusterListRequest.Execute()

if err != nil {
logrus.Debugf("Full HTTP response: %v", r)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
describeClusterCmd.Run(cmd, args)
logrus.Warnln("\nThe command `ybm cluster get --cluster-name` is deprecated. Please use `ybm cluster describe --cluster-name` instead.")
} else {
listClusterCmd.Run(cmd, args)
logrus.Warnln("\nThe command `ybm cluster get` is deprecated. Please use `ybm cluster list` instead.")
}

if isGetByName && len(resp.GetData()) > 0 && viper.GetString("output") == "table" {
fullClusterContext := *formatter.NewFullClusterContext()
fullClusterContext.Output = os.Stdout
fullClusterContext.Format = formatter.NewFullClusterFormat(viper.GetString("output"))
fullClusterContext.SetFullCluster(*authApi, resp.GetData()[0])
fullClusterContext.Write()
return
}
clustersCtx := formatter.Context{
Output: os.Stdout,
Format: formatter.NewClusterFormat(viper.GetString("output")),
}
formatter.ClusterWrite(clustersCtx, resp.GetData())
},
}

func init() {
ClusterCmd.AddCommand(getClusterCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// getClusterCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// getClusterCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
getClusterCmd.Flags().String("cluster-name", "", "[OPTIONAL] The name of the cluster to get details.")
}
62 changes: 62 additions & 0 deletions cmd/cluster/list_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 cluster

import (
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ybmAuthClient "github.com/yugabyte/ybm-cli/internal/client"
"github.com/yugabyte/ybm-cli/internal/formatter"
)

var listClusterCmd = &cobra.Command{
Use: "list",
Short: "List clusters in YugabyteDB Managed",
Long: "List clusters in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("could not initiate api client: %s", err.Error())
}
authApi.GetInfo("", "")
clusterListRequest := authApi.ListClusters()
// if user filters by name, add it to the request
clusterName, _ := cmd.Flags().GetString("cluster-name")
if clusterName != "" {
clusterListRequest = clusterListRequest.Name(clusterName)
}

resp, r, err := clusterListRequest.Execute()

if err != nil {
logrus.Debugf("Full HTTP response: %v", r)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}

clustersCtx := formatter.Context{
Output: os.Stdout,
Format: formatter.NewClusterFormat(viper.GetString("output")),
}
formatter.ClusterWrite(clustersCtx, resp.GetData())
},
}

func init() {
ClusterCmd.AddCommand(listClusterCmd)
}
6 changes: 3 additions & 3 deletions cmd/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ var _ = Describe("Cluster", func() {
})
Context("with a valid Api token and default output table", func() {
It("should return list of cluster", func() {
cmd := exec.Command(compiledCLIPath, "cluster", "get")
cmd := exec.Command(compiledCLIPath, "cluster", "list")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait(2)
Expand All @@ -224,7 +224,7 @@ stunning-sole Dedicated 2.16.0.1-b7 ACTIVE 💚 us-west-2 1
ghttp.RespondWithJSONEncodedPtr(&statusCode, responseNetworkAllowList),
),
)
cmd := exec.Command(compiledCLIPath, "cluster", "get", "--cluster-name", "stunning-sole")
cmd := exec.Command(compiledCLIPath, "cluster", "describe", "--cluster-name", "stunning-sole")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait(2)
Expand Down Expand Up @@ -258,7 +258,7 @@ us-west-2 1 2 8GB 100GB`))
ghttp.VerifyFormKV("name", "test"),
),
)
cmd := exec.Command(compiledCLIPath, "cluster", "get", "--cluster-name", "test")
cmd := exec.Command(compiledCLIPath, "cluster", "describe", "--cluster-name", "test")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait(2)
Expand Down
18 changes: 17 additions & 1 deletion cmd/nal/network_allow_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ var NalCmd = &cobra.Command{
var getNetworkAllowListCmd = &cobra.Command{
Use: "get",
Short: "Get network allow list in YugabyteDB Managed",
Long: "Get network allow list in YugabyteDB Managed",
Long: `Get network allow list in YugabyteDB Managed`,
Run: func(cmd *cobra.Command, args []string) {
listNetworkAllowListCmd.Run(cmd, args)
logrus.Warnln("\nThe command `ybm network-allow-list get` is deprecated. Please use `ybm network-allow-list list` instead.")
},
}

var listNetworkAllowListCmd = &cobra.Command{
Use: "list",
Short: "List network allow lists in YugabyteDB Managed",
Long: "List network allow lists in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
Expand All @@ -61,6 +71,7 @@ var getNetworkAllowListCmd = &cobra.Command{
}

respFilter = resp.GetData()
// TODO: should we even allow this parameter to be specified when you do list?
if cmd.Flags().Changed("name") {
allowList, err := util.FindNetworkAllowList(resp.Data, nalName)
if err != nil {
Expand All @@ -79,6 +90,8 @@ var getNetworkAllowListCmd = &cobra.Command{
},
}

// TODO: decide if we need a describe

var createNetworkAllowListCmd = &cobra.Command{
Use: "create",
Short: "Create network allow lists in YugabyteDB Managed",
Expand Down Expand Up @@ -149,6 +162,9 @@ func init() {
NalCmd.AddCommand(getNetworkAllowListCmd)
getNetworkAllowListCmd.Flags().StringVarP(&nalName, "name", "n", "", "[OPTIONAL] The name of the Network Allow List.")

NalCmd.AddCommand(listNetworkAllowListCmd)
listNetworkAllowListCmd.Flags().StringVarP(&nalName, "name", "n", "", "[OPTIONAL] The name of the Network Allow List.")

NalCmd.AddCommand(createNetworkAllowListCmd)
createNetworkAllowListCmd.Flags().SortFlags = false
createNetworkAllowListCmd.Flags().StringSliceVarP(&nalIpAddrs, "ip-addr", "i", []string{}, "[REQUIRED] IP addresses included in the Network Allow List.")
Expand Down
Loading

0 comments on commit 7dc4602

Please sign in to comment.