Skip to content

Commit ef1eb45

Browse files
committed
Apply Dachary feedback 2
1 parent 8717025 commit ef1eb45

45 files changed

Lines changed: 648 additions & 341 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

generated-usage-examples/go/atlas-sdk-go/main.snippet.archive-collections.go

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
"log"
8-
"os"
98
"time"
109

1110
"atlas-sdk-go/internal/archive"
@@ -21,8 +20,7 @@ func main() {
2120
log.Printf("Warning: could not load %s file: %v", envFile, err)
2221
}
2322

24-
configPath := os.Getenv("CONFIG_FILE")
25-
secrets, cfg, err := config.LoadAll(configPath)
23+
secrets, cfg, err := config.LoadAllFromEnv()
2624
if err != nil {
2725
log.Fatalf("Failed to load configuration %v", err)
2826
}
@@ -51,25 +49,62 @@ func main() {
5149

5250
// Connect to each cluster and analyze collections for archiving
5351
failedArchives := 0
52+
skippedCandidates := 0
5453
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+
5562
for _, cluster := range clusters.GetResults() {
5663
clusterName := cluster.GetName()
5764
fmt.Printf("\n=== Analyzing cluster: %s ===", clusterName)
5865

59-
// Find collections suitable for archiving based on specific criteria.
60-
// NOTE: The actual implementation of this function would involve more complex logic
61-
// to determine which collections are eligible for archiving.
62-
candidates := archive.CollectionsForArchiving(ctx, client, projectID, clusterName)
66+
// Find collections suitable for archiving based on demo criteria.
67+
// This simplified example first selects all collections with counts, and then filtering 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+
}
6390
totalCandidates += len(candidates)
6491
fmt.Printf("\nFound %d collections eligible for archiving in cluster %s\n",
6592
len(candidates), clusterName)
6693

6794
// Configure online archive for each candidate collection
6895
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+
69104
fmt.Printf("- Configuring archive for %s.%s\n",
70105
candidate.DatabaseName, candidate.CollectionName)
71106

72-
configureErr := archive.ConfigureOnlineArchive(ctx, client, projectID, clusterName, candidate)
107+
configureErr := archive.ConfigureOnlineArchive(ctx, client, projectID, clusterName, candidate, opts)
73108
if configureErr != nil {
74109
fmt.Printf(" Failed to configure archive: %v\n", configureErr)
75110
failedArchives++
@@ -81,8 +116,11 @@ func main() {
81116
}
82117
}
83118

119+
if skippedCandidates > 0 {
120+
fmt.Printf("\nINFO: Skipped %d of %d candidates due to validation errors\n", skippedCandidates, totalCandidates)
121+
}
84122
if failedArchives > 0 {
85-
fmt.Printf("\nWARNING: %d of %d archive configurations failed\n", failedArchives, totalCandidates)
123+
fmt.Printf("WARNING: %d of %d archive configurations failed (excluding skipped)\n", failedArchives, totalCandidates-skippedCandidates)
86124
}
87125

88126
fmt.Println("Archive analysis and configuration completed.")

generated-usage-examples/go/atlas-sdk-go/main.snippet.get-logs.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
"log"
8-
"os"
98

109
"atlas-sdk-go/internal/auth"
1110
"atlas-sdk-go/internal/config"
@@ -22,8 +21,7 @@ func main() {
2221
log.Printf("Warning: could not load %s file: %v", envFile, err)
2322
}
2423

25-
configPath := os.Getenv("CONFIG_FILE")
26-
secrets, cfg, err := config.LoadAll(configPath)
24+
secrets, cfg, err := config.LoadAllFromEnv()
2725
if err != nil {
2826
log.Fatalf("Failed to load configuration %v", err)
2927
}

generated-usage-examples/go/atlas-sdk-go/main.snippet.get-metrics-dev.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"log"
9-
"os"
109

1110
"atlas-sdk-go/internal/auth"
1211
"atlas-sdk-go/internal/config"
@@ -22,8 +21,7 @@ func main() {
2221
log.Printf("Warning: could not load %s file: %v", envFile, err)
2322
}
2423

25-
configPath := os.Getenv("CONFIG_FILE")
26-
secrets, cfg, err := config.LoadAll(configPath)
24+
secrets, cfg, err := config.LoadAllFromEnv()
2725
if err != nil {
2826
log.Fatalf("Failed to load configuration %v", err)
2927
}

generated-usage-examples/go/atlas-sdk-go/main.snippet.get-metrics-prod.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"log"
9-
"os"
109

1110
"atlas-sdk-go/internal/auth"
1211
"atlas-sdk-go/internal/config"
@@ -23,8 +22,7 @@ func main() {
2322
log.Printf("Warning: could not load %s file: %v", envFile, err)
2423
}
2524

26-
configPath := os.Getenv("CONFIG_FILE")
27-
secrets, cfg, err := config.LoadAll(configPath)
25+
secrets, cfg, err := config.LoadAllFromEnv()
2826
if err != nil {
2927
log.Fatalf("Failed to load configuration %v", err)
3028
}

generated-usage-examples/go/atlas-sdk-go/main.snippet.historical-billing.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
"log"
8-
"os"
98
"time"
109

1110
"atlas-sdk-go/internal/auth"
@@ -23,8 +22,7 @@ func main() {
2322
if err := godotenv.Load(envFile); err != nil {
2423
log.Printf("Warning: could not load %s file: %v", envFile, err)
2524
}
26-
configPath := os.Getenv("CONFIG_PATH")
27-
secrets, cfg, err := config.LoadAll(configPath)
25+
secrets, cfg, err := config.LoadAllFromEnv()
2826
if err != nil {
2927
log.Fatalf("Failed to load configuration %v", err)
3028
}

generated-usage-examples/go/atlas-sdk-go/main.snippet.line-items.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
"log"
8-
"os"
98

109
"github.com/joho/godotenv"
1110
"go.mongodb.org/atlas-sdk/v20250219001/admin"
@@ -23,8 +22,7 @@ func main() {
2322
log.Printf("Warning: could not load %s file: %v", envFile, err)
2423
}
2524

26-
configPath := os.Getenv("CONFIG_FILE")
27-
secrets, cfg, err := config.LoadAll(configPath)
25+
secrets, cfg, err := config.LoadAllFromEnv()
2826
if err != nil {
2927
log.Fatalf("Failed to load configuration %v", err)
3028
}

generated-usage-examples/go/atlas-sdk-go/main.snippet.linked-billing.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
"log"
8-
"os"
98

109
"atlas-sdk-go/internal/auth"
1110
"atlas-sdk-go/internal/billing"
@@ -21,8 +20,7 @@ func main() {
2120
log.Printf("Warning: could not load %s file: %v", envFile, err)
2221
}
2322

24-
configPath := os.Getenv("CONFIG_FILE")
25-
secrets, cfg, err := config.LoadAll(configPath)
23+
secrets, cfg, err := config.LoadAllFromEnv()
2624
if err != nil {
2725
log.Fatalf("Failed to load configuration %v", err)
2826
}

generated-usage-examples/go/atlas-sdk-go/project-copy/.gitignore

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# Secrets (keep example)
2-
tmp/.env
2+
.env
33
!.env.example
4-
tmp/.env.development
54
.env.production
6-
tmp/.env.test
75

8-
# config files (keep example)
9-
configs
6+
# Configs (keep example)
7+
configs/config.json
108
!configs/config.example.json
119

1210
# temporary files

generated-usage-examples/go/atlas-sdk-go/project-copy/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ and improvements to existing code.
6464
MONGODB_ATLAS_SERVICE_ACCOUNT_ID=<your_service_account_id>
6565
MONGODB_ATLAS_SERVICE_ACCOUNT_SECRET=<your_service_account_secret>
6666
ATLAS_DOWNLOADS_DIR="tmp/atlas_downloads" # optional download directory
67-
CONFIG_PATH="configs/config.development.json"
67+
CONFIG_PATH="configs/config.development.json" # optional path to Atlas config file
6868
```
6969
> **NOTE:** For production, use a secrets manager (e.g. HashiCorp Vault, AWS Secrets Manager)
7070
> instead of environment variables.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"MONGODB_ATLAS_BASE_URL": "https://cloud.mongodb.com",
3+
"ATLAS_ORG_ID": "<your-organization-id>",
4+
"ATLAS_PROJECT_ID": "<your-project-id>",
5+
"ATLAS_CLUSTER_NAME": "<clusterName>",
6+
"ATLAS_PROCESS_ID": "<clusterName-shard-00-00.hostSuffix.mongodb.net:port>"
7+
}

0 commit comments

Comments
 (0)