Skip to content

Commit 26ce74e

Browse files
committed
Snip and copy files
1 parent 340ce32 commit 26ce74e

43 files changed

Lines changed: 926 additions & 359 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.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// See entire project at https://github.com/mongodb/atlas-architecture-go-sdk
2+
package main
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"time"
8+
9+
"atlas-sdk-go/internal/archive"
10+
"atlas-sdk-go/internal/auth"
11+
"atlas-sdk-go/internal/config"
12+
"atlas-sdk-go/internal/errors"
13+
)
14+
15+
func main() {
16+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
17+
defer cancel()
18+
19+
// Load application context with configuration and secrets for the environment
20+
explicitEnv := "production"
21+
appCtx, err := config.LoadAppContextWithContext(ctx, explicitEnv, false)
22+
if err != nil {
23+
errors.ExitWithError("Failed to load configuration", err)
24+
}
25+
26+
client, err := auth.NewClient(appCtx.Config, appCtx.Secrets)
27+
if err != nil {
28+
errors.ExitWithError("Failed to initialize Atlas client", err)
29+
}
30+
31+
projectID := appCtx.Config.ProjectID
32+
if projectID == "" {
33+
errors.ExitWithError("Project ID not found in configuration", nil)
34+
}
35+
36+
fmt.Println("Starting archive analysis for project:", projectID)
37+
38+
// Step 1: List all clusters in the project
39+
clusters, _, err := client.ClustersApi.ListClusters(ctx, projectID).Execute()
40+
if err != nil {
41+
errors.ExitWithError("Failed to list clusters", err)
42+
}
43+
44+
fmt.Printf("Found %d clusters to analyze", len(clusters.GetResults()))
45+
46+
// Step 2: Process each cluster
47+
failedArchives := 0
48+
for _, cluster := range clusters.GetResults() {
49+
clusterName := cluster.GetName()
50+
fmt.Printf("Analyzing cluster: %s", clusterName)
51+
52+
// Step 3: Find collections suitable for archiving
53+
// NOTE: In a real production scenario, you would customize the collection analysis logic to match your specific data patterns.
54+
candidates := archive.CollectionsForArchiving(ctx, client, projectID, clusterName)
55+
fmt.Printf("Found %d collections eligible for archiving in cluster %s",
56+
len(candidates), clusterName)
57+
58+
// Step 4: Configure online archive for each candidate collection
59+
for _, candidate := range candidates {
60+
fmt.Printf("Configuring archive for %s.%s",
61+
candidate.DatabaseName, candidate.CollectionName)
62+
63+
configureErr := archive.ConfigureOnlineArchive(ctx, client, projectID, clusterName, candidate)
64+
if configureErr != nil {
65+
fmt.Printf("Failed to configure archive: %v", configureErr)
66+
failedArchives++
67+
continue
68+
}
69+
70+
fmt.Printf("Successfully configured online archive for %s.%s",
71+
candidate.DatabaseName, candidate.CollectionName)
72+
}
73+
}
74+
75+
if failedArchives > 0 {
76+
fmt.Printf("Warning: %d archive configurations failed", failedArchives)
77+
}
78+
79+
fmt.Println("Archive analysis and configuration completed")
80+
}
81+

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@ import (
1212
"atlas-sdk-go/internal/fileutils"
1313
"atlas-sdk-go/internal/logs"
1414

15-
"github.com/joho/godotenv"
1615
"go.mongodb.org/atlas-sdk/v20250219001/admin"
1716
)
1817

1918
func main() {
20-
if err := godotenv.Load(); err != nil {
21-
log.Printf("Warning: .env file not loaded: %v", err)
22-
}
23-
24-
secrets, cfg, err := config.LoadAll("configs/config.json")
19+
configPath := "" // Use default config path for environment
20+
explicitEnv := "" // Use default environment
21+
secrets, cfg, err := config.LoadAll(configPath, explicitEnv)
2522
if err != nil {
2623
errors.ExitWithError("Failed to load configuration", err)
2724
}
@@ -39,13 +36,16 @@ func main() {
3936
HostName: cfg.HostName,
4037
LogName: "mongodb",
4138
}
39+
fmt.Printf("Request parameters: GroupID=%s, HostName=%s, LogName=%s\n",
40+
cfg.ProjectID, cfg.HostName, p.LogName)
4241
rc, err := logs.FetchHostLogs(ctx, client.MonitoringAndLogsApi, p)
4342
if err != nil {
4443
errors.ExitWithError("Failed to fetch logs", err)
4544
}
4645
defer fileutils.SafeClose(rc)
4746

4847
// Prepare output paths
48+
// If the ATLAS_DOWNLOADS_DIR env variable is set, it will be used as the base directory for output files
4949
outDir := "logs"
5050
prefix := fmt.Sprintf("%s_%s", p.HostName, p.LogName)
5151
gzPath, err := fileutils.GenerateOutputPath(outDir, prefix, "gz")

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,19 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"log"
98

109
"atlas-sdk-go/internal/auth"
1110
"atlas-sdk-go/internal/config"
1211
"atlas-sdk-go/internal/errors"
1312
"atlas-sdk-go/internal/metrics"
1413

15-
"github.com/joho/godotenv"
1614
"go.mongodb.org/atlas-sdk/v20250219001/admin"
1715
)
1816

