Skip to content

Commit

Permalink
Redesign CLI for DR to not take cluster name when not required (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
posriniv authored Oct 28, 2024
1 parent 63b5bb5 commit 18dfe83
Show file tree
Hide file tree
Showing 16 changed files with 29 additions and 40 deletions.
5 changes: 0 additions & 5 deletions cmd/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/spf13/cobra"
"github.com/yugabyte/ybm-cli/cmd/cluster/cert"
connectionpooling "github.com/yugabyte/ybm-cli/cmd/cluster/connection-pooling"
dr "github.com/yugabyte/ybm-cli/cmd/cluster/dr"
encryption "github.com/yugabyte/ybm-cli/cmd/cluster/encryption"
log_exporter "github.com/yugabyte/ybm-cli/cmd/cluster/log-exporter"
"github.com/yugabyte/ybm-cli/cmd/cluster/namespace"
Expand Down Expand Up @@ -74,8 +73,4 @@ func init() {
util.AddCommandIfFeatureFlag(ClusterCmd, connectionpooling.ConnectionPoolingCmd, util.CONNECTION_POOLING)
connectionpooling.ConnectionPoolingCmd.PersistentFlags().StringVarP(&connectionpooling.ClusterName, "cluster-name", "c", "", "[REQUIRED] The name of the cluster.")
connectionpooling.ConnectionPoolingCmd.MarkPersistentFlagRequired("cluster-name")

util.AddCommandIfFeatureFlag(ClusterCmd, dr.DrCmd, util.DR)
dr.DrCmd.PersistentFlags().StringVarP(&dr.ClusterName, "cluster-name", "c", "", "[REQUIRED] The name of the cluster.")
dr.DrCmd.MarkPersistentFlagRequired("cluster-name")
}
5 changes: 4 additions & 1 deletion cmd/cluster/dr/create_dr.go → cmd/dr/create_dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ var createDrCmd = &cobra.Command{
authApi.GetInfo("", "")

drName, _ := cmd.Flags().GetString("dr-name")
sourceClusterName, _ := cmd.Flags().GetString("source-cluster-name")
targetClusterName, _ := cmd.Flags().GetString("target-cluster-name")
databases, _ := cmd.Flags().GetStringArray("databases")
sourceClusterId, err := authApi.GetClusterIdByName(ClusterName)
sourceClusterId, err := authApi.GetClusterIdByName(sourceClusterName)
if err != nil {
logrus.Fatalf("Could not get cluster data: %s", ybmAuthClient.GetApiErrorDetails(err))
}
Expand Down Expand Up @@ -113,6 +114,8 @@ func init() {
DrCmd.AddCommand(createDrCmd)
createDrCmd.Flags().String("dr-name", "", "[REQUIRED] Name of the DR configuration.")
createDrCmd.MarkFlagRequired("dr-name")
createDrCmd.Flags().String("source-cluster-name", "", "[REQUIRED] Target cluster in the DR configuration.")
createDrCmd.MarkFlagRequired("source-cluster-name")
createDrCmd.Flags().String("target-cluster-name", "", "[REQUIRED] Target cluster in the DR configuration.")
createDrCmd.MarkFlagRequired("target-cluster-name")
createDrCmd.Flags().StringArray("databases", []string{}, "[REQUIRED] Databases to be replicated.")
Expand Down
9 changes: 4 additions & 5 deletions cmd/cluster/dr/delete_dr.go → cmd/dr/delete_dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var deleteDrCmd = &cobra.Command{
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("force", cmd.Flags().Lookup("force"))
drName, _ := cmd.Flags().GetString("dr-name")
msg := fmt.Sprintf("Are you sure you want to delete dr-name: %s for cluster: %s", drName, ClusterName)
msg := fmt.Sprintf("Are you sure you want to delete dr: %s", drName)
err := util.ConfirmCommand(msg, viper.GetBool("force"))
if err != nil {
logrus.Fatal(err)
Expand All @@ -48,16 +48,15 @@ var deleteDrCmd = &cobra.Command{
authApi.GetInfo("", "")

drName, _ := cmd.Flags().GetString("dr-name")
clusterID, err := authApi.GetClusterIdByName(ClusterName)
if err != nil {
logrus.Fatal(err)
}
drId, err := authApi.GetDrIdByName(clusterID, drName)
drId, clusterId, err := authApi.GetDrDetailsByName(drName)
if err != nil {
logrus.Fatal(err)
}

r, err := authApi.DeleteXClusterDr(clusterID, drId).Execute()
r, err := authApi.DeleteXClusterDr(clusterId, drId).Execute()
if err != nil {
logrus.Debugf("Full HTTP response: %v", r)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
Expand All @@ -66,7 +65,7 @@ var deleteDrCmd = &cobra.Command{
msg := fmt.Sprintf("The DR %s is being deleted", formatter.Colorize(drName, formatter.GREEN_COLOR))

if viper.GetBool("wait") {
returnStatus, err := authApi.WaitForTaskCompletion(clusterID, ybmclient.ENTITYTYPEENUM_CLUSTER, ybmclient.TASKTYPEENUM_DELETE_DR, []string{"FAILED", "SUCCEEDED"}, msg)
returnStatus, err := authApi.WaitForTaskCompletion(clusterId, ybmclient.ENTITYTYPEENUM_CLUSTER, ybmclient.TASKTYPEENUM_DELETE_DR, []string{"FAILED", "SUCCEEDED"}, msg)
if err != nil {
logrus.Fatalf("error when getting task status: %s", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/cluster/dr/describe_dr.go → cmd/dr/describe_dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ var describeDrCmd = &cobra.Command{
authApi.GetInfo("", "")

drName, _ := cmd.Flags().GetString("dr-name")
clusterId, err := authApi.GetClusterIdByName(ClusterName)
if err != nil {
logrus.Fatalf("Could not get cluster data: %s", ybmAuthClient.GetApiErrorDetails(err))
}
drId, err := authApi.GetDrIdByName(clusterId, drName)
drId, clusterId, err := authApi.GetDrDetailsByName(drName)
if err != nil {
logrus.Fatalf("Could not get DR data: %s", ybmAuthClient.GetApiErrorDetails(err))
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/cluster/dr/dr.go → cmd/dr/dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/spf13/cobra"
)

var ClusterName string

var DrCmd = &cobra.Command{
Use: "dr",
Short: "Manage DR for a cluster.",
Expand Down
3 changes: 1 addition & 2 deletions cmd/cluster/dr/failover_dr.go → cmd/dr/failover_dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ var failoverDrCmd = &cobra.Command{

drName, _ := cmd.Flags().GetString("dr-name")
safetimes, _ := cmd.Flags().GetStringArray("safetimes")
clusterId, err := authApi.GetClusterIdByName(ClusterName)
if err != nil {
logrus.Fatalf("Could not get cluster data: %s", ybmAuthClient.GetApiErrorDetails(err))
}
drId, err := authApi.GetDrIdByName(clusterId, drName)
drId, clusterId, err := authApi.GetDrDetailsByName(drName)
if err != nil {
logrus.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/cluster/dr/list_dr.go → cmd/dr/list_dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ var listDrCmd = &cobra.Command{
}
authApi.GetInfo("", "")

clusterId, err := authApi.GetClusterIdByName(ClusterName)
if err != nil {
logrus.Fatal(err)
}
resp, r, err := authApi.ListXClusterDr(clusterId).Execute()
resp, r, err := authApi.ListXClusterDr().Execute()
if err != nil {
logrus.Debugf("Full HTTP response: %v", r)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
Expand Down
3 changes: 1 addition & 2 deletions cmd/cluster/dr/pause_dr.go → cmd/dr/pause_dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ var pauseDrCmd = &cobra.Command{

drName, _ := cmd.Flags().GetString("dr-name")
durationInMin, _ := cmd.Flags().GetInt32("duration")
clusterId, err := authApi.GetClusterIdByName(ClusterName)
if err != nil {
logrus.Fatalf("Could not get cluster data: %s", ybmAuthClient.GetApiErrorDetails(err))
}
drId, err := authApi.GetDrIdByName(clusterId, drName)
drId, clusterId, err := authApi.GetDrDetailsByName(drName)
if err != nil {
logrus.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/cluster/dr/restart_dr.go → cmd/dr/restart_dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ var restartDrCmd = &cobra.Command{

drName, _ := cmd.Flags().GetString("dr-name")
databases, _ := cmd.Flags().GetStringArray("databases")
clusterId, err := authApi.GetClusterIdByName(ClusterName)
if err != nil {
logrus.Fatalf("Could not get cluster data: %s", ybmAuthClient.GetApiErrorDetails(err))
}
drId, err := authApi.GetDrIdByName(clusterId, drName)
drId, clusterId, err := authApi.GetDrDetailsByName(drName)
if err != nil {
logrus.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/cluster/dr/resume_dr.go → cmd/dr/resume_dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ var resumeDrCmd = &cobra.Command{
authApi.GetInfo("", "")

drName, _ := cmd.Flags().GetString("dr-name")
clusterId, err := authApi.GetClusterIdByName(ClusterName)
if err != nil {
logrus.Fatalf("Could not get cluster data: %s", ybmAuthClient.GetApiErrorDetails(err))
}
drId, err := authApi.GetDrIdByName(clusterId, drName)
drId, clusterId, err := authApi.GetDrDetailsByName(drName)
if err != nil {
logrus.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/cluster/dr/switchover_dr.go → cmd/dr/switchover_dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ var switchoverDrCmd = &cobra.Command{
authApi.GetInfo("", "")

drName, _ := cmd.Flags().GetString("dr-name")
clusterId, err := authApi.GetClusterIdByName(ClusterName)
if err != nil {
logrus.Fatalf("Could not get cluster data: %s", ybmAuthClient.GetApiErrorDetails(err))
}
drId, err := authApi.GetDrIdByName(clusterId, drName)
drId, clusterId, err := authApi.GetDrDetailsByName(drName)
if err != nil {
logrus.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/cluster/dr/update_dr.go → cmd/dr/update_dr.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ var updateDrCmd = &cobra.Command{

drName, _ := cmd.Flags().GetString("dr-name")
databases, _ := cmd.Flags().GetStringArray("databases")
clusterId, err := authApi.GetClusterIdByName(ClusterName)
if err != nil {
logrus.Fatalf("Could not get cluster data: %s", ybmAuthClient.GetApiErrorDetails(err))
}
drId, err := authApi.GetDrIdByName(clusterId, drName)
drId, clusterId, err := authApi.GetDrDetailsByName(drName)
if err != nil {
logrus.Fatal(err)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/yugabyte/ybm-cli/cmd/cdc"
"github.com/yugabyte/ybm-cli/cmd/cluster"
"github.com/yugabyte/ybm-cli/cmd/db_audit_logs_exporter"
"github.com/yugabyte/ybm-cli/cmd/dr"
"github.com/yugabyte/ybm-cli/cmd/integration"
"github.com/yugabyte/ybm-cli/cmd/metrics_exporter"
"github.com/yugabyte/ybm-cli/cmd/nal"
Expand Down Expand Up @@ -138,6 +139,7 @@ func init() {
rootCmd.AddCommand(user.UserCmd)
rootCmd.AddCommand(metrics_exporter.MetricsExporterCmd)
rootCmd.AddCommand(integration.IntegrationCmd)
util.AddCommandIfFeatureFlag(rootCmd, dr.DrCmd, util.DR)
util.AddCommandIfFeatureFlag(rootCmd, db_audit_logs_exporter.DbAuditLogsExporterCmd, util.DB_AUDIT_LOGS)
util.AddCommandIfFeatureFlag(rootCmd, tools.ToolsCmd, util.TOOLS)
util.AddCommandIfFeatureFlag(rootCmd, cdc.CdcCmd, util.CDC)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
github.com/yugabyte/yugabytedb-managed-go-client-internal v0.0.0-20241021102551-c12cda1dda14
github.com/yugabyte/yugabytedb-managed-go-client-internal v0.0.0-20241026194158-9358bc3fbd31
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
golang.org/x/mod v0.20.0
golang.org/x/term v0.23.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1:J6v8awz+me+xeb/cUTotKgceAYouhIB3pjzgRd6IlGk=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA=
github.com/yugabyte/yugabytedb-managed-go-client-internal v0.0.0-20241021102551-c12cda1dda14 h1:6k1ZmB/Wngtwo0SzTGl+Ky0Vpd32L+J9HVxpRY0m1/w=
github.com/yugabyte/yugabytedb-managed-go-client-internal v0.0.0-20241021102551-c12cda1dda14/go.mod h1:5vW0xIzIZw+1djkiWKx0qqNmqbRBSf4mjc4qw8lIMik=
github.com/yugabyte/yugabytedb-managed-go-client-internal v0.0.0-20241026194158-9358bc3fbd31 h1:4YdZLlhG8hWPCjb9S7TIZ7THPtaLjgGDEdJnITO8IVU=
github.com/yugabyte/yugabytedb-managed-go-client-internal v0.0.0-20241026194158-9358bc3fbd31/go.mod h1:5vW0xIzIZw+1djkiWKx0qqNmqbRBSf4mjc4qw8lIMik=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
16 changes: 8 additions & 8 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ func (a *AuthApiClient) GetClusterByName(clusterName string) (ybmclient.ClusterD
return ybmclient.ClusterData{}, fmt.Errorf("could not get cluster data for cluster name: %s", clusterName)
}

func (a *AuthApiClient) GetDrByName(clusterId string, drName string) (ybmclient.XClusterDrData, error) {
drResp, resp, err := a.ListXClusterDr(clusterId).Execute()
func (a *AuthApiClient) GetDrByName(drName string) (ybmclient.XClusterDrData, error) {
drResp, resp, err := a.ListXClusterDr().Execute()
if err != nil {
b, _ := httputil.DumpResponse(resp, true)
logrus.Debug(string(b))
Expand Down Expand Up @@ -587,13 +587,13 @@ func (a *AuthApiClient) GetClusterIdByName(clusterName string) (string, error) {
return "", fmt.Errorf("could not get cluster data for cluster name: %s", clusterName)
}

func (a *AuthApiClient) GetDrIdByName(clusterId string, drName string) (string, error) {
drData, err := a.GetDrByName(clusterId, drName)
func (a *AuthApiClient) GetDrDetailsByName(drName string) (string, string, error) {
drData, err := a.GetDrByName(drName)
if err == nil {
return drData.Info.GetId(), nil
return drData.Info.GetId(), drData.Info.GetSourceClusterId(), nil
}

return "", fmt.Errorf("could not get dr data for dr name: %s", drName)
return "", "", fmt.Errorf("could not get dr data for dr name: %s", drName)
}

func (a *AuthApiClient) CreateCluster() ybmclient.ApiCreateClusterRequest {
Expand Down Expand Up @@ -1718,8 +1718,8 @@ func (a *AuthApiClient) GetXClusterDr(clusterId string, drId string) ybmclient.A
return a.ApiClient.XclusterDrApi.GetXClusterDr(a.ctx, a.AccountID, a.ProjectID, clusterId, drId)
}

func (a *AuthApiClient) ListXClusterDr(clusterId string) ybmclient.ApiListXClusterDrRequest {
return a.ApiClient.XclusterDrApi.ListXClusterDr(a.ctx, a.AccountID, a.ProjectID, clusterId)
func (a *AuthApiClient) ListXClusterDr() ybmclient.ApiListAllXClusterDrInAccountRequest {
return a.ApiClient.XclusterDrApi.ListAllXClusterDrInAccount(a.ctx, a.AccountID, a.ProjectID)
}

func (a *AuthApiClient) DeleteXClusterDr(clusterId string, drId string) ybmclient.ApiDeleteXClusterDrRequest {
Expand Down

0 comments on commit 18dfe83

Please sign in to comment.