Skip to content

Commit 6be59f0

Browse files
authored
Merge pull request #32 from mongodb/global-clusters
Global Clusters Service
2 parents 73b33e5 + 9b32c84 commit 6be59f0

File tree

4 files changed

+556
-2
lines changed

4 files changed

+556
-2
lines changed

mongodbatlas/clusters_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ func TestClusters_UpdateProcessArgs(t *testing.T) {
630630

631631
}
632632

633-
func TestDatabaseUsers_GetProcessArgs(t *testing.T) {
633+
func TestClusters_GetProcessArgs(t *testing.T) {
634634
setup()
635635
defer teardown()
636636

@@ -676,7 +676,7 @@ func TestClusters_checkClusterNameParam(t *testing.T) {
676676
}
677677
}
678678

679-
func TestDatabaseUsers_Get(t *testing.T) {
679+
func TestClusters_Get(t *testing.T) {
680680
setup()
681681
defer teardown()
682682

mongodbatlas/global_clusters.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package mongodbatlas
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
const globalClustersBasePath = "groups/%s/clusters/%s/globalWrites/%s"
10+
11+
//GlobalClustersService is an interface for interfacing with the Global Clusters
12+
// endpoints of the MongoDB Atlas API.
13+
//See more: https://docs.atlas.mongodb.com/reference/api/global-clusters/
14+
type GlobalClustersService interface {
15+
Get(context.Context, string, string) (*GlobalCluster, *Response, error)
16+
AddManagedNamespace(context.Context, string, string, *ManagedNamespace) (*GlobalCluster, *Response, error)
17+
DeleteManagedNamespace(context.Context, string, string, *ManagedNamespace) (*GlobalCluster, *Response, error)
18+
AddCustomZoneMappings(context.Context, string, string, *CustomZoneMappingsRequest) (*GlobalCluster, *Response, error)
19+
DeleteCustomZoneMappings(context.Context, string, string) (*GlobalCluster, *Response, error)
20+
}
21+
22+
//GlobalClustersServiceOp handles communication with the GlobalClusters related methos of the
23+
//MongoDB Atlas API
24+
type GlobalClustersServiceOp struct {
25+
client *Client
26+
}
27+
28+
var _ GlobalClustersService = &GlobalClustersServiceOp{}
29+
30+
// GlobalCluster represents MongoDB Global Cluster Configuration in your Global Cluster.
31+
type GlobalCluster struct {
32+
CustomZoneMapping map[string]string `json:"customZoneMapping"`
33+
ManagedNamespaces []ManagedNamespace `json:"managedNamespaces"`
34+
}
35+
36+
//ManagedNamespace represents the information about managed namespace configuration.
37+
type ManagedNamespace struct {
38+
Db string `json:"db"`
39+
Collection string `json:"collection"`
40+
CustomShardKey string `json:"customShardKey,omitempty"`
41+
}
42+
43+
//CustomZoneMappingsRequest represents the request related to add custom zone mappings to a global cluster.
44+
type CustomZoneMappingsRequest struct {
45+
CustomZoneMappings []CustomZoneMapping `json:"customZoneMappings"`
46+
}
47+
48+
//CustomZoneMapping represents the custom zone mapping.
49+
type CustomZoneMapping struct {
50+
Location string `json:"location"`
51+
Zone string `json:"zone"`
52+
}
53+
54+
//Get retrieves all managed namespaces and custom zone mappings associated with the specified Global Cluster.
55+
//See more: https://docs.atlas.mongodb.com/reference/api/global-clusters-retrieve-namespaces/
56+
func (s *GlobalClustersServiceOp) Get(ctx context.Context, groupID string, clusterName string) (*GlobalCluster, *Response, error) {
57+
if clusterName == "" {
58+
return nil, nil, NewArgError("username", "must be set")
59+
}
60+
61+
path := fmt.Sprintf("groups/%s/clusters/%s/globalWrites", groupID, clusterName)
62+
63+
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
64+
if err != nil {
65+
return nil, nil, err
66+
}
67+
68+
root := new(GlobalCluster)
69+
resp, err := s.client.Do(ctx, req, root)
70+
if err != nil {
71+
return nil, resp, err
72+
}
73+
74+
return root, resp, err
75+
}
76+
77+
//AddManagedNamespace adds a managed namespace to the specified Global Cluster.
78+
//See more: https://docs.atlas.mongodb.com/reference/api/database-users-create-a-user/
79+
func (s *GlobalClustersServiceOp) AddManagedNamespace(ctx context.Context, groupID string, clusterName string, createRequest *ManagedNamespace) (*GlobalCluster, *Response, error) {
80+
if createRequest == nil {
81+
return nil, nil, NewArgError("createRequest", "cannot be nil")
82+
}
83+
84+
path := fmt.Sprintf(globalClustersBasePath, groupID, clusterName, "managedNamespaces")
85+
86+
req, err := s.client.NewRequest(ctx, http.MethodPost, path, createRequest)
87+
if err != nil {
88+
return nil, nil, err
89+
}
90+
91+
root := new(GlobalCluster)
92+
resp, err := s.client.Do(ctx, req, root)
93+
if err != nil {
94+
return nil, resp, err
95+
}
96+
97+
return root, resp, err
98+
}
99+
100+
//DeleteManagedNamespace deletes the managed namespace configuration of the global cluster given.
101+
//See more: https://docs.atlas.mongodb.com/reference/api/global-clusters-delete-namespace/
102+
func (s *GlobalClustersServiceOp) DeleteManagedNamespace(ctx context.Context, groupID string, clusterName string, deleteRequest *ManagedNamespace) (*GlobalCluster, *Response, error) {
103+
if deleteRequest == nil {
104+
return nil, nil, NewArgError("createRequest", "cannot be nil")
105+
}
106+
107+
path := fmt.Sprintf(globalClustersBasePath, groupID, clusterName, "managedNamespaces")
108+
109+
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
110+
if err != nil {
111+
return nil, nil, err
112+
}
113+
114+
q := req.URL.Query()
115+
q.Add("collection", deleteRequest.Collection)
116+
q.Add("db", deleteRequest.Db)
117+
req.URL.RawQuery = q.Encode()
118+
119+
root := new(GlobalCluster)
120+
resp, err := s.client.Do(ctx, req, root)
121+
if err != nil {
122+
return nil, resp, err
123+
}
124+
125+
return root, resp, err
126+
}
127+
128+
//AddCustomZoneMappings adds an entry to the list of custom zone mappings for the specified Global Cluster.
129+
//See more: https://docs.atlas.mongodb.com/reference/api/global-clusters-add-customzonemapping/
130+
func (s *GlobalClustersServiceOp) AddCustomZoneMappings(ctx context.Context, groupID string, clusterName string, createRequest *CustomZoneMappingsRequest) (*GlobalCluster, *Response, error) {
131+
if createRequest == nil {
132+
return nil, nil, NewArgError("createRequest", "cannot be nil")
133+
}
134+
135+
path := fmt.Sprintf(globalClustersBasePath, groupID, clusterName, "customZoneMapping")
136+
137+
req, err := s.client.NewRequest(ctx, http.MethodPost, path, createRequest)
138+
if err != nil {
139+
return nil, nil, err
140+
}
141+
142+
root := new(GlobalCluster)
143+
resp, err := s.client.Do(ctx, req, root)
144+
if err != nil {
145+
return nil, resp, err
146+
}
147+
148+
return root, resp, err
149+
}
150+
151+
//DeleteCustomZoneMappings removes all custom zone mappings from the specified Global Cluster.
152+
//See more: https://docs.atlas.mongodb.com/reference/api/global-clusters-delete-namespace/
153+
func (s *GlobalClustersServiceOp) DeleteCustomZoneMappings(ctx context.Context, groupID string, clusterName string) (*GlobalCluster, *Response, error) {
154+
path := fmt.Sprintf(globalClustersBasePath, groupID, clusterName, "customZoneMapping")
155+
156+
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
157+
if err != nil {
158+
return nil, nil, err
159+
}
160+
161+
root := new(GlobalCluster)
162+
resp, err := s.client.Do(ctx, req, root)
163+
if err != nil {
164+
return nil, resp, err
165+
}
166+
return root, resp, err
167+
}

0 commit comments

Comments
 (0)