From 6b801421e72c9293d609ac26f93cdf0a452a1224 Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Wed, 25 Jan 2023 19:07:40 +0000 Subject: [PATCH] feat: add test failover method (#343) --- mongodbatlas/advanced_clusters.go | 26 ++++++++++++++++++++++++++ mongodbatlas/advanced_clusters_test.go | 14 ++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/mongodbatlas/advanced_clusters.go b/mongodbatlas/advanced_clusters.go index 7158290ec..cc9e70b49 100644 --- a/mongodbatlas/advanced_clusters.go +++ b/mongodbatlas/advanced_clusters.go @@ -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 @@ -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 +} diff --git a/mongodbatlas/advanced_clusters_test.go b/mongodbatlas/advanced_clusters_test.go index 520881511..f9ec95556 100644 --- a/mongodbatlas/advanced_clusters_test.go +++ b/mongodbatlas/advanced_clusters_test.go @@ -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) + } +}