Skip to content

Commit

Permalink
Merge pull request #38 from terraform-providers/networkContainer
Browse files Browse the repository at this point in the history
Fix: Network container
  • Loading branch information
marinsalinas authored Oct 1, 2019
2 parents 4c873b8 + 0a39f4c commit e651f1c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion mongodbatlas/resource_mongodbatlas_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 42 additions & 5 deletions mongodbatlas/resource_mongodbatlas_network_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}

0 comments on commit e651f1c

Please sign in to comment.