Skip to content

Commit

Permalink
INTMDB-247: Fixes a bug where it's taking 3 minutes to read a cluster (
Browse files Browse the repository at this point in the history
…#530)

* fix: fixes a bug when it's taking long to finish for reading a cluster

* deleted delay

* added nl

* refactor: deleted refresh status in datasource cluster and changed time of delay in endpoint service

Co-authored-by: Edgar Lopez <[email protected]>
  • Loading branch information
coderGo93 and Edgar Lopez authored Aug 27, 2021
1 parent 908a03e commit 6995d9c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 63 deletions.
13 changes: 10 additions & 3 deletions examples/aws-atlas-privatelink/atlas-cluster.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "mongodbatlas_cluster" "cluster-atlas" {
project_id = var.atlasprojectid
name = "cluster-atlas"
provider_backup_enabled = true
cloud_backup = true
auto_scaling_disk_gb_enabled = true
mongo_db_major_version = "4.2"
cluster_type = "REPLICASET"
Expand All @@ -19,9 +19,16 @@ resource "mongodbatlas_cluster" "cluster-atlas" {
disk_size_gb = 10
provider_instance_size_name = "M10"
}

data "mongodbatlas_cluster" "cluster-atlas" {
project_id = var.atlasprojectid
name = mongodbatlas_cluster.cluster-atlas.name
depends_on = [mongodbatlas_privatelink_endpoint_service.atlaseplink]
}

output "atlasclusterstring" {
value = mongodbatlas_cluster.cluster-atlas.connection_strings
value = data.mongodbatlas_cluster.cluster-atlas.connection_strings
}
output "plstring" {
value = lookup(mongodbatlas_cluster.cluster-atlas.connection_strings[0].aws_private_link_srv, aws_vpc_endpoint.ptfe_service.id)
value = lookup(data.mongodbatlas_cluster.cluster-atlas.connection_strings[0].aws_private_link_srv, aws_vpc_endpoint.ptfe_service.id)
}
7 changes: 4 additions & 3 deletions examples/aws-atlas-privatelink/atlas-pl.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ resource "aws_vpc_endpoint" "ptfe_service" {
}

resource "mongodbatlas_privatelink_endpoint_service" "atlaseplink" {
project_id = mongodbatlas_privatelink_endpoint.atlaspl.project_id
private_link_id = mongodbatlas_privatelink_endpoint.atlaspl.private_link_id
interface_endpoint_id = aws_vpc_endpoint.ptfe_service.id
project_id = mongodbatlas_privatelink_endpoint.atlaspl.project_id
endpoint_service_id = aws_vpc_endpoint.ptfe_service.id
private_link_id = mongodbatlas_privatelink_endpoint.atlaspl.id
provider_name = "AWS"
}
56 changes: 0 additions & 56 deletions mongodbatlas/data_source_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ package mongodbatlas
import (
"context"
"fmt"
"log"
"net/http"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spf13/cast"
matlas "go.mongodb.org/atlas/mongodbatlas"
)

func dataSourceMongoDBAtlasCluster() *schema.Resource {
Expand Down Expand Up @@ -330,25 +325,6 @@ func dataSourceMongoDBAtlasClusterRead(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(fmt.Errorf(errorClusterRead, clusterName, err))
}

if cluster.ProviderSettings != nil && (cast.ToString(cluster.ProviderSettings.ProviderName) == "AWS" ||
cast.ToString(cluster.ProviderSettings.ProviderName) == "AZURE") {
stateConf := &resource.StateChangeConf{
Pending: []string{"PRIVATE_ENDPOINTS_NIL", "PRIVATE_ENDPOINTS_EMPTY"},
Target: []string{"PRIVATE_ENDPOINTS_EXISTS", "NORMAL"},
Refresh: datasourceClusterPrivateEndpointRefreshFunc(clusterName, projectID, conn),
Timeout: 10 * time.Minute,
MinTimeout: 1 * time.Minute,
Delay: 3 * time.Minute,
}

resp, err := stateConf.WaitForStateContext(ctx)
if err != nil {
log.Printf("[ERROR] %v", fmt.Errorf(errorClusterRead, clusterName, err))
} else {
cluster = resp.(*matlas.Cluster)
}
}

if err := d.Set("auto_scaling_disk_gb_enabled", cluster.AutoScaling.DiskGBEnabled); err != nil {
return diag.FromErr(fmt.Errorf(errorClusterSetting, "auto_scaling_disk_gb_enabled", clusterName, err))
}
Expand Down Expand Up @@ -458,35 +434,3 @@ func dataSourceMongoDBAtlasClusterRead(ctx context.Context, d *schema.ResourceDa

return nil
}

func datasourceClusterPrivateEndpointRefreshFunc(name, projectID string, client *matlas.Client) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
cluster, resp, err := client.Clusters.Get(context.Background(), projectID, name)

if err != nil && cluster == nil && resp == nil {
return nil, "", err
} else if err != nil {
if resp.StatusCode == 404 {
return "", "DELETED", nil
}
if resp.StatusCode == 503 {
return "", "PENDING", nil
}
return nil, "", err
}

if cluster.ConnectionStrings != nil {
if cluster.ConnectionStrings.PrivateEndpoint == nil {
return cluster, "PRIVATE_ENDPOINTS_NIL", nil
}
if cluster.ConnectionStrings.PrivateEndpoint != nil && len(cluster.ConnectionStrings.PrivateEndpoint) == 0 {
return cluster, "PRIVATE_ENDPOINTS_EMPTY", nil
}
if cluster.ConnectionStrings.PrivateEndpoint != nil && len(cluster.ConnectionStrings.PrivateEndpoint) != 0 {
return cluster, "PRIVATE_ENDPOINTS_EXISTS", nil
}
}

return cluster, "NORMAL", nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func resourceMongoDBAtlasPrivateEndpointServiceLinkCreate(ctx context.Context, d
Refresh: resourceServiceEndpointRefreshFunc(ctx, conn, projectID, providerName, privateLinkID, endpointServiceID),
Timeout: 1 * time.Hour,
MinTimeout: 5 * time.Second,
Delay: 3 * time.Second,
Delay: 5 * time.Minute,
}
// Wait, catching any errors
_, err = stateConf.WaitForStateContext(ctx)
Expand Down

0 comments on commit 6995d9c

Please sign in to comment.