Skip to content

Commit f484d2c

Browse files
committed
Fix up comments
1 parent ff1424d commit f484d2c

11 files changed

Lines changed: 78 additions & 139 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Notable changes to the project.
44

5+
## v1.2 (2025-08-17)
6+
### Added
7+
- Example scripts for scaling and archiving clusters.
8+
59
## v1.1 (2025-06-17)
610
### Added
711
- Example scripts for fetching cross-organization billing information.

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Currently, the repository includes examples that demonstrate the following:
1616
- Download logs for a specific host
1717
- Pull and parse line-item-level billing data
1818
- Return all linked organizations from a specific billing organization
19-
- Get historical invoices for an organization
20-
- Programmatically manage Atlas resources
19+
- Get historical invoices for an organization
20+
- Programmatically archive Atlas cluster data
2121

2222
As the Architecture Center documentation evolves, this repository will be updated with new examples
2323
and improvements to existing code.
@@ -28,12 +28,15 @@ and improvements to existing code.
2828
.
2929
├── examples # Runnable examples by category
3030
│ ├── billing/
31-
│ └── monitoring/
31+
│ ├── monitoring/
32+
│ └── performance/
3233
├── configs # Atlas configuration template
3334
│ └── config.json
3435
├── internal # Shared utilities and helpers
36+
│ ├── archive/
3537
│ ├── auth/
3638
│ ├── billing/
39+
│ ├── clusters/
3740
│ ├── config/
3841
│ ├── data/
3942
│ ├── errors/
@@ -56,10 +59,13 @@ and improvements to existing code.
5659

5760
## Setting Environment Variables
5861

