Skip to content

Commit

Permalink
INTMDB-198: Fixes a bug where it appears empty private endpoint in cl…
Browse files Browse the repository at this point in the history
…uster (#481)

* refactor: added refresh for waiting private endoping if providername is aws

* changed err to nil

* fixes linter

* added validation for azure

Co-authored-by: Edgar López <[email protected]>
  • Loading branch information
coderGo93 and Edgar López authored Jun 28, 2021
1 parent 1e7553e commit a5756de
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package mongodbatlas
import (
"context"
"fmt"
"log"
"net/http"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/spf13/cast"

matlas "go.mongodb.org/atlas/mongodbatlas"
)
Expand Down Expand Up @@ -334,6 +338,25 @@ func dataSourceMongoDBAtlasClusterRead(d *schema.ResourceData, meta interface{})
return 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.WaitForState()
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 fmt.Errorf(errorClusterSetting, "auto_scaling_disk_gb_enabled", clusterName, err)
}
Expand Down Expand Up @@ -443,3 +466,35 @@ func dataSourceMongoDBAtlasClusterRead(d *schema.ResourceData, meta interface{})

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
}
}

0 comments on commit a5756de

Please sign in to comment.