Skip to content

Commit de004d8

Browse files
committed
Refactor and write unit tests
1 parent 16b940e commit de004d8

File tree

34 files changed

+945
-667
lines changed

34 files changed

+945
-667
lines changed

.github/workflows/test-go-sdk.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# name: CI
2+
# on: [push, pull_request]
3+
# jobs:
4+
# test:
5+
# runs-on: ubuntu-latest
6+
# steps:
7+
# - name: Checkout code
8+
# uses: actions/checkout@v4
9+
# - name: Set up Go
10+
# uses: actions/setup-go@v5
11+
# with:
12+
# go-version-file: go.mod
13+
# - name: Test
14+
# run: go test

usage-examples/go/atlas-sdk-go/bluehawk/copy.sh renamed to usage-examples/go/atlas-sdk-go/.bluehawk/copy.sh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ PROJECT=$(git rev-parse --show-toplevel)
99
INPUT_DIR="$PROJECT/usage-examples/go/atlas-sdk-go/"
1010
OUTPUT_DIR="$PROJECT/generated-usage-examples/go/atlas-sdk-go/project-copy/"
1111

12-
# Set ignored internal files and directories
12+
# Set directories and files to ignore
1313
IGNORE=(
1414
"README.md"
1515
"bluehawk/"
1616
"tests/"
17-
".env"
17+
".*"
1818
"*.gz"
1919
"*.log"
2020
)
@@ -23,8 +23,18 @@ for path in "${IGNORE[@]}"; do
2323
IGNORE_ARGS+=("--ignore=$path")
2424
done
2525

26+
# Set directories and files to rename
27+
RENAME=(
28+
"REPO_README.md:README.md"
29+
)
30+
RENAME_ARGS=()
31+
for path in "${RENAME[@]}"; do
32+
IFS=":" read -r src dst <<< "$path" # Split the path into source and destination
33+
RENAME_ARGS+=("--rename=$src:$dst") # Add the rename argument to the array
34+
done
35+
2636
# Run Bluehawk copy command, passing all ignore args and "copy" state
27-
bluehawk copy \
37+
.bluehawk copy \
2838
"${IGNORE_ARGS[@]}" \
2939
--state copy \
3040
-o "$OUTPUT_DIR" \

usage-examples/go/atlas-sdk-go/bluehawk/snip.sh renamed to usage-examples/go/atlas-sdk-go/.bluehawk/snip.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ PROJECT=$(git rev-parse --show-toplevel)
55
GO_SDK_EXAMPLES="$PROJECT/usage-examples/go/atlas-sdk-go/"
66
GENERATED_EXAMPLES="$PROJECT/generated-usage-examples/go/atlas-sdk-go/usage-examples/"
77

