Skip to content

Commit 368ac0d

Browse files
CLOUDP-86937: Add support to the Atlas Go Client to load sample data into a cluster (#195)
1 parent 6a1b38c commit 368ac0d

File tree

2 files changed

+141
-4
lines changed

2 files changed

+141
-4
lines changed

mongodbatlas/clusters.go

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ import (
2424
type ChangeStatus string
2525

2626
const (
27-
ChangeStatusApplied ChangeStatus = "APPLIED"
28-
ChangeStatusPending ChangeStatus = "PENDING"
27+
ChangeStatusApplied ChangeStatus = "APPLIED"
28+
ChangeStatusPending ChangeStatus = "PENDING"
29+
clustersPath = "groups/%s/clusters"
30+
sampleDatasetLoadPath = "groups/%s/sampleDatasetLoad"
2931
)
3032

31-
const clustersPath = "groups/%s/clusters"
32-
3333
// ClustersService is an interface for interfacing with the Clusters
3434
// endpoints of the MongoDB Atlas API.
35+
//
3536
// See more: https://docs.atlas.mongodb.com/reference/api/clusters/
3637
type ClustersService interface {
3738
List(ctx context.Context, groupID string, options *ListOptions) ([]Cluster, *Response, error)
@@ -42,6 +43,8 @@ type ClustersService interface {
4243
UpdateProcessArgs(ctx context.Context, groupID, clusterName string, args *ProcessArgs) (*ProcessArgs, *Response, error)
4344
GetProcessArgs(ctx context.Context, groupID, clusterName string) (*ProcessArgs, *Response, error)
4445
Status(ctx context.Context, groupID, clusterName string) (ClusterStatus, *Response, error)
46+
LoadSampleDataset(ctx context.Context, groupID, clusterName string) (*SampleDatasetJob, *Response, error)
47+
GetSampleDatasetStatus(ctx context.Context, groupID, id string) (*SampleDatasetJob, *Response, error)
4548
}
4649

4750
// ClustersServiceOp handles communication with the Cluster related methods
@@ -182,6 +185,16 @@ type clustersResponse struct {
182185
TotalCount int `json:"totalCount,omitempty"`
183186
}
184187

188+
// SampleDatasetJob represents a sample dataset job
189+
type SampleDatasetJob struct {
190+
ClusterName string `json:"clusterName"`
191+
CompleteDate string `json:"completeDate,omitempty"`
192+
CreateDate string `json:"createDate,omitempty"`
193+
ErrorMessage string `json:"errorMessage,omitempty"`
194+
ID string `json:"_id"`
195+
State string `json:"state"`
196+
}
197+
185198
// DefaultDiskSizeGB represents the Tier and the default disk size for each one
186199
// it can be use like: DefaultDiskSizeGB["AWS"]["M10"]
187200
var DefaultDiskSizeGB = map[string]map[string]float64{
@@ -238,6 +251,7 @@ var DefaultDiskSizeGB = map[string]map[string]float64{
238251
}
239252

240253
// List all clusters in the project associated to {GROUP-ID}.
254+
//
241255
// See more: https://docs.atlas.mongodb.com/reference/api/clusters-get-all/
242256
func (s *ClustersServiceOp) List(ctx context.Context, groupID string, listOptions *ListOptions) ([]Cluster, *Response, error) {
243257
path := fmt.Sprintf(clustersPath, groupID)
@@ -267,6 +281,7 @@ func (s *ClustersServiceOp) List(ctx context.Context, groupID string, listOption
267281
}
268282

269283
// Get gets the cluster specified to {ClUSTER-NAME} from the project associated to {GROUP-ID}.
284+
//
270285
// See more: https://docs.atlas.mongodb.com/reference/api/clusters-get-one/
271286
func (s *ClustersServiceOp) Get(ctx context.Context, groupID, clusterName string) (*Cluster, *Response, error) {
272287
if err := checkClusterNameParam(clusterName); err != nil {
@@ -292,6 +307,7 @@ func (s *ClustersServiceOp) Get(ctx context.Context, groupID, clusterName string
292307
}
293308

294309
// Create adds a cluster to the project associated to {GROUP-ID}.
310+
//
295311
// See more: https://docs.atlas.mongodb.com/reference/api/clusters-create-one/
296312
func (s *ClustersServiceOp) Create(ctx context.Context, groupID string, createRequest *Cluster) (*Cluster, *Response, error) {
297313
if createRequest == nil {
@@ -315,6 +331,7 @@ func (s *ClustersServiceOp) Create(ctx context.Context, groupID string, createRe
315331
}
316332

317333
// Update a cluster in the project associated to {GROUP-ID}
334+
//
318335
// See more: https://docs.atlas.mongodb.com/reference/api/clusters-modify-one/
319336
func (s *ClustersServiceOp) Update(ctx context.Context, groupID, clusterName string, updateRequest *Cluster) (*Cluster, *Response, error) {
320337
if updateRequest == nil {
@@ -339,6 +356,7 @@ func (s *ClustersServiceOp) Update(ctx context.Context, groupID, clusterName str
339356
}
340357

341358
// Delete the cluster specified to {CLUSTER-NAME} from the project associated to {GROUP-ID}.
359+
//
342360
// See more: https://docs.atlas.mongodb.com/reference/api/clusters-delete-one/
343361
func (s *ClustersServiceOp) Delete(ctx context.Context, groupID, clusterName string) (*Response, error) {
344362
if clusterName == "" {
@@ -360,6 +378,7 @@ func (s *ClustersServiceOp) Delete(ctx context.Context, groupID, clusterName str
360378
}
361379

362380
// UpdateProcessArgs Modifies Advanced Configuration Options for One Cluster
381+
//
363382
// See more: https://docs.atlas.mongodb.com/reference/api/clusters-modify-advanced-configuration-options/
364383
func (s *ClustersServiceOp) UpdateProcessArgs(ctx context.Context, groupID, clusterName string, updateRequest *ProcessArgs) (*ProcessArgs, *Response, error) {
365384
if updateRequest == nil {
@@ -384,6 +403,7 @@ func (s *ClustersServiceOp) UpdateProcessArgs(ctx context.Context, groupID, clus
384403
}
385404

386405
// GetProcessArgs gets the Advanced Configuration Options for One Cluster
406+
//
387407
// See more: https://docs.atlas.mongodb.com/reference/api/clusters-get-advanced-configuration-options/#get-advanced-configuration-options-for-one-cluster
388408
func (s *ClustersServiceOp) GetProcessArgs(ctx context.Context, groupID, clusterName string) (*ProcessArgs, *Response, error) {
389409
if err := checkClusterNameParam(clusterName); err != nil {
@@ -408,6 +428,53 @@ func (s *ClustersServiceOp) GetProcessArgs(ctx context.Context, groupID, cluster
408428
return root, resp, err
409429
}
410430

431+
// LoadSampleDataset loads the sample dataset into your cluster.
432+
//
433+
// See more: https://docs.atlas.mongodb.com/reference/api/cluster/load-dataset/
434+
func (s *ClustersServiceOp) LoadSampleDataset(ctx context.Context, groupID, clusterName string) (*SampleDatasetJob, *Response, error) {
435+
if err := checkClusterNameParam(clusterName); err != nil {
436+
return nil, nil, err
437+
}
438+
439+
basePath := fmt.Sprintf(sampleDatasetLoadPath, groupID)
440+
escapedEntry := url.PathEscape(clusterName)
441+
path := fmt.Sprintf("%s/%s", basePath, escapedEntry)
442+
443+
req, err := s.Client.NewRequest(ctx, http.MethodPost, path, nil)
444+
if err != nil {
445+
return nil, nil, err
446+
}
447+
448+
root := new(SampleDatasetJob)
449+
resp, err := s.Client.Do(ctx, req, root)
450+
if err != nil {
451+
return nil, resp, err
452+
}
453+
454+
return root, resp, err
455+
}
456+
457+
// GetSampleDatasetStatus gets the Sample Dataset job
458+
//
459+
// See more: https://docs.atlas.mongodb.com/reference/api/cluster/check-dataset-status/
460+
func (s *ClustersServiceOp) GetSampleDatasetStatus(ctx context.Context, groupID, id string) (*SampleDatasetJob, *Response, error) {
461+
basePath := fmt.Sprintf(sampleDatasetLoadPath, groupID)
462+
path := fmt.Sprintf("%s/%s", basePath, id)
463+
464+
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
465+
if err != nil {
466+
return nil, nil, err
467+
}
468+
469+
root := new(SampleDatasetJob)
470+
resp, err := s.Client.Do(ctx, req, root)
471+
if err != nil {
472+
return nil, resp, err
473+
}
474+
475+
return root, resp, err
476+
}
477+
411478
// Status gets the status of the operation on the Cluster.
412479
//
413480
// See more: https://docs.atlas.mongodb.com/reference/api/clusters-check-operation-status/

mongodbatlas/clusters_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,3 +1001,73 @@ func TestClusters_Status(t *testing.T) {
10011001
t.Errorf("Expected %v but got %v", expected, status)
10021002
}
10031003
}
1004+
1005+
func TestClusters_LoadSampleDataset(t *testing.T) {
1006+
client, mux, teardown := setup()
1007+
defer teardown()
1008+
1009+
groupID := "1"
1010+
clusterName := "appData"
1011+
1012+
mux.HandleFunc(fmt.Sprintf("/groups/%s/sampleDatasetLoad/%s", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) {
1013+
testMethod(t, r, http.MethodPost)
1014+
fmt.Fprint(w, `{
1015+
"_id": "1",
1016+
"clusterName": "appData",
1017+
"completeDate": null,
1018+
"createDate": "2021-03-26T16:30:47Z",
1019+
"errorMessage": null,
1020+
"state": "WORKING"}`)
1021+
})
1022+
1023+
job, _, err := client.Clusters.LoadSampleDataset(ctx, groupID, clusterName)
1024+
if err != nil {
1025+
t.Fatalf("Clusters.LoadSampleDataset returned error: %v", err)
1026+
}
1027+
1028+
expected := &SampleDatasetJob{
1029+
ClusterName: clusterName,
1030+
CreateDate: "2021-03-26T16:30:47Z",
1031+
ID: "1",
1032+
State: "WORKING",
1033+
}
1034+
1035+
if diff := deep.Equal(job, expected); diff != nil {
1036+
t.Error(diff)
1037+
}
1038+
}
1039+
1040+
func TestClusters_GetSampleDatasetStatus(t *testing.T) {
1041+
client, mux, teardown := setup()
1042+
defer teardown()
1043+
1044+
groupID := "1"
1045+
jobID := "1"
1046+
1047+
mux.HandleFunc(fmt.Sprintf("/groups/%s/sampleDatasetLoad/%s", groupID, jobID), func(w http.ResponseWriter, r *http.Request) {
1048+
testMethod(t, r, http.MethodGet)
1049+
fmt.Fprint(w, `{
1050+
"_id": "1",
1051+
"clusterName": "appData",
1052+
"completeDate": null,
1053+
"createDate": "2021-03-26T16:30:47Z",
1054+
"errorMessage": null,
1055+
"state": "WORKING"}`)
1056+
})
1057+
1058+
job, _, err := client.Clusters.GetSampleDatasetStatus(ctx, groupID, jobID)
1059+
if err != nil {
1060+
t.Fatalf("Clusters.GetSampleDatasetStatus returned error: %v", err)
1061+
}
1062+
1063+
expected := &SampleDatasetJob{
1064+
ClusterName: "appData",
1065+
CreateDate: "2021-03-26T16:30:47Z",
1066+
ID: "1",
1067+
State: "WORKING",
1068+
}
1069+
1070+
if diff := deep.Equal(job, expected); diff != nil {
1071+
t.Error(diff)
1072+
}
1073+
}

0 commit comments

Comments
 (0)