|
| 1 | +package mongodbatlas |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/go-test/deep" |
| 11 | +) |
| 12 | + |
| 13 | +func TestGlobalClusters_Get(t *testing.T) { |
| 14 | + setup() |
| 15 | + defer teardown() |
| 16 | + |
| 17 | + groupID := "1" |
| 18 | + clusterName := "appData" |
| 19 | + |
| 20 | + mux.HandleFunc(fmt.Sprintf("/groups/%s/clusters/%s/globalWrites", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) { |
| 21 | + testMethod(t, r, http.MethodGet) |
| 22 | + fmt.Fprint(w, ` { |
| 23 | + "customZoneMapping" : { |
| 24 | + "AF" : "5b48f5a780eef5236f689f94", |
| 25 | + "AL" : "5b48f5a780eef5236f689f94", |
| 26 | + "AU" : "5b48f5cddff5220000f7f375", |
| 27 | + "AU-ACT" : "5b48f5cddff5220000f7f375", |
| 28 | + "AU-NSW" : "5b48f5cddff5220000f7f375", |
| 29 | + "AU-NT" : "5b48f5cddff5220000f7f375", |
| 30 | + "AU-QLD" : "5b48f5cddff5220000f7f375", |
| 31 | + "AU-SA" : "5b48f5cddff5220000f7f375", |
| 32 | + "AU-TAS" : "5b48f5cddff5220000f7f375", |
| 33 | + "AU-VIC" : "5b48f5cddff5220000f7f375", |
| 34 | + "AU-WA" : "5b48f5cddff5220000f7f375", |
| 35 | + "DZ" : "5b48f5a780eef5236f689f94" |
| 36 | + }, |
| 37 | + "managedNamespaces" : [ { |
| 38 | + "collection" : "zips", |
| 39 | + "customShardKey" : "city", |
| 40 | + "db" : "mydata" |
| 41 | + },{ |
| 42 | + "collection" : "stores", |
| 43 | + "customShardKey" : "store_number", |
| 44 | + "db" : "mydata" |
| 45 | + } ] |
| 46 | + }`) |
| 47 | + }) |
| 48 | + |
| 49 | + cluster, _, err := client.GlobalClusters.Get(ctx, groupID, clusterName) |
| 50 | + if err != nil { |
| 51 | + t.Errorf("GlobalClusters.Get returned error: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + expected := &GlobalCluster{ |
| 55 | + CustomZoneMapping: map[string]string{ |
| 56 | + "AF": "5b48f5a780eef5236f689f94", |
| 57 | + "AL": "5b48f5a780eef5236f689f94", |
| 58 | + "AU": "5b48f5cddff5220000f7f375", |
| 59 | + "AU-ACT": "5b48f5cddff5220000f7f375", |
| 60 | + "AU-NSW": "5b48f5cddff5220000f7f375", |
| 61 | + "AU-NT": "5b48f5cddff5220000f7f375", |
| 62 | + "AU-QLD": "5b48f5cddff5220000f7f375", |
| 63 | + "AU-SA": "5b48f5cddff5220000f7f375", |
| 64 | + "AU-TAS": "5b48f5cddff5220000f7f375", |
| 65 | + "AU-VIC": "5b48f5cddff5220000f7f375", |
| 66 | + "AU-WA": "5b48f5cddff5220000f7f375", |
| 67 | + "DZ": "5b48f5a780eef5236f689f94", |
| 68 | + }, |
| 69 | + ManagedNamespaces: []ManagedNamespace{ |
| 70 | + { |
| 71 | + Collection: "zips", |
| 72 | + CustomShardKey: "city", |
| 73 | + Db: "mydata", |
| 74 | + }, { |
| 75 | + Collection: "stores", |
| 76 | + CustomShardKey: "store_number", |
| 77 | + Db: "mydata", |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + if !reflect.DeepEqual(cluster, expected) { |
| 83 | + t.Errorf("GlobalClusters.Get\n got=%#v\nwant=%#v", cluster, expected) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestGlobalClusters_AddManagedNamespace(t *testing.T) { |
| 88 | + setup() |
| 89 | + defer teardown() |
| 90 | + |
| 91 | + groupID := "1" |
| 92 | + clusterName := "appData" |
| 93 | + |
| 94 | + createRequest := &ManagedNamespace{ |
| 95 | + Db: "mydata", |
| 96 | + Collection: "publishers", |
| 97 | + CustomShardKey: "city", |
| 98 | + } |
| 99 | + |
| 100 | + mux.HandleFunc(fmt.Sprintf("/groups/%s/clusters/%s/globalWrites/managedNamespaces", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) { |
| 101 | + expectedRequest := map[string]interface{}{ |
| 102 | + "db": "mydata", |
| 103 | + "collection": "publishers", |
| 104 | + "customShardKey": "city", |
| 105 | + } |
| 106 | + |
| 107 | + jsonBlob := ` |
| 108 | + { |
| 109 | + "customZoneMapping" : { |
| 110 | + "AF" : "5b48f5a780eef5236f689f94", |
| 111 | + "AL" : "5b48f5a780eef5236f689f94", |
| 112 | + "AU" : "5b48f5cddff5220000f7f375", |
| 113 | + "AU-ACT" : "5b48f5cddff5220000f7f375", |
| 114 | + "AU-NSW" : "5b48f5cddff5220000f7f375", |
| 115 | + "AU-NT" : "5b48f5cddff5220000f7f375", |
| 116 | + "AU-QLD" : "5b48f5cddff5220000f7f375", |
| 117 | + "AU-SA" : "5b48f5cddff5220000f7f375", |
| 118 | + "AU-TAS" : "5b48f5cddff5220000f7f375", |
| 119 | + "AU-VIC" : "5b48f5cddff5220000f7f375", |
| 120 | + "AU-WA" : "5b48f5cddff5220000f7f375", |
| 121 | + "DZ" : "5b48f5a780eef5236f689f94" |
| 122 | + }, |
| 123 | + "managedNamespaces" : [ { |
| 124 | + "collection" : "publishers", |
| 125 | + "customShardKey" : "city", |
| 126 | + "db" : "mydata" |
| 127 | + },{ |
| 128 | + "collection" : "stores", |
| 129 | + "customShardKey" : "store_number", |
| 130 | + "db" : "mydata" |
| 131 | + } ] |
| 132 | + } |
| 133 | + ` |
| 134 | + |
| 135 | + var v map[string]interface{} |
| 136 | + err := json.NewDecoder(r.Body).Decode(&v) |
| 137 | + if err != nil { |
| 138 | + t.Fatalf("decode json: %v", err) |
| 139 | + } |
| 140 | + if diff := deep.Equal(v, expectedRequest); diff != nil { |
| 141 | + t.Errorf("GlobalClusters.AddManagedNamespace Request Body = %v", diff) |
| 142 | + } |
| 143 | + |
| 144 | + if !reflect.DeepEqual(v, expectedRequest) { |
| 145 | + t.Errorf("Request body\n got=%#v\nwant=%#v", v, expectedRequest) |
| 146 | + } |
| 147 | + |
| 148 | + fmt.Fprint(w, jsonBlob) |
| 149 | + }) |
| 150 | + |
| 151 | + globalCluster, _, err := client.GlobalClusters.AddManagedNamespace(ctx, groupID, clusterName, createRequest) |
| 152 | + if err != nil { |
| 153 | + t.Errorf("GlobalClusters.AddManagedNamespace returned error: %v", err) |
| 154 | + } |
| 155 | + |
| 156 | + if namespacesCount := len(globalCluster.ManagedNamespaces); namespacesCount != 2 { |
| 157 | + t.Errorf("expected name '%d', received '%d'", 2, namespacesCount) |
| 158 | + } |
| 159 | + |
| 160 | + expectedCustomZoneMapping := map[string]string{ |
| 161 | + "AF": "5b48f5a780eef5236f689f94", |
| 162 | + "AL": "5b48f5a780eef5236f689f94", |
| 163 | + "AU": "5b48f5cddff5220000f7f375", |
| 164 | + "AU-ACT": "5b48f5cddff5220000f7f375", |
| 165 | + "AU-NSW": "5b48f5cddff5220000f7f375", |
| 166 | + "AU-NT": "5b48f5cddff5220000f7f375", |
| 167 | + "AU-QLD": "5b48f5cddff5220000f7f375", |
| 168 | + "AU-SA": "5b48f5cddff5220000f7f375", |
| 169 | + "AU-TAS": "5b48f5cddff5220000f7f375", |
| 170 | + "AU-VIC": "5b48f5cddff5220000f7f375", |
| 171 | + "AU-WA": "5b48f5cddff5220000f7f375", |
| 172 | + "DZ": "5b48f5a780eef5236f689f94", |
| 173 | + } |
| 174 | + |
| 175 | + if diff := deep.Equal(globalCluster.CustomZoneMapping, expectedCustomZoneMapping); diff != nil { |
| 176 | + t.Errorf("expected globalCluster.CustomZoneMapping = %v", diff) |
| 177 | + } |
| 178 | + |
| 179 | +} |
| 180 | + |
| 181 | +func TestGlobalClusters_DeleteManagedNamespace(t *testing.T) { |
| 182 | + setup() |
| 183 | + defer teardown() |
| 184 | + |
| 185 | + groupID := "1" |
| 186 | + name := "appData" |
| 187 | + |
| 188 | + mn := ManagedNamespace{ |
| 189 | + Db: "data", |
| 190 | + Collection: "distributors", |
| 191 | + } |
| 192 | + |
| 193 | + mux.HandleFunc(fmt.Sprintf("/groups/%s/clusters/%s/globalWrites/managedNamespaces", groupID, name), func(w http.ResponseWriter, r *http.Request) { |
| 194 | + |
| 195 | + if collection := r.URL.Query().Get("collection"); collection != mn.Collection { |
| 196 | + t.Errorf("expected query param collection = '%s', received '%s'", mn.Collection, collection) |
| 197 | + } |
| 198 | + |
| 199 | + if db := r.URL.Query().Get("db"); db != mn.Db { |
| 200 | + t.Errorf("expected query param db = '%s', received '%s'", mn.Collection, db) |
| 201 | + } |
| 202 | + |
| 203 | + jsonBlob := ` |
| 204 | + { |
| 205 | + "customZoneMapping" : { |
| 206 | + "AF" : "5b48f5a780eef5236f689f94", |
| 207 | + "AL" : "5b48f5a780eef5236f689f94", |
| 208 | + "AU" : "5b48f5cddff5220000f7f375", |
| 209 | + "AU-ACT" : "5b48f5cddff5220000f7f375", |
| 210 | + "AU-NSW" : "5b48f5cddff5220000f7f375", |
| 211 | + "AU-NT" : "5b48f5cddff5220000f7f375", |
| 212 | + "AU-QLD" : "5b48f5cddff5220000f7f375", |
| 213 | + "AU-SA" : "5b48f5cddff5220000f7f375", |
| 214 | + "AU-TAS" : "5b48f5cddff5220000f7f375", |
| 215 | + "AU-VIC" : "5b48f5cddff5220000f7f375", |
| 216 | + "AU-WA" : "5b48f5cddff5220000f7f375", |
| 217 | + "DZ" : "5b48f5a780eef5236f689f94" |
| 218 | + }, |
| 219 | + "managedNamespaces" : [ { |
| 220 | + "collection" : "zip-codes", |
| 221 | + "customShardKey" : "city", |
| 222 | + "db" : "data" |
| 223 | + }, { |
| 224 | + "collection" : "retail-stores", |
| 225 | + "customShardKey" : "store-number", |
| 226 | + "db" : "mydata" |
| 227 | + } ] |
| 228 | + } |
| 229 | + ` |
| 230 | + testMethod(t, r, http.MethodDelete) |
| 231 | + |
| 232 | + fmt.Fprint(w, jsonBlob) |
| 233 | + }) |
| 234 | + |
| 235 | + globalCluster, _, err := client.GlobalClusters.DeleteManagedNamespace(ctx, groupID, name, &mn) |
| 236 | + |
| 237 | + if err != nil { |
| 238 | + t.Errorf("GlobalClusters.DeleteManagedNamespace returned error: %v", err) |
| 239 | + } |
| 240 | + |
| 241 | + expected := &GlobalCluster{ |
| 242 | + CustomZoneMapping: map[string]string{ |
| 243 | + "AF": "5b48f5a780eef5236f689f94", |
| 244 | + "AL": "5b48f5a780eef5236f689f94", |
| 245 | + "AU": "5b48f5cddff5220000f7f375", |
| 246 | + "AU-ACT": "5b48f5cddff5220000f7f375", |
| 247 | + "AU-NSW": "5b48f5cddff5220000f7f375", |
| 248 | + "AU-NT": "5b48f5cddff5220000f7f375", |
| 249 | + "AU-QLD": "5b48f5cddff5220000f7f375", |
| 250 | + "AU-SA": "5b48f5cddff5220000f7f375", |
| 251 | + "AU-TAS": "5b48f5cddff5220000f7f375", |
| 252 | + "AU-VIC": "5b48f5cddff5220000f7f375", |
| 253 | + "AU-WA": "5b48f5cddff5220000f7f375", |
| 254 | + "DZ": "5b48f5a780eef5236f689f94", |
| 255 | + }, |
| 256 | + ManagedNamespaces: []ManagedNamespace{ |
| 257 | + { |
| 258 | + Collection: "zip-codes", |
| 259 | + CustomShardKey: "city", |
| 260 | + Db: "data", |
| 261 | + }, { |
| 262 | + Collection: "retail-stores", |
| 263 | + CustomShardKey: "store-number", |
| 264 | + Db: "mydata", |
| 265 | + }, |
| 266 | + }, |
| 267 | + } |
| 268 | + |
| 269 | + if diff := deep.Equal(globalCluster, expected); diff != nil { |
| 270 | + t.Errorf("GlobalClusters.DeleteCustomZoneMappings Reponse Body = %v", diff) |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +func TestGlobalClusters_AddCustomZoneMappings(t *testing.T) { |
| 275 | + setup() |
| 276 | + defer teardown() |
| 277 | + |
| 278 | + groupID := "1" |
| 279 | + clusterName := "appData" |
| 280 | + |
| 281 | + createRequest := &CustomZoneMappingsRequest{ |
| 282 | + CustomZoneMappings: []CustomZoneMapping{ |
| 283 | + { |
| 284 | + Location: "CA", |
| 285 | + Zone: "Zone 1", |
| 286 | + }, |
| 287 | + }, |
| 288 | + } |
| 289 | + |
| 290 | + mux.HandleFunc(fmt.Sprintf("/groups/%s/clusters/%s/globalWrites/customZoneMapping", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) { |
| 291 | + expectedRequest := map[string]interface{}{ |
| 292 | + "customZoneMappings": []interface{}{ |
| 293 | + map[string]interface{}{"location": "CA", "zone": "Zone 1"}, |
| 294 | + }, |
| 295 | + } |
| 296 | + |
| 297 | + jsonBlob := ` |
| 298 | + { |
| 299 | + "customZoneMapping" : { |
| 300 | + "CA" : "5b50bf4180eef547653df4d0" |
| 301 | + }, |
| 302 | + "managedNamespaces" : [ { |
| 303 | + "collection" : "publishers", |
| 304 | + "customShardKey" : "city", |
| 305 | + "db" : "myData" |
| 306 | + } ] |
| 307 | + } |
| 308 | + ` |
| 309 | + |
| 310 | + var v map[string]interface{} |
| 311 | + err := json.NewDecoder(r.Body).Decode(&v) |
| 312 | + if err != nil { |
| 313 | + t.Fatalf("decode json: %v", err) |
| 314 | + } |
| 315 | + if diff := deep.Equal(v, expectedRequest); diff != nil { |
| 316 | + t.Errorf("GlobalClusters.AddCustomZoneMappings Request Body = %v", diff) |
| 317 | + } |
| 318 | + |
| 319 | + if !reflect.DeepEqual(v, expectedRequest) { |
| 320 | + t.Errorf("Request body\n got=%#v\nwant=%#v", v, expectedRequest) |
| 321 | + } |
| 322 | + |
| 323 | + fmt.Fprint(w, jsonBlob) |
| 324 | + }) |
| 325 | + |
| 326 | + globalCluster, _, err := client.GlobalClusters.AddCustomZoneMappings(ctx, groupID, clusterName, createRequest) |
| 327 | + if err != nil { |
| 328 | + t.Errorf("GlobalClusters.AddCustomZoneMappings returned error: %v", err) |
| 329 | + } |
| 330 | + |
| 331 | + if namespacesCount := len(globalCluster.ManagedNamespaces); namespacesCount != 1 { |
| 332 | + t.Errorf("expected name '%d', received '%d'", 1, namespacesCount) |
| 333 | + } |
| 334 | + |
| 335 | + if id := globalCluster.CustomZoneMapping["CA"]; id != "5b50bf4180eef547653df4d0" { |
| 336 | + t.Errorf("expected CustomZoneMapping.CA '%s', received '%s'", "5b50bf4180eef547653df4d0", id) |
| 337 | + } |
| 338 | + |
| 339 | +} |
| 340 | + |
| 341 | +func TestGlobalClusters_DeleteCustomZoneMappings(t *testing.T) { |
| 342 | + setup() |
| 343 | + defer teardown() |
| 344 | + |
| 345 | + groupID := "1" |
| 346 | + name := "appData" |
| 347 | + |
| 348 | + mux.HandleFunc(fmt.Sprintf("/groups/%s/clusters/%s/globalWrites/customZoneMapping", groupID, name), func(w http.ResponseWriter, r *http.Request) { |
| 349 | + |
| 350 | + jsonBlob := ` |
| 351 | + { |
| 352 | + "customZoneMapping" : { }, |
| 353 | + "managedNamespaces" : [ { |
| 354 | + "collection" : "publishers", |
| 355 | + "customShardKey" : "city", |
| 356 | + "db" : "myData" |
| 357 | + } ] |
| 358 | + } |
| 359 | + ` |
| 360 | + testMethod(t, r, http.MethodDelete) |
| 361 | + |
| 362 | + fmt.Fprint(w, jsonBlob) |
| 363 | + }) |
| 364 | + |
| 365 | + globalCluster, _, err := client.GlobalClusters.DeleteCustomZoneMappings(ctx, groupID, name) |
| 366 | + |
| 367 | + if err != nil { |
| 368 | + t.Errorf("Cluster.Delete returned error: %v", err) |
| 369 | + } |
| 370 | + |
| 371 | + expected := &GlobalCluster{ |
| 372 | + CustomZoneMapping: map[string]string{}, |
| 373 | + ManagedNamespaces: []ManagedNamespace{ |
| 374 | + { |
| 375 | + Collection: "publishers", |
| 376 | + CustomShardKey: "city", |
| 377 | + Db: "myData", |
| 378 | + }, |
| 379 | + }, |
| 380 | + } |
| 381 | + |
| 382 | + if diff := deep.Equal(globalCluster, expected); diff != nil { |
| 383 | + t.Errorf("GlobalClusters.AddCustomZoneMappings Reponse Body = %v", diff) |
| 384 | + } |
| 385 | +} |
0 commit comments