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 3 commits
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
75 changes: 71 additions & 4 deletions mongodbatlas/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ 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/
type ClustersService interface {
List(ctx context.Context, groupID string, options *ListOptions) ([]Cluster, *Response, error)
Expand All @@ -42,6 +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) (*SampleDatasetJob, *Response, error)
GetSampleDatasetStatus(ctx context.Context, groupID, id string) (*SampleDatasetJob, *Response, error)
}

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

// SampleDatasetJob represents a sample dataset job
type SampleDatasetJob struct {
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 @@ -238,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)
Expand Down Expand Up @@ -267,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 {
Expand All @@ -292,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 {
Expand All @@ -315,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 {
Expand All @@ -339,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 == "" {
Expand All @@ -360,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 {
Expand All @@ -384,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 {
Expand All @@ -408,6 +428,53 @@ 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) (*SampleDatasetJob, *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(SampleDatasetJob)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, err
}

// GetSampleDatasetStatus gets the Sample Dataset job
//
// See more: https://docs.atlas.mongodb.com/reference/api/cluster/check-dataset-status/
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)

req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(SampleDatasetJob)
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 := &SampleDatasetJob{
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_GetSampleDatasetStatus(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.GetSampleDatasetStatus(ctx, groupID, jobID)
if err != nil {
t.Fatalf("Clusters.GetSampleDatasetStatus returned error: %v", err)
}

expected := &SampleDatasetJob{
ClusterName: "appData",
CreateDate: "2021-03-26T16:30:47Z",
ID: "1",
State: "WORKING",
}

if diff := deep.Equal(job, expected); diff != nil {
t.Error(diff)
}
}