|
| 1 | +// See entire project at https://github.com/mongodb/atlas-architecture-go-sdk |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "time" |
| 9 | + |
| 10 | + "atlas-sdk-go/internal/archive" |
| 11 | + "atlas-sdk-go/internal/auth" |
| 12 | + "atlas-sdk-go/internal/config" |
| 13 | + |
| 14 | + "github.com/joho/godotenv" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + envFile := ".env.production" |
| 19 | + if err := godotenv.Load(envFile); err != nil { |
| 20 | + log.Printf("Warning: could not load %s file: %v", envFile, err) |
| 21 | + } |
| 22 | + |
| 23 | + secrets, cfg, err := config.LoadAllFromEnv() |
| 24 | + if err != nil { |
| 25 | + log.Fatalf("Failed to load configuration %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) |
| 29 | + defer cancel() |
| 30 | + client, err := auth.NewClient(ctx, cfg, secrets) |
| 31 | + if err != nil { |
| 32 | + log.Fatalf("Failed to initialize authentication client: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + projectID := cfg.ProjectID |
| 36 | + if projectID == "" { |
| 37 | + log.Fatal("Failed to find Project ID in configuration") |
| 38 | + } |
| 39 | + |
| 40 | + fmt.Printf("Starting archive analysis for project: %s\n", projectID) |
| 41 | + |
| 42 | + // Get all clusters in the project |
| 43 | + clusters, _, err := client.ClustersApi.ListClusters(ctx, projectID).Execute() |
| 44 | + if err != nil { |
| 45 | + log.Fatalf("Failed to list clusters: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + fmt.Printf("\nFound %d clusters to analyze\n", len(clusters.GetResults())) |
| 49 | + |
| 50 | + // Connect to each cluster and analyze collections for archiving |
| 51 | + failedArchives := 0 |
| 52 | + skippedCandidates := 0 |
| 53 | + totalCandidates := 0 |
| 54 | + |
| 55 | + // Create archive options with custom settings |
| 56 | + opts := archive.DefaultOptions() |
| 57 | + opts.DefaultRetentionMultiplier = 2 |
| 58 | + opts.MinimumRetentionDays = 30 |
| 59 | + opts.EnableDataExpiration = true |
| 60 | + opts.ArchiveSchedule = "DAILY" |
| 61 | + |
| 62 | + for _, cluster := range clusters.GetResults() { |
| 63 | + clusterName := cluster.GetName() |
| 64 | + fmt.Printf("\n=== Analyzing cluster: %s ===", clusterName) |
| 65 | + |
| 66 | + // Find collections suitable for archiving based on demo criteria. |
| 67 | + // This simplified example first selects all collections with counts, and then filters them. |
| 68 | + // NOTE: In a real implementation, you would analyze collections based on size, age, |
| 69 | + // access patterns, and other factors to determine candidates for archiving. |
| 70 | + stats := archive.ListCollectionsWithCounts(ctx, client, projectID, clusterName) |
| 71 | + candidates := make([]archive.Candidate, 0) |
| 72 | + const docThreshold = 100000 |
| 73 | + for _, s := range stats { |
| 74 | + // Skip internal databases |
| 75 | + if s.DatabaseName == "admin" || s.DatabaseName == "local" || s.DatabaseName == "config" { |
| 76 | + continue |
| 77 | + } |
| 78 | + // Demo criterion: collections with >= 100k documents |
| 79 | + if s.EstimatedCount >= docThreshold { |
| 80 | + candidates = append(candidates, archive.Candidate{ |
| 81 | + DatabaseName: s.DatabaseName, |
| 82 | + CollectionName: s.CollectionName, |
| 83 | + DateField: "createdAt", |
| 84 | + DateFormat: "DATE", |
| 85 | + RetentionDays: 90, |
| 86 | + PartitionFields: []string{"createdAt"}, |
| 87 | + }) |
| 88 | + } |
| 89 | + } |
| 90 | + totalCandidates += len(candidates) |
| 91 | + fmt.Printf("\nFound %d collections eligible for archiving in cluster %s\n", |
| 92 | + len(candidates), clusterName) |
| 93 | + |
| 94 | + // Configure online archive for each candidate collection |
| 95 | + for _, candidate := range candidates { |
| 96 | + // Pre-validate candidate before attempting configuration |
| 97 | + if err := archive.ValidateCandidate(candidate, opts); err != nil { |
| 98 | + fmt.Printf("- Skipping %s.%s: invalid candidate: %v\n", |
| 99 | + candidate.DatabaseName, candidate.CollectionName, err) |
| 100 | + skippedCandidates++ |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + fmt.Printf("- Configuring archive for %s.%s\n", |
| 105 | + candidate.DatabaseName, candidate.CollectionName) |
| 106 | + |
| 107 | + configureErr := archive.ConfigureOnlineArchive(ctx, client, projectID, clusterName, candidate, opts) |
| 108 | + if configureErr != nil { |
| 109 | + fmt.Printf(" Failed to configure archive: %v\n", configureErr) |
| 110 | + failedArchives++ |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + fmt.Printf(" Successfully configured online archive for %s.%s\n", |
| 115 | + candidate.DatabaseName, candidate.CollectionName) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if skippedCandidates > 0 { |
| 120 | + fmt.Printf("\nINFO: Skipped %d of %d candidates due to validation errors\n", skippedCandidates, totalCandidates) |
| 121 | + } |
| 122 | + if failedArchives > 0 { |
| 123 | + fmt.Printf("WARNING: %d of %d archive configurations failed (excluding skipped)\n", failedArchives, totalCandidates-skippedCandidates) |
| 124 | + } |
| 125 | + |
| 126 | + fmt.Println("Archive analysis and configuration completed.") |
| 127 | +} |
| 128 | + |
0 commit comments