59-
1. Create a `.env` file in the root directory with your MongoDB Atlas service account credentials:
62+
1. Create a `.env.<environment>` file in the root directory with your MongoDB Atlas service account credentials. For example, to create a `.env.development` file for your dev environment:
6063
```dotenv
6164
MONGODB_ATLAS_SERVICE_ACCOUNT_ID=your_service_account_id
6265
MONGODB_ATLAS_SERVICE_ACCOUNT_SECRET=your_service_account_secret
66+
ATLAS_DOWNLOADS_DIR="tmp/atlas_downloads"
67+
CONFIG_PATH="/configs"
68+
APP_ENV="dev"
6369
```
6470
> **NOTE:** For production, use a secrets manager (e.g. HashiCorp Vault, AWS Secrets Manager)
6571
> instead of environment variables.
@@ -121,6 +127,13 @@ go run examples/monitoring/metrics_disk/main.go
121127
go run examples/monitoring/metrics_process/main.go
122128
```
123129

130+
### Performance
131+
132+
#### Archive Cluster Data
133+
```bash
134+
go run examples/performance/archiving/main.go
135+
```
136+
124137
## Changelog
125138

126139
For list of major changes to this project, see [CHANGELOG](CHANGELOG.md).

generated-usage-examples/go/atlas-sdk-go/project-copy/internal/config/appcontext.go

Lines changed: 12 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"atlas-sdk-go/internal/errors"
1414
)
1515

16+
// Package config provides application context management, including environment-specific configurations
17+
// and caching mechanisms to optimize performance and reduce redundant loading of configurations.
1618
var (
1719
cachedAppContext *AppContext
1820
cachedAppContextTime time.Time
@@ -41,12 +43,9 @@ type AppContext struct {
4143
// If strictValidation is true, invalid environments will return an error
4244
func LoadAppContext(explicitEnv string, strictValidation bool) (*AppContext, error) {
4345
// Environment resolution priority:
44-
// 1. Explicitly passed environment parameter
45-
// 2. APP_ENV environment variable
46-
// 3. Default to "development"
47-
//
48-
// Special environments:
49-
// - "test": Used for automated testing, loads from .env.test and configs/config.test.json
46+
// 1. An explicitly passed environment parameter
47+
// 2. An APP_ENV environment variable
48+
// 3. Otherwise, defaults to "development"
5049

5150
// Determine environment
5251
env := explicitEnv
@@ -68,7 +67,6 @@ func LoadAppContext(explicitEnv string, strictValidation bool) (*AppContext, err
6867
}
6968
cacheMutex.RUnlock()
7069

71-
// Validate environment
7270
if !ValidateEnvironment(env) {
7371
if strictValidation {
7472
return nil, fmt.Errorf("invalid environment: %s", env)
@@ -103,7 +101,6 @@ func LoadAppContext(explicitEnv string, strictValidation bool) (*AppContext, err
103101
log.Printf("Loading configuration for environment: %s", env)
104102
log.Printf("Using config file: %s", configPath)
105103

106-
// Load secrets and config
107104
secrets, err := LoadSecrets()
108105
if err != nil {
109106
return nil, errors.WithContext(err, "loading secrets")
@@ -114,8 +111,7 @@ func LoadAppContext(explicitEnv string, strictValidation bool) (*AppContext, err
114111
return nil, errors.WithContext(err, "loading config")
115112
}
116113

117-
// Validate config with environment context
118-
if err := config.Validate(env); err != nil {
114+
if err = config.Validate(env); err != nil {
119115
return nil, errors.WithContext(err, "validating config")
120116
}
121117

@@ -138,7 +134,9 @@ func LoadAppContext(explicitEnv string, strictValidation bool) (*AppContext, err
138134
return appCtx, nil
139135
}
140136

141-
// LoadAppContextWithContext Add context support to handle timeouts and cancellation
137+
// LoadAppContextWithContext initializes application context with environment-specific configuration using a provided context for cancellation support.
138+
// If explicitEnv is provided, it overrides the APP_ENV environment variable
139+
// If strictValidation is true, invalid environments will return an error
142140
func LoadAppContextWithContext(ctx context.Context, explicitEnv string, strictValidation bool) (*AppContext, error) {
143141
// Use context for potential operations that may need cancellation
144142
select {
@@ -168,27 +166,22 @@ func LoadAppContextWithContext(ctx context.Context, explicitEnv string, strictVa
168166
}
169167
cacheMutex.RUnlock()
170168

171-
// Rest of implementation mirrors LoadAppContext but with context checks
169+
// The implementation mirrors LoadAppContext but with context checks
172170
if !ValidateEnvironment(env) {
173171
if strictValidation {
174172
return nil, fmt.Errorf("invalid environment: %s", env)
175173
}
176174
log.Printf("Warning: Unexpected environment '%s' may cause issues", env)
177175
}
178176

179-
// Special handling for test environment
180-
if env == "test" {
181-
log.Printf("Using test environment - ensure test fixtures are available")
182-
}
183-
184177
// Add context check before expensive operations
185178
select {
186179
case <-ctx.Done():
187180
return nil, fmt.Errorf("context cancelled while loading environment files: %w", ctx.Err())
188181
default:
189182
}
190183

191-
// Load environment files with improved approach
184+
// Load environment files
192185
envFiles := []string{
193186
fmt.Sprintf(".env.%s", env),
194187
".env",
@@ -223,7 +216,6 @@ func LoadAppContextWithContext(ctx context.Context, explicitEnv string, strictVa
223216
log.Printf("Loading configuration for environment: %s", env)
224217
log.Printf("Using config file: %s", configPath)
225218

226-
// Load secrets and config
227219
secrets, err := LoadSecrets()
228220
if err != nil {
229221
return nil, errors.WithContext(err, "loading secrets")
@@ -234,8 +226,7 @@ func LoadAppContextWithContext(ctx context.Context, explicitEnv string, strictVa
234226
return nil, errors.WithContext(err, "loading config")
235227
}
236228

237-
// Validate config with environment context
238-
if err := config.Validate(env); err != nil {
229+
if err = config.Validate(env); err != nil {
239230
return nil, errors.WithContext(err, "validating config")
240231
}
241232

@@ -258,73 +249,3 @@ func LoadAppContextWithContext(ctx context.Context, explicitEnv string, strictVa
258249
return appCtx, nil
259250
}
260251

261-
// getTestConfiguration provides specialized test configuration
262-
func getTestConfiguration() (*Config, *Secrets, error) {
263-
// Check if specific test config file exists
264-
testConfigFile := "configs/config.test.json"
265-
if _, err := os.Stat(testConfigFile); err == nil {
266-
config, err := LoadConfig(testConfigFile)
267-
if err != nil {
268-
return nil, nil, err
269-
}
270-
271-
// Still use mock secrets to avoid requiring real credentials
272-
mockSecrets := &Secrets{
273-
ServiceAccountID: "test-service-account-id",
274-
ServiceAccountSecret: "test-service-account-secret",
275-
}
276-
277-
return config, mockSecrets, nil
278-
}
279-
280-
// Fall back to fully mocked configuration
281-
return &Config{
282-
BaseURL: "https://cloud-mock.mongodb.com",
283-
OrgID: "test-org-id",
284-
ProjectID: "test-project-id",
285-
ClusterName: "TestCluster",
286-
ProcessID: "test-cluster-shard-00-00.test.mongodb.net:27017",
287-
HostName: "test-cluster-shard-00-00.test.mongodb.net",
288-
}, &Secrets{
289-
ServiceAccountID: "test-service-account-id",
290-
ServiceAccountSecret: "test-service-account-secret",
291-
}, nil
292-
}
293-
294-
// Add diff support for testing
295-
func (a *AppContext) Diff(other *AppContext) []string {
296-
var differences []string
297-
298-
if a.Environment != other.Environment {
299-
differences = append(differences, fmt.Sprintf("Environment: %s vs %s",
300-
a.Environment, other.Environment))
301-
}
302-
303-
// Compare important config fields
304-
if a.Config.BaseURL != other.Config.BaseURL {
305-
differences = append(differences, fmt.Sprintf("BaseURL: %s vs %s",
306-
a.Config.BaseURL, other.Config.BaseURL))
307-
}
308-
309-
if a.Config.OrgID != other.Config.OrgID {
310-
differences = append(differences, fmt.Sprintf("OrgID: %s vs %s",
311-
a.Config.OrgID, other.Config.OrgID))
312-
}
313-
314-
if a.Config.ProjectID != other.Config.ProjectID {
315-
differences = append(differences, fmt.Sprintf("ProjectID: %s vs %s",
316-
a.Config.ProjectID, other.Config.ProjectID))
317-
}
318-
319-
if a.Config.ClusterName != other.Config.ClusterName {
320-
differences = append(differences, fmt.Sprintf("ClusterName: %s vs %s",
321-
a.Config.ClusterName, other.Config.ClusterName))
322-
}
323-
324-
if a.Config.ProcessID != other.Config.ProcessID {
325-
differences = append(differences, fmt.Sprintf("ProcessID: %s vs %s",
326-
a.Config.ProcessID, other.Config.ProcessID))
327-
}
328-
329-
return differences
330-
}

generated-usage-examples/go/atlas-sdk-go/project-copy/internal/config/loadconfig.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"atlas-sdk-go/internal/errors"
99
)
1010

11+
// Config holds the configuration for connecting to MongoDB Atlas
1112
type Config struct {
1213
BaseURL string `json:"MONGODB_ATLAS_BASE_URL"`
1314
OrgID string `json:"ATLAS_ORG_ID"`
@@ -18,6 +19,7 @@ type Config struct {
1819
}
1920

2021
// LoadConfig reads a JSON configuration file and returns a Config struct
22+
// It validates required fields and returns an error if any validation fails.
2123
func LoadConfig(path string) (*Config, error) {
2224
if path == "" {
2325
return nil, &errors.ValidationError{
@@ -34,7 +36,7 @@ func LoadConfig(path string) (*Config, error) {
3436
}
3537

3638
var config Config
37-
if err := json.Unmarshal(data, &config); err != nil {
39+
if err = json.Unmarshal(data, &config); err != nil {
3840
return nil, errors.WithContext(err, "parsing configuration file")
3941
}
4042

@@ -62,6 +64,8 @@ func LoadConfig(path string) (*Config, error) {
6264
return &config, nil
6365
}
6466

67+
// Validate checks the configuration for required fields and environment-specific rules
68+
// It returns an error if any validation fails.
6569
func (c *Config) Validate(env string) error {
6670
if c.BaseURL == "" {
6771
return &errors.ValidationError{Message: "BaseURL is required"}

generated-usage-examples/go/atlas-sdk-go/project-copy/internal/config/loadenv.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import (
77
"atlas-sdk-go/internal/errors"
88
)
99

10+
// Environment variable names for service account credentials
1011
type Secrets struct {
1112
ServiceAccountID string
1213
ServiceAccountSecret string
1314
}
1415

16+
// LoadSecrets loads the required secrets from environment variables.
17+
// It returns a Secrets struct or an error if any required variable is missing.
1518
func LoadSecrets() (*Secrets, error) {
1619
s := &Secrets{}
1720
var missing []string

generated-usage-examples/go/atlas-sdk-go/project-copy/internal/config/utils.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)