8-
# ——— helper: run bluehawk and only show the key lines ———
8+
# ——— helper: run .bluehawk and only show the key lines ———
99
run_snip() {
1010
local extra_flags="$1"
1111
local label="$2"
12-
local dst="$GENERATED_EXAMPLES"
12+
local dest="$GENERATED_EXAMPLES"
1313

1414
echo "$label"
1515
npx bluehawk snip "$GO_SDK_EXAMPLES" -o "$GENERATED_EXAMPLES"
@@ -18,9 +18,9 @@ run_snip() {
1818
run_snip "" "Global snippets"
1919

2020
# ——— 2) state‑specific snippets ———
21-
read -p "Do you have any state tags to enter? [y/N]: " resp
21+
read -r -p "Do you have any state tags to enter? [y/N]: " resp
2222
if [[ $resp =~ ^[Yy]$ ]]; then
23-
read -a STATES -p "Enter one or more state tags (space‑separated): "
23+
read -r -a STATES -p "Enter one or more state tags (space‑separated): "
2424
for s in "${STATES[@]}"; do
2525
run_snip "--state $s" "State: $s"
2626
done
Lines changed: 33 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
1-
[//]: # (.. Don't Copy to Target Repo )
21
# Atlas SDK for Go
32

4-
This project demonstrates how to script specific functionality using the Atlas SDK for Go. Code examples are included in the Atlas Architecture Center docs and available in a user-facing version of the project.
5-
6-
- Generated usage examples, which are included directly in the docs
7-
- Within a
3+
This project demonstrates how to script specific functionality using the Atlas
4+
SDK for Go. Code examples are used in the Atlas Architecture Center docs, and
5+
the project is made available in a user-facing repo.
86

97
## Project Structure
108
```text
119
atlas-sdk-go/
12-
│── bluehawk/ # Bluehawk scripts to snip and copy code examples
13-
│ ├── copy.sh
14-
│ ├── snip.sh
1510
│── cmd/ # Self-contained, runnable scripts
1611
│ ├── get_logs/
1712
│ ├── main.go
18-
│ ├── get_metrics/
19-
│ ├── dev/
20-
│ ├── main.go
21-
│ ├── prod/
22-
│ ├── main.go
13+
│ ├── get_metrics_disk/
14+
│ ├── main.go
15+
│ ├── get_metrics_process/
16+
│ ├── main.go
2317
│── config/ # Atlas configuration settings
2418
│ ├── config.json
2519
│── internal/ # Shared internal logic
26-
│── auth/
27-
├── auth.go
28-
│ ├── config_loader.go
29-
│ ├── secrets_loader.go
20+
│ ├── auth/
21+
| ├── client.go
22+
│ ├── config/
23+
| ├── json.go
24+
| ├── secrets.go
25+
| ├── loader.go
3026
│── .env # Secrets file (excluded from Git)
3127
│── go.mod
3228
│── go.sum
33-
│── README.md # Internal-only README (do not copy)
29+
│── README.md # Internal-only README (do not copy with Copier Tool)
30+
│── scripts/ # Internal-only Bluehawk scripts to snip and copy code examples
31+
│ ├── bluehawk.sh
3432
```
3533

3634
## Runnable Scripts
37-
You can run individual scripts using `run_cmd.sh` and specifying the script's action (i.e. the parent directory for the `main.go` you want to run).
38-
39-
For example, to run `get_logs/main.go`:
35+
You can run individual scripts from the terminal. For example, to run `get_logs/main.go`:
4036
```shell
41-
./run_cmd.sh get_logs
37+
go run cmd/get_logs/main.go
4238
```
4339

4440
## Set up
@@ -47,9 +43,11 @@ For example, to run `get_logs/main.go`:
4743

4844
- A [service account](https://www.mongodb.com/docs/atlas/api/service-accounts-overview/#std-label-service-accounts-overview) with access to your Atlas project
4945

46+
> **NOTE:** Some scripts require an M10+ cluster
47+
5048
### Set environment variables and config file
5149

52-
1. Set the following variable values, either as a `.env` file in the root directory or through your IDE
50+
1. Set the following variable values, either as a `.env` file in the root directory or through your IDE:
5351
```shell
5452
MONGODB_ATLAS_SERVICE_ACCOUNT_ID=your-service-account-id
5553
MONGODB_ATLAS_SERVICE_ACCOUNT_SECRET=your-service-account-secret
@@ -67,54 +65,26 @@ For example, to run `get_logs/main.go`:
6765
}
6866
```
6967
> **NOTE: Group ID == Project ID** Groups and projects are synonymous terms. Groups and projects are synonymous terms. Your group id is the same as your project id.
70-
3.
68+
69+
## Write Tests
70+
71+
# TODO
7172

7273
## Generate Examples
7374

74-
### Generate code usage examples for docs
75-
This project uses the following Bluehawk commands to generate the code examples:
75+
This project uses Bluehawk to generate code examples from the source code.
7676

77-
- Usage examples for the docs. These are generated using the `bluehawk snip` command based on the `snippet` markup in the code file.
78-
```shell
79-
./bluehawk/snip.sh
80-
```
77+
- Usage examples for the docs. These are generated using the `bluehawk snip`
78+
command based on the `snippet` markup in the code file.
79+
- Full project files for the user-facing project repo. These are generated using
80+
the `bluehawk copy` command.
8181

82-
### Copy project files for user-facing project repo
82+
Run the bluehawk script and enter either `snip` or `copy`. The selected command
83+
runs with the defined defaults.
8384

84-
To copy the full project files for the user-facing artifact repo. These are generated using the `bluehawk copy` command, and any specified files are ignored.
8585
```shell
86-
./bluehawk/copy.sh
86+
./scripts/bluehawk.sh
8787
```
8888

8989
> **NOTE: "Copy" State** This project uses a state named "copy" specifically for any manipulations needed for code copied to the artifact repo.
9090

91-
## Copy Generated Examples to Other Repos
92-
93-
94-
95-
---
96-
scratchpad:
97-
- manually test against real infra
98-
- pull the real data
99-
100-
PR Push > run on captured data
101-
& scheduled job to validate? (or bump our sdk version along with the v release &
102-
verify; fix any failing test)
103-
- run the gh action locally
104-
---
105-
1. go sdk testing
106-
2. atlas testing
107-
108-
we'd need to be notified when either change
109-
we can test sdk automatically with the captured data
110-
111-
periodic manual validation step to test the infra side
112-
- release cadence for sdk > gh
113-
- release cadence for atlas/infra > server version? api update?
114-
---
115-
arch center docs versioning considerations
116-
- how to keep arch center version in sync with code example version updates?
117-
---
118-
snippets in generated-examples > push to arch center repo to use in docs?
119-
---
120-
narrating out loud the next step?
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# MongoDB Atlas Architecture Center Go SDK Code Examples
2+
3+
This repository contains [Atlas Go SDK](https://www.mongodb.com/docs/atlas/sdk/)
4+
code examples that follow recommendations in MongoDB's official
5+
[Atlas Architecture Center documentation](https://www.mongodb.com/docs/atlas/architecture/current/).
6+
You can run, download, and modify these code examples as starting points for
7+
configuring your MongoDB Atlas architecture for your use case.
8+
9+
## Overview
10+
11+
### Project Structure
12+
13+
```text
14+
Project Root
15+
├── cmd
16+
│ ├── get_logs/main.go
17+
│ ├── get_metrics_disk/main.go
18+
│ ├── get_metrics_process/main.go
19+
├── internal
20+
│ ├── auth
21+
│ │ ├── auth.go
22+
│ ├── logs
23+
│ │ ├── downloader.go
24+
│ ├── metrics
25+
│ │ ├── metrics.go
26+
├── go.mod
27+
├── go.sum
28+
├── configs
29+
│ ├── config.json
30+
├── .env #
31+
```
32+
33+
> NOTE: In a production environment, you are likely to use
34+
35+
note in the README that you'll most likely be using a secrets manager in prod
36+
## License
37+
38+
This project is licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0).
39+
40+
## Issues
41+
42+
To report an issue with any of these code examples, please leave feedback
43+
through the corresponding documentation page in the
44+
[MongoDB Atlas Architecture Center](https://www.mongodb.com/docs/atlas/architecture/current/).
45+
Using the `Rate This Page` button, you can add a comment about the issue after
46+
leaving a star rating.
47+
48+
## Contributing
49+
50+
We are not currently accepting public contributions to this repository at this
51+
time.****

0 commit comments

Comments
 (0)