1917
func main() {
20-
if err := godotenv.Load(); err != nil {
21-
log.Printf("Warning: .env file not loaded: %v", err)
22-
}
23-
24-
secrets, cfg, err := config.LoadAll("configs/config.json")
18+
configPath := "" // Use default config path for environment
19+
explicitEnv := "development"
20+
secrets, cfg, err := config.LoadAll(configPath, explicitEnv)
2521
if err != nil {
2622
errors.ExitWithError("Failed to load configuration", err)
2723
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,21 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"log"
98

109
"atlas-sdk-go/internal/errors"
1110

1211
"atlas-sdk-go/internal/auth"
1312
"atlas-sdk-go/internal/config"
1413

15-
"github.com/joho/godotenv"
1614
"go.mongodb.org/atlas-sdk/v20250219001/admin"
1715

1816
"atlas-sdk-go/internal/metrics"
1917
)
2018

2119
func main() {
22-
if err := godotenv.Load(); err != nil {
23-
log.Printf("Warning: .env file not loaded: %v", err)
24-
}
25-
26-
secrets, cfg, err := config.LoadAll("configs/config.json")
20+
configPath := "" // Use default config path for environment
21+
explicitEnv := "production"
22+
secrets, cfg, err := config.LoadAll(configPath, explicitEnv)
2723
if err != nil {
2824
errors.ExitWithError("Failed to load configuration", err)
2925
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ import (
1414
"atlas-sdk-go/internal/errors"
1515
"atlas-sdk-go/internal/fileutils"
1616

17-
"github.com/joho/godotenv"
1817
"go.mongodb.org/atlas-sdk/v20250219001/admin"
1918
)
2019

2120
func main() {
22-
if err := godotenv.Load(); err != nil {
23-
log.Printf("Warning: .env file not loaded: %v", err)
24-
}
25-
26-
secrets, cfg, err := config.LoadAll("configs/config.json")
21+
configPath := "" // Use default config path for environment
22+
explicitEnv := "" // Use default environment
23+
secrets, cfg, err := config.LoadAll(configPath, explicitEnv)
2724
if err != nil {
2825
errors.ExitWithError("Failed to load configuration", err)
2926
}

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

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

910
"go.mongodb.org/atlas-sdk/v20250219001/admin"
1011

@@ -14,16 +15,12 @@ import (
1415
"atlas-sdk-go/internal/data/export"
1516
"atlas-sdk-go/internal/errors"
1617
"atlas-sdk-go/internal/fileutils"
17-
18-
"github.com/joho/godotenv"
1918
)
2019

2120
func main() {
22-
if err := godotenv.Load(); err != nil {
23-
log.Printf("Warning: .env file not loaded: %v", err)
24-
}
25-
26-
secrets, cfg, err := config.LoadAll("configs/config.json")
21+
configPath := "" // Use default config path for environment
22+
explicitEnv := "" // Use default environment
23+
secrets, cfg, err := config.LoadAll(configPath, explicitEnv)
2724
if err != nil {
2825
errors.ExitWithError("Failed to load configuration", err)
2926
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@ package main
44
import (
55
"context"
66
"fmt"
7-
"log"
87

98
"atlas-sdk-go/internal/auth"
109
"atlas-sdk-go/internal/billing"
1110
"atlas-sdk-go/internal/config"
1211
"atlas-sdk-go/internal/errors"
1312

14-
"github.com/joho/godotenv"
1513
"go.mongodb.org/atlas-sdk/v20250219001/admin"
1614
)
1715

1816
func main() {
19-
if err := godotenv.Load(); err != nil {
20-
log.Printf("Warning: .env file not loaded: %v", err)
21-
}
22-
23-
secrets, cfg, err := config.LoadAll("configs/config.json")
17+
configPath := "" // Use default config path for environment
18+
explicitEnv := "" // Use default environment
19+
secrets, cfg, err := config.LoadAll(configPath, explicitEnv)
2420
if err != nil {
2521
errors.ExitWithError("Failed to load configuration", err)
2622
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Secrets
22
.env
3+
!.env.example
4+
.env.development
5+
.env.production
6+
configs/config.example.json
7+
!configs/config.development.json
8+
!configs/config.production.json
9+
10+
tmp
11+
312

413
# Logs
514
*.log

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Notable changes to the project.
55
## v1.1 (2025-06-17)
66
### Added
77
- Example scripts for fetching cross-organization billing information.
8-
8+
99
## v1.0 (2025-05-29)
1010
### Added
1111
- Initial project structure with example scripts for fetching logs and metrics.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"ENVIRONMENT": "dev",
3+
"MONGODB_ATLAS_BASE_URL": "https://cloud-dev.mongodb.com",
4+
"ATLAS_ORG_ID": "32b6e34b3d91647abb20e7b8",
5+
"ATLAS_PROJECT_ID": "5e2211c17a3e5a48f5497de3",
6+
"ATLAS_PROJECT_NAME": "Customer Portal - Dev",
7+
"ATLAS_PROCESS_ID": "CustomerPortalDev-shard-00-00.ajlj3.mongodb.net:27017",
8+
"CLOUD_PROVIDER": "AWS",
9+
"AUTO_SCALING_DISK_GB": true,
10+
"AUTO_SCALING_COMPUTE": true,
11+
"DISK_SIZE_GB": 10000
12+
}

0 commit comments

Comments
 (0)