From 92471da806de7ace444fe05f01e8dc14798e609b Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Tue, 6 Apr 2021 12:10:31 +0100 Subject: [PATCH 1/4] CLOUDP-86937: Add support to the Atlas Go Client to load sample data into cluster --- mongodbatlas/clusters.go | 65 ++++++++++++++++++++++++++++++-- mongodbatlas/clusters_test.go | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 4 deletions(-) diff --git a/mongodbatlas/clusters.go b/mongodbatlas/clusters.go index 602295603..abe5c2acd 100644 --- a/mongodbatlas/clusters.go +++ b/mongodbatlas/clusters.go @@ -24,12 +24,12 @@ import ( type ChangeStatus string const ( - ChangeStatusApplied ChangeStatus = "APPLIED" - ChangeStatusPending ChangeStatus = "PENDING" + ChangeStatusApplied ChangeStatus = "APPLIED" + ChangeStatusPending ChangeStatus = "PENDING" + clustersPath = "groups/%s/clusters" + sampleDatasetLoadPath = "groups/%s/sampleDatasetLoad" ) -const clustersPath = "groups/%s/clusters" - // ClustersService is an interface for interfacing with the Clusters // endpoints of the MongoDB Atlas API. // See more: https://docs.atlas.mongodb.com/reference/api/clusters/ @@ -42,6 +42,8 @@ type ClustersService interface { UpdateProcessArgs(ctx context.Context, groupID, clusterName string, args *ProcessArgs) (*ProcessArgs, *Response, error) GetProcessArgs(ctx context.Context, groupID, clusterName string) (*ProcessArgs, *Response, error) Status(ctx context.Context, groupID, clusterName string) (ClusterStatus, *Response, error) + LoadSampleDataset(ctx context.Context, groupID, clusterName string) (*SampleDatasetLoadJob, *Response, error) + GetSampleDataset(ctx context.Context, groupID, id string) (*SampleDatasetLoadJob, *Response, error) } // ClustersServiceOp handles communication with the Cluster related methods @@ -182,6 +184,16 @@ type clustersResponse struct { TotalCount int `json:"totalCount,omitempty"` } +// SampleDatasetLoadJob represents a sample dataset load job +type SampleDatasetLoadJob struct { + ClusterName string `json:"clusterName"` + CompleteDate string `json:"completeDate,omitempty"` + CreateDate string `json:"createDate,omitempty"` + ErrorMessage string `json:"errorMessage,omitempty"` + ID string `json:"id"` + State string `json:"state"` +} + // DefaultDiskSizeGB represents the Tier and the default disk size for each one // it can be use like: DefaultDiskSizeGB["AWS"]["M10"] var DefaultDiskSizeGB = map[string]map[string]float64{ @@ -408,6 +420,51 @@ func (s *ClustersServiceOp) GetProcessArgs(ctx context.Context, groupID, cluster return root, resp, err } +// LoadSampleDataset loads the sample dataset into your cluster. +// See more: https://docs.atlas.mongodb.com/reference/api/cluster/load-dataset/ +func (s *ClustersServiceOp) LoadSampleDataset(ctx context.Context, groupID, clusterName string) (*SampleDatasetLoadJob, *Response, error) { + if err := checkClusterNameParam(clusterName); err != nil { + return nil, nil, err + } + + basePath := fmt.Sprintf(sampleDatasetLoadPath, groupID) + escapedEntry := url.PathEscape(clusterName) + path := fmt.Sprintf("%s/%s", basePath, escapedEntry) + + req, err := s.Client.NewRequest(ctx, http.MethodPost, path, nil) + if err != nil { + return nil, nil, err + } + + root := new(SampleDatasetLoadJob) + resp, err := s.Client.Do(ctx, req, root) + if err != nil { + return nil, resp, err + } + + return root, resp, err +} + +// GetSampleDataset gets the Sample Dataset load job +// See more: https://docs.atlas.mongodb.com/reference/api/cluster/check-dataset-status/ +func (s *ClustersServiceOp) GetSampleDataset(ctx context.Context, groupID, id string) (*SampleDatasetLoadJob, *Response, error) { + basePath := fmt.Sprintf(sampleDatasetLoadPath, groupID) + path := fmt.Sprintf("%s/%s", basePath, id) + + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) + if err != nil { + return nil, nil, err + } + + root := new(SampleDatasetLoadJob) + resp, err := s.Client.Do(ctx, req, root) + if err != nil { + return nil, resp, err + } + + return root, resp, err +} + // Status gets the status of the operation on the Cluster. // // See more: https://docs.atlas.mongodb.com/reference/api/clusters-check-operation-status/ diff --git a/mongodbatlas/clusters_test.go b/mongodbatlas/clusters_test.go index fb88d62da..b88fcd873 100644 --- a/mongodbatlas/clusters_test.go +++ b/mongodbatlas/clusters_test.go @@ -1001,3 +1001,73 @@ func TestClusters_Status(t *testing.T) { t.Errorf("Expected %v but got %v", expected, status) } } + +func TestClusters_LoadSampleDataset(t *testing.T) { + client, mux, teardown := setup() + defer teardown() + + groupID := "1" + clusterName := "appData" + + mux.HandleFunc(fmt.Sprintf("/groups/%s/sampleDatasetLoad/%s", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPost) + fmt.Fprint(w, `{ + "id": "1", + "clusterName": "appData", + "completeDate": null, + "createDate": "2021-03-26T16:30:47Z", + "errorMessage": null, + "state": "WORKING"}`) + }) + + job, _, err := client.Clusters.LoadSampleDataset(ctx, groupID, clusterName) + if err != nil { + t.Fatalf("Clusters.LoadSampleDataset returned error: %v", err) + } + + expected := &SampleDatasetLoadJob{ + ClusterName: clusterName, + CreateDate: "2021-03-26T16:30:47Z", + ID: "1", + State: "WORKING", + } + + if diff := deep.Equal(job, expected); diff != nil { + t.Error(diff) + } +} + +func TestClusters_GetSampleDatasett(t *testing.T) { + client, mux, teardown := setup() + defer teardown() + + groupID := "1" + jobID := "1" + + mux.HandleFunc(fmt.Sprintf("/groups/%s/sampleDatasetLoad/%s", groupID, jobID), func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + fmt.Fprint(w, `{ + "id": "1", + "clusterName": "appData", + "completeDate": null, + "createDate": "2021-03-26T16:30:47Z", + "errorMessage": null, + "state": "WORKING"}`) + }) + + job, _, err := client.Clusters.GetSampleDataset(ctx, groupID, jobID) + if err != nil { + t.Fatalf("Clusters.GetSampleDataset returned error: %v", err) + } + + expected := &SampleDatasetLoadJob{ + ClusterName: "appData", + CreateDate: "2021-03-26T16:30:47Z", + ID: "1", + State: "WORKING", + } + + if diff := deep.Equal(job, expected); diff != nil { + t.Error(diff) + } +} From d0037111e1ebdc60e477e8a650e12b342083c5a6 Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Tue, 6 Apr 2021 12:44:40 +0100 Subject: [PATCH 2/4] Addressed PR comments --- mongodbatlas/clusters.go | 28 +++++++++++++++++++--------- mongodbatlas/clusters_test.go | 10 +++++----- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/mongodbatlas/clusters.go b/mongodbatlas/clusters.go index abe5c2acd..fe3bfb8aa 100644 --- a/mongodbatlas/clusters.go +++ b/mongodbatlas/clusters.go @@ -32,6 +32,7 @@ const ( // ClustersService is an interface for interfacing with the Clusters // endpoints of the MongoDB Atlas API. +// // See more: https://docs.atlas.mongodb.com/reference/api/clusters/ type ClustersService interface { List(ctx context.Context, groupID string, options *ListOptions) ([]Cluster, *Response, error) @@ -42,8 +43,8 @@ type ClustersService interface { UpdateProcessArgs(ctx context.Context, groupID, clusterName string, args *ProcessArgs) (*ProcessArgs, *Response, error) GetProcessArgs(ctx context.Context, groupID, clusterName string) (*ProcessArgs, *Response, error) Status(ctx context.Context, groupID, clusterName string) (ClusterStatus, *Response, error) - LoadSampleDataset(ctx context.Context, groupID, clusterName string) (*SampleDatasetLoadJob, *Response, error) - GetSampleDataset(ctx context.Context, groupID, id string) (*SampleDatasetLoadJob, *Response, error) + LoadSampleDataset(ctx context.Context, groupID, clusterName string) (*SampleDatasetJob, *Response, error) + GetSampleDatasetStatus(ctx context.Context, groupID, id string) (*SampleDatasetJob, *Response, error) } // ClustersServiceOp handles communication with the Cluster related methods @@ -184,8 +185,8 @@ type clustersResponse struct { TotalCount int `json:"totalCount,omitempty"` } -// SampleDatasetLoadJob represents a sample dataset load job -type SampleDatasetLoadJob struct { +// SampleDatasetJob represents a sample dataset load job +type SampleDatasetJob struct { ClusterName string `json:"clusterName"` CompleteDate string `json:"completeDate,omitempty"` CreateDate string `json:"createDate,omitempty"` @@ -250,6 +251,7 @@ var DefaultDiskSizeGB = map[string]map[string]float64{ } // List all clusters in the project associated to {GROUP-ID}. +// // See more: https://docs.atlas.mongodb.com/reference/api/clusters-get-all/ func (s *ClustersServiceOp) List(ctx context.Context, groupID string, listOptions *ListOptions) ([]Cluster, *Response, error) { path := fmt.Sprintf(clustersPath, groupID) @@ -279,6 +281,7 @@ func (s *ClustersServiceOp) List(ctx context.Context, groupID string, listOption } // Get gets the cluster specified to {ClUSTER-NAME} from the project associated to {GROUP-ID}. +// // See more: https://docs.atlas.mongodb.com/reference/api/clusters-get-one/ func (s *ClustersServiceOp) Get(ctx context.Context, groupID, clusterName string) (*Cluster, *Response, error) { if err := checkClusterNameParam(clusterName); err != nil { @@ -304,6 +307,7 @@ func (s *ClustersServiceOp) Get(ctx context.Context, groupID, clusterName string } // Create adds a cluster to the project associated to {GROUP-ID}. +// // See more: https://docs.atlas.mongodb.com/reference/api/clusters-create-one/ func (s *ClustersServiceOp) Create(ctx context.Context, groupID string, createRequest *Cluster) (*Cluster, *Response, error) { if createRequest == nil { @@ -327,6 +331,7 @@ func (s *ClustersServiceOp) Create(ctx context.Context, groupID string, createRe } // Update a cluster in the project associated to {GROUP-ID} +// // See more: https://docs.atlas.mongodb.com/reference/api/clusters-modify-one/ func (s *ClustersServiceOp) Update(ctx context.Context, groupID, clusterName string, updateRequest *Cluster) (*Cluster, *Response, error) { if updateRequest == nil { @@ -351,6 +356,7 @@ func (s *ClustersServiceOp) Update(ctx context.Context, groupID, clusterName str } // Delete the cluster specified to {CLUSTER-NAME} from the project associated to {GROUP-ID}. +// // See more: https://docs.atlas.mongodb.com/reference/api/clusters-delete-one/ func (s *ClustersServiceOp) Delete(ctx context.Context, groupID, clusterName string) (*Response, error) { if clusterName == "" { @@ -372,6 +378,7 @@ func (s *ClustersServiceOp) Delete(ctx context.Context, groupID, clusterName str } // UpdateProcessArgs Modifies Advanced Configuration Options for One Cluster +// // See more: https://docs.atlas.mongodb.com/reference/api/clusters-modify-advanced-configuration-options/ func (s *ClustersServiceOp) UpdateProcessArgs(ctx context.Context, groupID, clusterName string, updateRequest *ProcessArgs) (*ProcessArgs, *Response, error) { if updateRequest == nil { @@ -396,6 +403,7 @@ func (s *ClustersServiceOp) UpdateProcessArgs(ctx context.Context, groupID, clus } // GetProcessArgs gets the Advanced Configuration Options for One Cluster +// // See more: https://docs.atlas.mongodb.com/reference/api/clusters-get-advanced-configuration-options/#get-advanced-configuration-options-for-one-cluster func (s *ClustersServiceOp) GetProcessArgs(ctx context.Context, groupID, clusterName string) (*ProcessArgs, *Response, error) { if err := checkClusterNameParam(clusterName); err != nil { @@ -421,8 +429,9 @@ func (s *ClustersServiceOp) GetProcessArgs(ctx context.Context, groupID, cluster } // LoadSampleDataset loads the sample dataset into your cluster. +// // See more: https://docs.atlas.mongodb.com/reference/api/cluster/load-dataset/ -func (s *ClustersServiceOp) LoadSampleDataset(ctx context.Context, groupID, clusterName string) (*SampleDatasetLoadJob, *Response, error) { +func (s *ClustersServiceOp) LoadSampleDataset(ctx context.Context, groupID, clusterName string) (*SampleDatasetJob, *Response, error) { if err := checkClusterNameParam(clusterName); err != nil { return nil, nil, err } @@ -436,7 +445,7 @@ func (s *ClustersServiceOp) LoadSampleDataset(ctx context.Context, groupID, clus return nil, nil, err } - root := new(SampleDatasetLoadJob) + root := new(SampleDatasetJob) resp, err := s.Client.Do(ctx, req, root) if err != nil { return nil, resp, err @@ -445,9 +454,10 @@ func (s *ClustersServiceOp) LoadSampleDataset(ctx context.Context, groupID, clus return root, resp, err } -// GetSampleDataset gets the Sample Dataset load job +// GetSampleDatasetStatus gets the Sample Dataset job +// // See more: https://docs.atlas.mongodb.com/reference/api/cluster/check-dataset-status/ -func (s *ClustersServiceOp) GetSampleDataset(ctx context.Context, groupID, id string) (*SampleDatasetLoadJob, *Response, error) { +func (s *ClustersServiceOp) GetSampleDatasetStatus(ctx context.Context, groupID, id string) (*SampleDatasetJob, *Response, error) { basePath := fmt.Sprintf(sampleDatasetLoadPath, groupID) path := fmt.Sprintf("%s/%s", basePath, id) @@ -456,7 +466,7 @@ func (s *ClustersServiceOp) GetSampleDataset(ctx context.Context, groupID, id st return nil, nil, err } - root := new(SampleDatasetLoadJob) + root := new(SampleDatasetJob) resp, err := s.Client.Do(ctx, req, root) if err != nil { return nil, resp, err diff --git a/mongodbatlas/clusters_test.go b/mongodbatlas/clusters_test.go index b88fcd873..c54c4928b 100644 --- a/mongodbatlas/clusters_test.go +++ b/mongodbatlas/clusters_test.go @@ -1025,7 +1025,7 @@ func TestClusters_LoadSampleDataset(t *testing.T) { t.Fatalf("Clusters.LoadSampleDataset returned error: %v", err) } - expected := &SampleDatasetLoadJob{ + expected := &SampleDatasetJob{ ClusterName: clusterName, CreateDate: "2021-03-26T16:30:47Z", ID: "1", @@ -1037,7 +1037,7 @@ func TestClusters_LoadSampleDataset(t *testing.T) { } } -func TestClusters_GetSampleDatasett(t *testing.T) { +func TestClusters_GetSampleDatasetStatus(t *testing.T) { client, mux, teardown := setup() defer teardown() @@ -1055,12 +1055,12 @@ func TestClusters_GetSampleDatasett(t *testing.T) { "state": "WORKING"}`) }) - job, _, err := client.Clusters.GetSampleDataset(ctx, groupID, jobID) + job, _, err := client.Clusters.GetSampleDatasetStatus(ctx, groupID, jobID) if err != nil { - t.Fatalf("Clusters.GetSampleDataset returned error: %v", err) + t.Fatalf("Clusters.GetSampleDatasetStatus returned error: %v", err) } - expected := &SampleDatasetLoadJob{ + expected := &SampleDatasetJob{ ClusterName: "appData", CreateDate: "2021-03-26T16:30:47Z", ID: "1", From 24aaca3e93ee6eb2cb37b8fdb1825cc9f950885e Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Tue, 6 Apr 2021 12:46:21 +0100 Subject: [PATCH 3/4] Update clusters.go --- mongodbatlas/clusters.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongodbatlas/clusters.go b/mongodbatlas/clusters.go index fe3bfb8aa..f342d630d 100644 --- a/mongodbatlas/clusters.go +++ b/mongodbatlas/clusters.go @@ -185,7 +185,7 @@ type clustersResponse struct { TotalCount int `json:"totalCount,omitempty"` } -// SampleDatasetJob represents a sample dataset load job +// SampleDatasetJob represents a sample dataset job type SampleDatasetJob struct { ClusterName string `json:"clusterName"` CompleteDate string `json:"completeDate,omitempty"` From 6d93613b2d7416ca0625bb92bb35b76bbfb1d45b Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Tue, 6 Apr 2021 14:59:03 +0100 Subject: [PATCH 4/4] Addressed PR comment --- mongodbatlas/clusters.go | 2 +- mongodbatlas/clusters_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mongodbatlas/clusters.go b/mongodbatlas/clusters.go index f342d630d..7ffd633c1 100644 --- a/mongodbatlas/clusters.go +++ b/mongodbatlas/clusters.go @@ -191,7 +191,7 @@ type SampleDatasetJob struct { CompleteDate string `json:"completeDate,omitempty"` CreateDate string `json:"createDate,omitempty"` ErrorMessage string `json:"errorMessage,omitempty"` - ID string `json:"id"` + ID string `json:"_id"` State string `json:"state"` } diff --git a/mongodbatlas/clusters_test.go b/mongodbatlas/clusters_test.go index c54c4928b..0aed96169 100644 --- a/mongodbatlas/clusters_test.go +++ b/mongodbatlas/clusters_test.go @@ -1012,7 +1012,7 @@ func TestClusters_LoadSampleDataset(t *testing.T) { mux.HandleFunc(fmt.Sprintf("/groups/%s/sampleDatasetLoad/%s", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPost) fmt.Fprint(w, `{ - "id": "1", + "_id": "1", "clusterName": "appData", "completeDate": null, "createDate": "2021-03-26T16:30:47Z", @@ -1047,7 +1047,7 @@ func TestClusters_GetSampleDatasetStatus(t *testing.T) { mux.HandleFunc(fmt.Sprintf("/groups/%s/sampleDatasetLoad/%s", groupID, jobID), func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) fmt.Fprint(w, `{ - "id": "1", + "_id": "1", "clusterName": "appData", "completeDate": null, "createDate": "2021-03-26T16:30:47Z",