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

CLOUDP-86937: Add support to the Atlas Go Client to load sample data into a cluster #195

Merged
merged 4 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 61 additions & 4 deletions mongodbatlas/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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
Expand Down Expand Up @@ -182,6 +184,16 @@ type clustersResponse struct {
TotalCount int `json:"totalCount,omitempty"`
}

// SampleDatasetLoadJob represents a sample dataset load job
type SampleDatasetLoadJob struct {
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
ClusterName string `json:"clusterName"`
CompleteDate string `json:"completeDate,omitempty"`
CreateDate string `json:"createDate,omitempty"`
ErrorMessage string `json:"errorMessage,omitempty"`
ID string `json:"id"`
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
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{
Expand Down Expand Up @@ -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/
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
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/
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
func (s *ClustersServiceOp) GetSampleDataset(ctx context.Context, groupID, id string) (*SampleDatasetLoadJob, *Response, error) {
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
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/
Expand Down
70 changes: 70 additions & 0 deletions mongodbatlas/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}