diff --git a/mongodbatlas/resource_mongodbatlas_cluster_test.go b/mongodbatlas/resource_mongodbatlas_cluster_test.go index 922673841d..8f6c982a8c 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster_test.go +++ b/mongodbatlas/resource_mongodbatlas_cluster_test.go @@ -307,7 +307,7 @@ func testAccMongoDBAtlasClusterConfigAWS(projectID, name, backupEnabled string) resource "mongodbatlas_cluster" "test" { project_id = "%s" name = "%s" - disk_size_gb = 40 + disk_size_gb = 100 num_shards = 1 replication_factor = 3 diff --git a/mongodbatlas/resource_mongodbatlas_network_container.go b/mongodbatlas/resource_mongodbatlas_network_container.go index 1c6d6905a1..37c92ca716 100644 --- a/mongodbatlas/resource_mongodbatlas_network_container.go +++ b/mongodbatlas/resource_mongodbatlas_network_container.go @@ -8,7 +8,9 @@ import ( "net/http" "reflect" "strings" + "time" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" matlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" @@ -221,14 +223,22 @@ func resourceMongoDBAtlasNetworkContainerUpdate(d *schema.ResourceData, meta int func resourceMongoDBAtlasNetworkContainerDelete(d *schema.ResourceData, meta interface{}) error { //Get client connection. conn := meta.(*matlas.Client) - ids := decodeStateID(d.Id()) - projectID := ids["project_id"] - containerID := ids["container_id"] - _, err := conn.Containers.Delete(context.Background(), projectID, containerID) + stateConf := &resource.StateChangeConf{ + Pending: []string{"provisioned_container"}, + Target: []string{"deleted"}, + Refresh: resourceNetworkContainerRefreshFunc(d, conn), + Timeout: 1 * time.Hour, + MinTimeout: 10 * time.Second, + Delay: 2 * time.Minute, + } + + // Wait, catching any errors + _, err := stateConf.WaitForState() if err != nil { - return fmt.Errorf(errorContainerDelete, containerID, err) + return fmt.Errorf(errorContainerDelete, decodeStateID(d.Id())["container_id"], err) } + return nil } @@ -267,3 +277,30 @@ func resourceMongoDBAtlasNetworkContainerImportState(d *schema.ResourceData, met } return []*schema.ResourceData{d}, nil } + +func resourceNetworkContainerRefreshFunc(d *schema.ResourceData, client *matlas.Client) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + ids := decodeStateID(d.Id()) + projectID := ids["project_id"] + containerID := ids["container_id"] + + var err error + container, res, err := client.Containers.Get(context.Background(), projectID, containerID) + if err != nil { + if res.StatusCode == 404 { + return 42, "deleted", nil + } + return nil, "", err + } + if *container.Provisioned && err == nil { + return nil, "provisioned_container", nil + } + + _, err = client.Containers.Delete(context.Background(), projectID, containerID) + if err != nil { + return nil, "provisioned_container", nil + } + + return 42, "deleted", nil + } +}