-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbasic.go
49 lines (40 loc) · 1.27 KB
/
basic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"log"
"os"
"context"
"go.mongodb.org/atlas-sdk/v20250219001/admin"
"go.mongodb.org/atlas-sdk/v20250219001/examples"
)
/*
* MongoDB Atlas Go SDK Basic Example
*/
func main() {
ctx := context.Background()
// Values provided as part of env variables
// See: https://www.mongodb.com/docs/atlas/app-services/authentication/api-key/
apiKey := os.Getenv("MONGODB_ATLAS_PUBLIC_KEY")
apiSecret := os.Getenv("MONGODB_ATLAS_PRIVATE_KEY")
url := os.Getenv("MONGODB_ATLAS_BASE_URL")
sdk, err := admin.NewClient(
admin.UseDigestAuth(apiKey, apiSecret),
admin.UseBaseURL(url))
examples.HandleErr(err, nil)
// -- 1. Get first project
request := sdk.ProjectsApi.ListProjectsWithParams(ctx,
// 2. We are passing a struct with all parameters to the request
&admin.ListProjectsApiParams{
ItemsPerPage: admin.PtrInt(1),
IncludeCount: admin.PtrBool(true),
PageNum: admin.PtrInt(1),
})
// 3. We can also use builder pattern to construct request
projects, response, err := request.IncludeCount(true).PageNum(1).Execute()
examples.HandleErr(err, response)
if projects.GetTotalCount() == 0 {
log.Fatal("account should have at least single project")
}
projectId := projects.GetResults()[0].GetId()
fmt.Printf("Project we use %v", projectId)
}