Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

1 change: 0 additions & 1 deletion generated-usage-examples/copier-test/test.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"MONGODB_ATLAS_BASE_URL": "https://cloud-dev.mongodb.com",
"ATLAS_ORG_ID": "32b6e34b3d91647abb20e7b8",
"ATLAS_PROJECT_ID": "5e2211c17a3e5a48f5497de3",
"ATLAS_PROJECT_NAME": "Customer Portal - Dev",
"ATLAS_PROCESS_ID": "CustomerPortalDev-shard-00-00.ajlj3.mongodb.net:27017",
"programmatic_scaling": {
"target_tier": "M20",
"pre_scale_event": true,
"cpu_threshold": 75.0,
"cpu_period_minutes": 60,
"dry_run": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"MONGODB_ATLAS_BASE_URL": "https://cloud.mongodb.com",
"ATLAS_ORG_ID": "32b6e34b3d91647abb20e7b8",
"ATLAS_PROJECT_ID": "5e2211c17a3e5a48f5497de3",
"ATLAS_PROJECT_NAME": "Customer Portal - Prod",
"ATLAS_PROCESS_ID": "CustomerPortalProd-shard-00-00.ajlj3.mongodb.net:27017",
"programmatic_scaling": {
"target_tier": "M50",
"pre_scale_event": true,
"cpu_threshold": 75.0,
"cpu_period_minutes": 60,
"dry_run": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func main() {

fmt.Printf("\nFound %d clusters to analyze\n", len(clusters.GetResults()))

// Connect to each cluster and analyze collections for archiving
failedArchives := 0
skippedCandidates := 0
totalCandidates := 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"fmt"
"log"

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

"atlas-sdk-go/internal/auth"
"atlas-sdk-go/internal/billing"
"atlas-sdk-go/internal/config"

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

func main() {
Expand Down Expand Up @@ -55,4 +55,3 @@ func main() {
fmt.Printf(" %d. %v\n", i+1, org)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (

"atlas-sdk-go/internal/auth"
"atlas-sdk-go/internal/config"
"atlas-sdk-go/internal/metrics"

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

"atlas-sdk-go/internal/metrics"
)

func main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"fmt"
"log"

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

"atlas-sdk-go/internal/auth"
"atlas-sdk-go/internal/billing"
"atlas-sdk-go/internal/config"
"atlas-sdk-go/internal/data/export"
"atlas-sdk-go/internal/fileutils"

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

func main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// See entire project at https://github.com/mongodb/atlas-architecture-go-sdk
package main

import (
"context"
"fmt"
"log"
"strings"
"time"

"atlas-sdk-go/internal/auth"
"atlas-sdk-go/internal/clusterutils"
"atlas-sdk-go/internal/config"
"atlas-sdk-go/internal/scale"

"github.com/joho/godotenv"
)

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(), 30*time.Minute)
defer cancel()
client, err := auth.NewClient(ctx, cfg, secrets)
if err != nil {
log.Fatalf("Failed to initialize authentication client: %v", err)
}

projectID := cfg.ProjectID
if projectID == "" {
log.Fatal("Failed to find Project ID in configuration")
}

procDetails, err := clusterutils.ListClusterProcessDetails(ctx, client, projectID)
if err != nil {
log.Printf("Warning: unable to map detailed processes to clusters: %v", err)
}

// Based on the configuration settings, perform the following programmatic scaling:
// - Pre-scale ahead of a known traffic spike (e.g. planned bulk inserts)
// - Reactive scale when sustained compute utilization exceeds a threshold
//
// NOTE: Prefer Atlas built-in auto-scaling for gradual growth. Use programmatic scaling for exceptional events or custom logic.
scaling := scale.LoadScalingConfig(cfg)
fmt.Printf("Starting scaling analysis for project: %s\n", projectID)
fmt.Printf("Configuration - Target tier: %s, Pre-scale: %v, CPU threshold: %.1f%%, Period: %d min, Dry run: %v\n",
scaling.TargetTier, scaling.PreScale, scaling.CPUThreshold, scaling.PeriodMinutes, scaling.DryRun)

clusterList, _, err := client.ClustersApi.ListClusters(ctx, projectID).Execute()
if err != nil {
log.Fatalf("Failed to list clusters: %v", err)
}

clusters := clusterList.GetResults()
fmt.Printf("\nFound %d clusters to analyze for scaling\n", len(clusters))

// Track scaling operations across all clusters
scalingCandidates := 0
successfulScales := 0
failedScales := 0
skippedClusters := 0

for _, cluster := range clusters {
clusterName := cluster.GetName()
fmt.Printf("\n=== Analyzing cluster: %s ===\n", clusterName)

// Skip clusters that are not in IDLE state
if cluster.HasStateName() && cluster.GetStateName() != "IDLE" {
fmt.Printf("- Skipping cluster %s: not in IDLE state (current: %s)\n", clusterName, cluster.GetStateName())
skippedClusters++
continue
}

currentTier, err := scale.ExtractInstanceSize(&cluster)
if err != nil {
fmt.Printf("- Skipping cluster %s: failed to extract current tier: %v\n", clusterName, err)
skippedClusters++
continue
}
fmt.Printf("- Current tier: %s, Target tier: %s\n", currentTier, scaling.TargetTier)

// Skip if already at target tier
if strings.EqualFold(currentTier, scaling.TargetTier) {
fmt.Printf("- No action needed: cluster already at target tier %s\n", scaling.TargetTier)
continue
}

// Shared tier handling: skip reactive CPU (metrics unavailable) unless pre-scale
if scale.IsSharedTier(currentTier) && !scaling.PreScale {
fmt.Printf("- Shared tier (%s): reactive CPU metrics unavailable; skipping (enable PreScale to force scale)\n", currentTier)
continue
}

// Gather process info for dedicated tiers
var processID string
var primaryID string
var processIDs []string
if procs, ok := procDetails[clusterName]; ok && len(procs) > 0 {
for _, p := range procs {
processIDs = append(processIDs, p.ID)
}
if pid, okp := clusterutils.GetPrimaryProcessID(procs); okp {
primaryID = pid
}
processID = processIDs[0]
}
if len(processIDs) > 0 && !scale.IsSharedTier(currentTier) {
fmt.Printf("- Found %d processes (primary=%s)\n", len(processIDs), primaryID)
} else if processID != "" {
fmt.Printf("- Using process ID: %s for metrics\n", processID)
}

// Evaluate scaling decision based on configuration and metrics
var shouldScale bool
var reason string
if !scale.IsSharedTier(currentTier) && len(processIDs) > 0 { // dedicated tier with multiple processes
shouldScale, reason = scale.EvaluateDecisionAggregated(ctx, client, projectID, clusterName, processIDs, primaryID, scaling)
} else if !scale.IsSharedTier(currentTier) && processID != "" { // fallback if no aggregation possible
shouldScale, reason = scale.EvaluateDecisionForProcess(ctx, client, projectID, clusterName, processID, scaling)
} else if !scale.IsSharedTier(currentTier) { // dedicated tier but no process info
shouldScale, reason = scale.EvaluateDecision(ctx, client, projectID, clusterName, scaling)
} else { // shared tier (M0/M2/M5)
shouldScale = scaling.PreScale
if shouldScale {
reason = "pre-scale event flag set (shared tier)"
} else {
reason = "shared tier without pre-scale"
}
}
if !shouldScale {
fmt.Printf("- Conditions not met: %s\n", reason)
continue
}

scalingCandidates++
fmt.Printf("- Scaling decision: proceed -> %s\n", reason)

if scaling.DryRun {
fmt.Printf("- DRY_RUN=true: would scale cluster %s from %s to %s\n",
clusterName, currentTier, scaling.TargetTier)
successfulScales++
continue
}

if err := scale.ExecuteClusterScaling(ctx, client, projectID, clusterName, &cluster, scaling.TargetTier); err != nil {
fmt.Printf("- ERROR: Failed to scale cluster %s: %v\n", clusterName, err)
failedScales++
continue
}
fmt.Printf("- Successfully initiated scaling for cluster %s from %s to %s\n",
clusterName, currentTier, scaling.TargetTier)
successfulScales++
}

fmt.Printf("\n=== Scaling Operation Summary ===\n")
fmt.Printf("Total clusters analyzed: %d\n", len(clusters))
fmt.Printf("Scaling candidates identified: %d\n", scalingCandidates)
fmt.Printf("Successful scaling operations: %d\n", successfulScales)
fmt.Printf("Failed scaling operations: %d\n", failedScales)
fmt.Printf("Skipped clusters: %d\n", skippedClusters)

if failedScales > 0 {
fmt.Printf("WARNING: %d of %d scaling operations failed\n", failedScales, scalingCandidates)
}

if successfulScales > 0 && !scaling.DryRun {
fmt.Println("\nAtlas will perform rolling resizes with zero-downtime semantics.")
fmt.Println("Monitor status in the Atlas UI or poll cluster states until STATE_NAME becomes IDLE.")
}
fmt.Println("Scaling analysis and operations completed.")
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Secrets (keep example)
.env
!.env.example
.env.production
.env.*

# Configs (keep example)
configs/config.json
configs/config.*.json
!configs/config.example.json

# temporary files
Expand Down
25 changes: 0 additions & 25 deletions generated-usage-examples/go/atlas-sdk-go/project-copy/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,3 @@ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]

Licensed 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.
Loading
Loading