Skip to content

Commit 56f18b1

Browse files
committed
Fix config loader to correctly extract host name
1 parent d6101b9 commit 56f18b1

File tree

6 files changed

+43
-7
lines changed

6 files changed

+43
-7
lines changed

usage-examples/go/atlas-sdk-go/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.

usage-examples/go/atlas-sdk-go/INTERNAL_README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ SDK for Go. Code examples are used in the Atlas Architecture Center docs, and
88
a sanitized copy of the project is available in a user-facing "artifact repo":
99
https://github.com/mongodb/atlas-architecture-go-sdk.
1010

11+
## Terms
12+
13+
Artifact Repo
14+
The user-facing GitHub repo that contains the sanitized version of this project (i.e. stripped of any internal comments, markup, test files, etc.).
15+
Files are copied
16+
17+
18+
19+
1120
## Project Structure
1221
```text
1322
.

usage-examples/go/atlas-sdk-go/examples/monitoring/logs/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ func main() {
4242
HostName: cfg.HostName,
4343
LogName: "mongodb",
4444
}
45+
fmt.Printf("Request parameters: GroupID=%s, HostName=%s, LogName=%s\n",
46+
cfg.ProjectID, cfg.HostName, p.LogName)
4547
rc, err := logs.FetchHostLogs(ctx, client.MonitoringAndLogsApi, p)
4648
if err != nil {
4749
errors.ExitWithError("Failed to fetch logs", err)
4850
}
4951
defer fileutils.SafeClose(rc)
5052

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

usage-examples/go/atlas-sdk-go/internal/billing/collector.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type ProjectInfo struct {
3737

3838
// CollectLineItemBillingData retrieves all pending invoices for the specified organization,
3939
// transforms them into detailed billing records, and filters out items processed before lastProcessedDate.
40-
// Returns a slice of billing Details or an error if no valid invoices or line items are found.
40+
// Returns a slice of billing Details if pending invoices exists or an error if the operation fails.
4141
func CollectLineItemBillingData(ctx context.Context, sdk admin.InvoicesApi, orgSdk admin.OrganizationsApi, orgID string, lastProcessedDate *time.Time) ([]Detail, error) {
4242
req := sdk.ListPendingInvoices(ctx, orgID)
4343
r, _, err := req.Execute()
@@ -46,11 +46,9 @@ func CollectLineItemBillingData(ctx context.Context, sdk admin.InvoicesApi, orgS
4646
return nil, errors.FormatError("list pending invoices", orgID, err)
4747
}
4848
if r == nil || !r.HasResults() || len(r.GetResults()) == 0 {
49-
return nil, &errors.NotFoundError{Resource: "pending invoices", ID: orgID}
49+
return nil, nil
5050
}
5151

52-
fmt.Printf("Found %d pending invoice(s)\n", len(r.GetResults()))
53-
5452
// Get organization name
5553
orgName, err := getOrganizationName(ctx, orgSdk, orgID)
5654
if err != nil {
@@ -112,7 +110,7 @@ func processInvoices(invoices []admin.BillingInvoice, orgID, orgName string, las
112110
return billingDetails, nil
113111
}
114112

115-
// getOrganizationName fetches organization name from API or returns orgID if not found
113+
// getOrganizationName fetches an organization's name for the given organization ID.
116114
func getOrganizationName(ctx context.Context, sdk admin.OrganizationsApi, orgID string) (string, error) {
117115
req := sdk.GetOrganization(ctx, orgID)
118116
org, _, err := req.Execute()

usage-examples/go/atlas-sdk-go/internal/config/loadconfig.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"encoding/json"
55
"os"
6+
"strings"
67

78
"atlas-sdk-go/internal/errors"
89
)
@@ -37,11 +38,26 @@ func LoadConfig(path string) (*Config, error) {
3738
return nil, errors.WithContext(err, "parsing configuration file")
3839
}
3940

41+
if config.OrgID == "" {
42+
return nil, &errors.ValidationError{
43+
Message: "organization ID is required in configuration",
44+
}
45+
}
4046
if config.ProjectID == "" {
4147
return nil, &errors.ValidationError{
4248
Message: "project ID is required in configuration",
4349
}
4450
}
4551

52+
if config.HostName == "" {
53+
if host, _, ok := strings.Cut(config.ProcessID, ":"); ok {
54+
config.HostName = host
55+
} else {
56+
return nil, &errors.ValidationError{
57+
Message: "process ID must be in the format 'hostname:port'",
58+
}
59+
}
60+
}
61+
4662
return &config, nil
4763
}

usage-examples/go/atlas-sdk-go/internal/fileutils/io.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,18 @@ func SafeCopy(dst io.Writer, src io.Reader) error {
5252
// :remove-start:
5353

5454
// SafeDelete removes files generated in the specified directory
55-
// NOTE: INTERNAL ONLY FUNCTION; remove this and "path/filepath" import from Bluehawked files
55+
// NOTE: INTERNAL ONLY FUNCTION; before running `bluehawk.sh`, ensure this this and "path/filepath" import are marked for removal
5656
func SafeDelete(dir string) error {
57+
// Check for global downloads directory
58+
defaultDir := os.Getenv("ATLAS_DOWNLOADS_DIR")
59+
if defaultDir != "" {
60+
dir = filepath.Join(defaultDir, dir)
61+
}
62+
// Check if directory exists before attempting to walk it
63+
if _, err := os.Stat(dir); os.IsNotExist(err) {
64+
return errors.WithContext(err, "directory does not exist")
65+
}
66+
5767
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
5868
if err != nil {
5969
return errors.WithContext(err, "walking directory")

0 commit comments

Comments
 (0)