Skip to content

Fix #68: Added the ability to re-create the whitelist entry when it's remove manually #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions mongodbatlas/resource_mongodbatlas_project_ip_whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"
"net"
"strings"
"time"
Expand All @@ -28,7 +29,7 @@ func resourceMongoDBAtlasProjectIPWhitelist() *schema.Resource {
Read: resourceMongoDBAtlasProjectIPWhitelistRead,
Delete: resourceMongoDBAtlasProjectIPWhitelistDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceMongoDBAtlasIPWhitelistImportState,
},
Schema: map[string]*schema.Schema{
"project_id": {
Expand Down Expand Up @@ -173,10 +174,15 @@ func resourceMongoDBAtlasProjectIPWhitelistRead(d *schema.ResourceData, meta int

whitelist, _, err := conn.ProjectIPWhitelist.Get(context.Background(), ids["project_id"], ids["entry"])
if err != nil {
if strings.Contains(fmt.Sprint(err), "500") || strings.Contains(fmt.Sprint(err), "404") {
switch {
case strings.Contains(fmt.Sprint(err), "500"):
return resource.RetryableError(err)
case strings.Contains(fmt.Sprint(err), "404"):
d.SetId("")
return nil
default:
return resource.NonRetryableError(fmt.Errorf(errorWhitelistRead, err))
}
return resource.NonRetryableError(fmt.Errorf(errorWhitelistRead, err))
}

if whitelist != nil {
Expand Down Expand Up @@ -235,3 +241,31 @@ func resourceMongoDBAtlasProjectIPWhitelistDelete(d *schema.ResourceData, meta i
return nil
})
}

func resourceMongoDBAtlasIPWhitelistImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
conn := meta.(*matlas.Client)

parts := strings.SplitN(d.Id(), "-", 2)
if len(parts) != 2 {
return nil, errors.New("import format error: to import a peer, use the format {project_id}-{whitelist_entry}")
}

projectID := parts[0]
entry := parts[1]

_, _, err := conn.ProjectIPWhitelist.Get(context.Background(), projectID, entry)
if err != nil {
return nil, fmt.Errorf("couldn't import entry whitelist %s in project %s, error: %s", entry, projectID, err)
}

if err := d.Set("project_id", projectID); err != nil {
log.Printf("[WARN] Error setting project_id for (%s): %s", projectID, err)
}

d.SetId(encodeStateID(map[string]string{
"project_id": projectID,
"entry": entry,
}))

return []*schema.ResourceData{d}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,10 @@ func TestAccResourceMongoDBAtlasProjectIPWhitelist_importBasic(t *testing.T) {
Config: testAccMongoDBAtlasProjectIPWhitelistConfigSettingIPAddress(projectID, ipAddress, comment),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccCheckMongoDBAtlasProjectIPWhitelistImportStateIDFunc(resourceName),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"project_id"},
ResourceName: resourceName,
ImportStateIdFunc: testAccCheckMongoDBAtlasProjectIPWhitelistImportStateIDFunc(resourceName),
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down Expand Up @@ -264,7 +263,10 @@ func testAccCheckMongoDBAtlasProjectIPWhitelistImportStateIDFunc(resourceName st
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}
return rs.Primary.Attributes["project_id"], nil

ids := decodeStateID(rs.Primary.ID)

return fmt.Sprintf("%s-%s", ids["project_id"], ids["entry"]), nil
}
}

Expand Down