Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global cluster support for advanced cluster #337

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions mongodbatlas/global_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"net/http"
)

const globalClustersBasePath = "api/atlas/v1.0/groups/%s/clusters/%s/globalWrites/%s"
const globalClustersBasePath = "api/atlas/v1.5/groups/%s/clusters/%s/globalWrites/%s"

// GlobalClustersService is an interface for interfacing with the Global Clusters
// endpoints of the MongoDB Atlas API.
Expand Down Expand Up @@ -53,6 +53,8 @@ type ManagedNamespace struct {
CustomShardKey string `json:"customShardKey,omitempty"`
IsCustomShardKeyHashed *bool `json:"isCustomShardKeyHashed,omitempty"` // Flag that specifies whether the custom shard key for the collection is hashed.
IsShardKeyUnique *bool `json:"isShardKeyUnique,omitempty"` // Flag that specifies whether the underlying index enforces a unique constraint.
NumInitialChunks int `json:"numInitialChunks,omitempty"`
PresplitHashedZones *bool `json:"presplitHashedZones,omitempty"`
}

// CustomZoneMappingsRequest represents the request related to add custom zone mappings to a global cluster.
Expand All @@ -71,10 +73,14 @@ type CustomZoneMapping struct {
// See more: https://docs.atlas.mongodb.com/reference/api/global-clusters-retrieve-namespaces/
func (s *GlobalClustersServiceOp) Get(ctx context.Context, groupID, clusterName string) (*GlobalCluster, *Response, error) {
if clusterName == "" {
return nil, nil, NewArgError("username", "must be set")
return nil, nil, NewArgError("clusterName", "must be set")
}

path := fmt.Sprintf("api/atlas/v1.0/groups/%s/clusters/%s/globalWrites", groupID, clusterName)
if groupID == "" {
return nil, nil, NewArgError("groupID", "must be set")
}

path := fmt.Sprintf(globalClustersBasePath, groupID, clusterName, "")

req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
Expand Down
20 changes: 14 additions & 6 deletions mongodbatlas/global_clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestGlobalClusters_Get(t *testing.T) {
groupID := "1"
clusterName := "appData"

mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/%s/globalWrites", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.5/groups/%s/clusters/%s/globalWrites/", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, ` {
"customZoneMapping" : {
Expand All @@ -53,7 +53,9 @@ func TestGlobalClusters_Get(t *testing.T) {
"customShardKey" : "city",
"db" : "mydata",
"isCustomShardKeyHashed" : true,
"isShardKeyUnique" : true
"isShardKeyUnique" : true,
"numInitialChunks" : 4,
"presplitHashedZones" : true
},{
"collection" : "stores",
"customShardKey" : "store_number",
Expand Down Expand Up @@ -89,6 +91,8 @@ func TestGlobalClusters_Get(t *testing.T) {
Db: "mydata",
IsCustomShardKeyHashed: pointy.Bool(true),
IsShardKeyUnique: pointy.Bool(true),
NumInitialChunks: 4,
PresplitHashedZones: pointy.Bool(true),
}, {
Collection: "stores",
CustomShardKey: "store_number",
Expand All @@ -115,15 +119,19 @@ func TestGlobalClusters_AddManagedNamespace(t *testing.T) {
CustomShardKey: "city",
IsCustomShardKeyHashed: pointy.Bool(true),
IsShardKeyUnique: pointy.Bool(true),
NumInitialChunks: 4,
PresplitHashedZones: pointy.Bool(true),
}

mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/%s/globalWrites/managedNamespaces", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.5/groups/%s/clusters/%s/globalWrites/managedNamespaces", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) {
expectedRequest := map[string]interface{}{
"db": "mydata",
"collection": "publishers",
"customShardKey": "city",
"isCustomShardKeyHashed": true,
"isShardKeyUnique": true,
"numInitialChunks": float64(4),
"presplitHashedZones": true,
}

jsonBlob := `
Expand Down Expand Up @@ -209,7 +217,7 @@ func TestGlobalClusters_DeleteManagedNamespace(t *testing.T) {
Collection: "distributors",
}

mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/%s/globalWrites/managedNamespaces", groupID, name), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.5/groups/%s/clusters/%s/globalWrites/managedNamespaces", groupID, name), func(w http.ResponseWriter, r *http.Request) {
if collection := r.URL.Query().Get("collection"); collection != mn.Collection {
t.Errorf("expected query param collection = '%s', received '%s'", mn.Collection, collection)
}
Expand Down Expand Up @@ -305,7 +313,7 @@ func TestGlobalClusters_AddCustomZoneMappings(t *testing.T) {
},
}

mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/%s/globalWrites/customZoneMapping", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.5/groups/%s/clusters/%s/globalWrites/customZoneMapping", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) {
expectedRequest := map[string]interface{}{
"customZoneMappings": []interface{}{
map[string]interface{}{"location": "CA", "zone": "Zone 1"},
Expand Down Expand Up @@ -358,7 +366,7 @@ func TestGlobalClusters_DeleteCustomZoneMappings(t *testing.T) {
groupID := "1"
name := "appData"

mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/%s/globalWrites/customZoneMapping", groupID, name), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.5/groups/%s/clusters/%s/globalWrites/customZoneMapping", groupID, name), func(w http.ResponseWriter, r *http.Request) {
jsonBlob := `
{
"customZoneMapping" : { },
Expand Down