Skip to content

Commit

Permalink
fix: add refresh func to avoid issues
Browse files Browse the repository at this point in the history
  • Loading branch information
PacoDw committed Sep 24, 2019
1 parent 0a11fd9 commit 491d914
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 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: 3 * time.Hour,
MinTimeout: 60 * time.Second,
Delay: 5 * time.Minute,
}

// Wait, catching any errors
_, err := stateConf.WaitForState()
if err != nil {
return fmt.Errorf(errorContainerDelete, containerID, err)
return fmt.Errorf("error %s", err)
}

return nil
}

Expand Down Expand Up @@ -267,3 +277,26 @@ 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, _, err := client.Containers.Get(context.Background(), projectID, containerID)
if *container.Provisioned && err == nil {
return nil, "provisioned_container", nil
} else if err != nil {
return nil, "", err
}

_, err = client.Containers.Delete(context.Background(), projectID, containerID)
if err != nil {
return nil, "provisioned_container", nil
}

return 42, "deleted", nil
}
}

0 comments on commit 491d914

Please sign in to comment.