-
Notifications
You must be signed in to change notification settings - Fork 6
DOCSP-46223: Add Disaster Recovery example script for go sdk #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cbullinger
wants to merge
3
commits into
mongodb:main
Choose a base branch
from
cbullinger:docsp-46223-disaster-recovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
generated-usage-examples/go/atlas-sdk-go/main.snippet.disaster-recovery.go
This file contains hidden or 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,116 @@ | ||
| // See entire project at https://github.com/mongodb/atlas-architecture-go-sdk | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
| "time" | ||
|
|
||
| "atlas-sdk-go/internal/auth" | ||
| "atlas-sdk-go/internal/config" | ||
| "atlas-sdk-go/internal/data/recovery" | ||
| "atlas-sdk-go/internal/typeutils" | ||
|
|
||
| "github.com/joho/godotenv" | ||
| "go.mongodb.org/atlas-sdk/v20250219001/admin" | ||
| ) | ||
|
|
||
| const ( | ||
| scenarioRegionalOutage = "regional-outage" | ||
| scenarioDataDeletion = "data-deletion" | ||
| ) | ||
|
|
||
| func main() { | ||
| envFile := ".env.production" | ||
| if err := godotenv.Load(envFile); err != nil { | ||
| log.Printf("Warning: could not load %s file: %v", envFile, err) | ||
| } | ||
|
|
||
| secrets, cfg, err := config.LoadAllFromEnv() | ||
| if err != nil { | ||
| log.Fatalf("Failed to load configuration %v", err) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute) | ||
| defer cancel() | ||
| client, err := auth.NewClient(ctx, cfg, secrets) | ||
| if err != nil { | ||
| log.Fatalf("Failed to initialize authentication client: %v", err) | ||
| } | ||
|
|
||
| opts, err := recovery.LoadDROptionsFromEnv(cfg.ProjectID) | ||
| if err != nil { | ||
| log.Fatalf("Configuration error: %v", err) | ||
| } | ||
|
|
||
| fmt.Printf("Starting disaster recovery scenario: %s\nProject: %s\nCluster: %s\n", opts.Scenario, opts.ProjectID, opts.ClusterName) | ||
|
|
||
| if opts.DryRun { | ||
| fmt.Println("DRY RUN: no write operations will be performed") | ||
| } | ||
|
|
||
| var summary string | ||
| var opErr error | ||
|
|
||
| switch opts.Scenario { | ||
| case scenarioRegionalOutage: | ||
| summary, opErr = simulateRegionalOutage(ctx, client, opts) | ||
| case scenarioDataDeletion: | ||
| summary, opErr = executeDataDeletionRestore(ctx, client, opts) | ||
| default: | ||
| opErr = fmt.Errorf("unsupported DR_SCENARIO '%s'", opts.Scenario) | ||
| } | ||
|
|
||
| if opErr != nil { | ||
| log.Fatalf("Scenario failed: %v", opErr) | ||
| } | ||
|
|
||
| fmt.Println("\n=== Summary ===") | ||
| fmt.Println(summary) | ||
| fmt.Println("Disaster recovery procedure completed.") | ||
| } | ||
|
|
||
| // executeDataDeletionRestore initiates a restore job for a specified snapshot in a MongoDB Atlas cluster. | ||
| func executeDataDeletionRestore(ctx context.Context, client *admin.APIClient, o recovery.DrOptions) (string, error) { | ||
| job := admin.DiskBackupSnapshotRestoreJob{SnapshotId: &o.SnapshotID, TargetClusterName: &o.ClusterName} | ||
| if o.DryRun { | ||
| return fmt.Sprintf("(dry-run) Would submit restore job for snapshot %s", o.SnapshotID), nil | ||
| } | ||
| _, _, err := client.CloudBackupsApi.CreateBackupRestoreJob(ctx, o.ProjectID, o.ClusterName, &job).Execute() | ||
| if err != nil { | ||
| return "", fmt.Errorf("create restore job: %w", err) | ||
| } | ||
| return fmt.Sprintf("Restore job submitted for snapshot %s", o.SnapshotID), nil | ||
| } | ||
|
|
||
| // simulateRegionalOutage modifies the electable node count in a target region for a MongoDB Atlas cluster. | ||
| func simulateRegionalOutage(ctx context.Context, client *admin.APIClient, o recovery.DrOptions) (string, error) { | ||
| cluster, _, err := client.ClustersApi.GetCluster(ctx, o.ProjectID, o.ClusterName).Execute() | ||
| if err != nil { | ||
| return "", fmt.Errorf("get cluster: %w", err) | ||
| } | ||
| if !cluster.HasReplicationSpecs() { | ||
| return "", fmt.Errorf("cluster has no replication specs") | ||
| } | ||
| repl := cluster.GetReplicationSpecs() | ||
| addedNodes, foundTarget := recovery.AddElectableNodesToRegion(repl, o.TargetRegion, o.AddNodes) | ||
| if !foundTarget { | ||
| return "", fmt.Errorf("target region '%s' not found in replication specs", o.TargetRegion) | ||
| } | ||
| zeroedRegions := 0 | ||
| if o.OutageRegion != "" { | ||
| zeroedRegions = recovery.ZeroElectableNodesInRegion(repl, o.OutageRegion) | ||
| } | ||
| payload := admin.NewClusterDescription20240805() | ||
| payload.SetReplicationSpecs(repl) | ||
| if o.DryRun { | ||
| return fmt.Sprintf("(dry-run) Would add %d electable nodes to %s%s", addedNodes, o.TargetRegion, typeutils.SuffixZeroed(zeroedRegions, o.OutageRegion)), nil | ||
| } | ||
| _, _, err = client.ClustersApi.UpdateCluster(ctx, o.ProjectID, o.ClusterName, payload).Execute() | ||
| if err != nil { | ||
| return "", fmt.Errorf("update cluster: %w", err) | ||
| } | ||
| return fmt.Sprintf("Added %d electable nodes to %s%s", addedNodes, o.TargetRegion, typeutils.SuffixZeroed(zeroedRegions, o.OutageRegion)), nil | ||
| } | ||
|
|
This file contains hidden or 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
115 changes: 115 additions & 0 deletions
115
generated-usage-examples/go/atlas-sdk-go/project-copy/examples/recovery/main.go
This file contains hidden or 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,115 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
| "time" | ||
|
|
||
| "atlas-sdk-go/internal/auth" | ||
| "atlas-sdk-go/internal/config" | ||
| "atlas-sdk-go/internal/data/recovery" | ||
| "atlas-sdk-go/internal/typeutils" | ||
|
|
||
| "github.com/joho/godotenv" | ||
| "go.mongodb.org/atlas-sdk/v20250219001/admin" | ||
| ) | ||
|
|
||
| const ( | ||
| scenarioRegionalOutage = "regional-outage" | ||
| scenarioDataDeletion = "data-deletion" | ||
| ) | ||
|
|
||
| func main() { | ||
| envFile := ".env.production" | ||
| if err := godotenv.Load(envFile); err != nil { | ||
| log.Printf("Warning: could not load %s file: %v", envFile, err) | ||
| } | ||
|
|
||
| secrets, cfg, err := config.LoadAllFromEnv() | ||
| if err != nil { | ||
| log.Fatalf("Failed to load configuration %v", err) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute) | ||
| defer cancel() | ||
| client, err := auth.NewClient(ctx, cfg, secrets) | ||
| if err != nil { | ||
| log.Fatalf("Failed to initialize authentication client: %v", err) | ||
| } | ||
|
|
||
| opts, err := recovery.LoadDROptionsFromEnv(cfg.ProjectID) | ||
| if err != nil { | ||
| log.Fatalf("Configuration error: %v", err) | ||
| } | ||
|
|
||
| fmt.Printf("Starting disaster recovery scenario: %s\nProject: %s\nCluster: %s\n", opts.Scenario, opts.ProjectID, opts.ClusterName) | ||
|
|
||
| if opts.DryRun { | ||
| fmt.Println("DRY RUN: no write operations will be performed") | ||
| } | ||
|
|
||
| var summary string | ||
| var opErr error | ||
|
|
||
| switch opts.Scenario { | ||
| case scenarioRegionalOutage: | ||
| summary, opErr = simulateRegionalOutage(ctx, client, opts) | ||
| case scenarioDataDeletion: | ||
| summary, opErr = executeDataDeletionRestore(ctx, client, opts) | ||
| default: | ||
| opErr = fmt.Errorf("unsupported DR_SCENARIO '%s'", opts.Scenario) | ||
| } | ||
|
|
||
| if opErr != nil { | ||
| log.Fatalf("Scenario failed: %v", opErr) | ||
| } | ||
|
|
||
| fmt.Println("\n=== Summary ===") | ||
| fmt.Println(summary) | ||
| fmt.Println("Disaster recovery procedure completed.") | ||
| } | ||
|
|
||
| // executeDataDeletionRestore initiates a restore job for a specified snapshot in a MongoDB Atlas cluster. | ||
| func executeDataDeletionRestore(ctx context.Context, client *admin.APIClient, o recovery.DrOptions) (string, error) { | ||
| job := admin.DiskBackupSnapshotRestoreJob{SnapshotId: &o.SnapshotID, TargetClusterName: &o.ClusterName} | ||
| if o.DryRun { | ||
| return fmt.Sprintf("(dry-run) Would submit restore job for snapshot %s", o.SnapshotID), nil | ||
| } | ||
| _, _, err := client.CloudBackupsApi.CreateBackupRestoreJob(ctx, o.ProjectID, o.ClusterName, &job).Execute() | ||
| if err != nil { | ||
| return "", fmt.Errorf("create restore job: %w", err) | ||
| } | ||
| return fmt.Sprintf("Restore job submitted for snapshot %s", o.SnapshotID), nil | ||
| } | ||
|
|
||
| // simulateRegionalOutage modifies the electable node count in a target region for a MongoDB Atlas cluster. | ||
| func simulateRegionalOutage(ctx context.Context, client *admin.APIClient, o recovery.DrOptions) (string, error) { | ||
| cluster, _, err := client.ClustersApi.GetCluster(ctx, o.ProjectID, o.ClusterName).Execute() | ||
| if err != nil { | ||
| return "", fmt.Errorf("get cluster: %w", err) | ||
| } | ||
| if !cluster.HasReplicationSpecs() { | ||
| return "", fmt.Errorf("cluster has no replication specs") | ||
| } | ||
| repl := cluster.GetReplicationSpecs() | ||
| addedNodes, foundTarget := recovery.AddElectableNodesToRegion(repl, o.TargetRegion, o.AddNodes) | ||
| if !foundTarget { | ||
| return "", fmt.Errorf("target region '%s' not found in replication specs", o.TargetRegion) | ||
| } | ||
| zeroedRegions := 0 | ||
| if o.OutageRegion != "" { | ||
| zeroedRegions = recovery.ZeroElectableNodesInRegion(repl, o.OutageRegion) | ||
| } | ||
| payload := admin.NewClusterDescription20240805() | ||
| payload.SetReplicationSpecs(repl) | ||
| if o.DryRun { | ||
| return fmt.Sprintf("(dry-run) Would add %d electable nodes to %s%s", addedNodes, o.TargetRegion, typeutils.SuffixZeroed(zeroedRegions, o.OutageRegion)), nil | ||
| } | ||
| _, _, err = client.ClustersApi.UpdateCluster(ctx, o.ProjectID, o.ClusterName, payload).Execute() | ||
| if err != nil { | ||
| return "", fmt.Errorf("update cluster: %w", err) | ||
| } | ||
| return fmt.Sprintf("Added %d electable nodes to %s%s", addedNodes, o.TargetRegion, typeutils.SuffixZeroed(zeroedRegions, o.OutageRegion)), nil | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks good here in terms of output files, and I was able to build and execute this new example, but same caveat applies that I wasn't able to fully complete either scenario due to configuration constraints.