-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support update and switchover DR (#260)
- Loading branch information
Showing
6 changed files
with
228 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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 dr | ||
|
||
import ( | ||
"fmt" | ||
"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" | ||
ybmclient "github.com/yugabyte/yugabytedb-managed-go-client-internal" | ||
) | ||
|
||
var switchoverDrCmd = &cobra.Command{ | ||
Use: "switchover", | ||
Short: "Switchover DR for a cluster", | ||
Long: `Switchover DR for a cluster`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
authApi, err := ybmAuthClient.NewAuthApiClient() | ||
if err != nil { | ||
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
} | ||
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) | ||
if err != nil { | ||
logrus.Fatal(err) | ||
} | ||
|
||
response, err := authApi.SwitchoverXClusterDr(clusterId, drId).Execute() | ||
if err != nil { | ||
logrus.Debugf("Full HTTP response: %v", response) | ||
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
} | ||
|
||
msg := fmt.Sprintf("Switchover is in progress for the DR %s ", formatter.Colorize(drName, formatter.GREEN_COLOR)) | ||
|
||
if viper.GetBool("wait") { | ||
returnStatus, err := authApi.WaitForTaskCompletion(clusterId, ybmclient.ENTITYTYPEENUM_CLUSTER, ybmclient.TASKTYPEENUM_DR_SWITCHOVER, []string{"FAILED", "SUCCEEDED"}, msg) | ||
if err != nil { | ||
logrus.Fatalf("error when getting task status: %s", err) | ||
} | ||
if returnStatus != "SUCCEEDED" { | ||
logrus.Fatalf("Operation failed with error: %s", returnStatus) | ||
} | ||
fmt.Printf("Switchover for DR config %s is successful\n", formatter.Colorize(drName, formatter.GREEN_COLOR)) | ||
|
||
drGetResp, r, err := authApi.GetXClusterDr(clusterId, drId).Execute() | ||
if err != nil { | ||
logrus.Debugf("Full HTTP response: %v", r) | ||
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
} | ||
drCtx := formatter.Context{ | ||
Output: os.Stdout, | ||
Format: formatter.NewDrFormat(viper.GetString("output")), | ||
} | ||
|
||
formatter.DrWrite(drCtx, []ybmclient.XClusterDrData{drGetResp.GetData()}, *authApi) | ||
} else { | ||
fmt.Println(msg) | ||
} | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
DrCmd.AddCommand(switchoverDrCmd) | ||
switchoverDrCmd.Flags().String("dr-name", "", "[REQUIRED] Name of the DR configuration.") | ||
switchoverDrCmd.MarkFlagRequired("dr-name") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// 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 dr | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"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" | ||
ybmclient "github.com/yugabyte/yugabytedb-managed-go-client-internal" | ||
) | ||
|
||
var updateDrCmd = &cobra.Command{ | ||
Use: "update", | ||
Short: "Update DR for a cluster", | ||
Long: `Update DR for a cluster`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
authApi, err := ybmAuthClient.NewAuthApiClient() | ||
if err != nil { | ||
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
} | ||
authApi.GetInfo("", "") | ||
|
||
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) | ||
if err != nil { | ||
logrus.Fatal(err) | ||
} | ||
namespacesResp, r, err := authApi.GetClusterNamespaces(clusterId).Execute() | ||
if err != nil { | ||
logrus.Debugf("Full HTTP response: %v", r) | ||
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
} | ||
dbNameToIdMap := map[string]string{} | ||
for _, namespace := range namespacesResp.Data { | ||
dbNameToIdMap[namespace.GetName()] = namespace.GetId() | ||
} | ||
databaseIds := []string{} | ||
for _, databaseString := range databases { | ||
for _, database := range strings.Split(databaseString, ",") { | ||
if databaseId, exists := dbNameToIdMap[database]; exists { | ||
databaseIds = append(databaseIds, databaseId) | ||
} else { | ||
logrus.Fatalf("The database %s doesn't exist", database) | ||
} | ||
} | ||
} | ||
|
||
updateDrRequest := ybmclient.NewEditXClusterDrRequest(*ybmclient.NewEditXClusterDrSpec(databaseIds)) | ||
drResp, response, err := authApi.EditXClusterDr(clusterId, drId).EditXClusterDrRequest(*updateDrRequest).Execute() | ||
if err != nil { | ||
logrus.Debugf("Full HTTP response: %v", response) | ||
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
} | ||
|
||
msg := fmt.Sprintf("DR config %s is being updated", formatter.Colorize(drName, formatter.GREEN_COLOR)) | ||
|
||
if viper.GetBool("wait") { | ||
returnStatus, err := authApi.WaitForTaskCompletion(clusterId, ybmclient.ENTITYTYPEENUM_CLUSTER, ybmclient.TASKTYPEENUM_EDIT_DR, []string{"FAILED", "SUCCEEDED"}, msg) | ||
if err != nil { | ||
logrus.Fatalf("error when getting task status: %s", err) | ||
} | ||
if returnStatus != "SUCCEEDED" { | ||
logrus.Fatalf("Operation failed with error: %s", returnStatus) | ||
} | ||
fmt.Printf("DR config %s has been updated\n", formatter.Colorize(drName, formatter.GREEN_COLOR)) | ||
|
||
drGetResp, r, err := authApi.GetXClusterDr(clusterId, drId).Execute() | ||
if err != nil { | ||
logrus.Debugf("Full HTTP response: %v", r) | ||
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
} | ||
drResp = drGetResp | ||
} else { | ||
fmt.Println(msg) | ||
} | ||
|
||
drCtx := formatter.Context{ | ||
Output: os.Stdout, | ||
Format: formatter.NewDrFormat(viper.GetString("output")), | ||
} | ||
|
||
formatter.DrWrite(drCtx, []ybmclient.XClusterDrData{drResp.GetData()}, *authApi) | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
DrCmd.AddCommand(updateDrCmd) | ||
updateDrCmd.Flags().String("dr-name", "", "[REQUIRED] Name of the DR configuration.") | ||
updateDrCmd.MarkFlagRequired("dr-name") | ||
updateDrCmd.Flags().StringArray("databases", []string{}, "[REQUIRED] Databases to be replicated.") | ||
updateDrCmd.MarkFlagRequired("databases") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters