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

feat: add test failover method #343

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions mongodbatlas/advanced_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type AdvancedClustersService interface {
Create(ctx context.Context, groupID string, cluster *AdvancedCluster) (*AdvancedCluster, *Response, error)
Update(ctx context.Context, groupID, clusterName string, cluster *AdvancedCluster) (*AdvancedCluster, *Response, error)
Delete(ctx context.Context, groupID, clusterName string) (*Response, error)
TestFailover(ctx context.Context, groupID, clusterName string) (*Response, error)
}

// AdvancedClustersServiceOp handles communication with the Cluster (Advanced) related methods
Expand Down Expand Up @@ -246,3 +247,28 @@ func (s *AdvancedClustersServiceOp) Delete(ctx context.Context, groupID, cluster

return resp, err
}

// TestFailover starts a failover test for the specified cluster in the specified project
//
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Multi-Cloud-Clusters/operation/testFailover
func (s *AdvancedClustersServiceOp) TestFailover(ctx context.Context, groupID, clusterName string) (*Response, error) {
if groupID == "" {
return nil, NewArgError("groupId", "must be set")
}
if clusterName == "" {
return nil, NewArgError("clusterName", "must be set")
}

basePath := fmt.Sprintf(advancedClustersPath, groupID)
escapedEntry := url.PathEscape(clusterName)
path := fmt.Sprintf("%s/%s/restartPrimaries", basePath, escapedEntry)

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

resp, err := s.Client.Do(ctx, req, nil)

return resp, err
}
14 changes: 14 additions & 0 deletions mongodbatlas/advanced_clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1246,3 +1246,17 @@ func TestAdvancedClusters_Delete(t *testing.T) {
t.Fatalf("Cluster.Delete returned error: %v", err)
}
}

func TestFailover(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.5/groups/%s/clusters/%s/restartPrimaries", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
})

_, err := client.AdvancedClusters.TestFailover(ctx, groupID, clusterName)
if err != nil {
t.Fatalf("Cluster.TestFailover returned error: %v", err)
}
}