Skip to content

Commit

Permalink
INTMDB-218: fixes the bug when you try to add more than 100 ip whitel…
Browse files Browse the repository at this point in the history
…ist (#514)

* fix: fixes the bug when you try to add more than 100 ip whitelist

* fixes linter

* added constant number

* refactor

* renamed file

Co-authored-by: Edgar Lopez <edgarlopez@pop-os.localdomain>
  • Loading branch information
coderGo93 and Edgar Lopez committed Aug 17, 2021
1 parent 1cb0755 commit 3e9b293
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 27 deletions.
76 changes: 66 additions & 10 deletions mongodbatlas/resource_mongodbatlas_project_ip_access_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,21 @@ func resourceMongoDBAtlasProjectIPAccessListCreate(ctx context.Context, d *schem
return nil, "failed", fmt.Errorf(errorAccessListCreate, err)
}

if accessList.TotalCount > 0 {
accessListEntry := ipAddress
if cidrBlock != "" {
accessListEntry = cidrBlock
}
accessListEntry := ipAddress
if len(cidrBlock) > 0 {
accessListEntry = cidrBlock
}

for _, entry := range accessList.Results {
if entry.IPAddress == accessListEntry || entry.CIDRBlock == accessListEntry {
return accessList, "created", nil
}
exists, err := isEntryInProjectAccessList(ctx, conn, projectID, accessListEntry)
if err != nil {
if strings.Contains(fmt.Sprint(err), "Unexpected error") ||
strings.Contains(fmt.Sprint(err), "UNEXPECTED_ERROR") ||
strings.Contains(fmt.Sprint(err), "500") {
return nil, "pending", nil
}
return nil, "failed", fmt.Errorf(errorAccessListCreate, err)
}
if !exists {
return nil, "pending", nil
}

Expand Down Expand Up @@ -236,7 +240,8 @@ func resourceMongoDBAtlasProjectIPAccessListDelete(ctx context.Context, d *schem
entry, _, err := conn.ProjectIPAccessList.Get(ctx, ids["project_id"], ids["entry"])
if err != nil {
if strings.Contains(fmt.Sprint(err), "404") ||
strings.Contains(fmt.Sprint(err), "ATLAS_ACCESS_LIST_NOT_FOUND") {
strings.Contains(fmt.Sprint(err), "ATLAS_ACCESS_LIST_NOT_FOUND") ||
strings.Contains(fmt.Sprint(err), "ATLAS_NETWORK_PERMISSION_ENTRY_NOT_FOUND") {
return nil
}

Expand Down Expand Up @@ -278,3 +283,54 @@ func resourceMongoDBAtlasIPAccessListImportState(ctx context.Context, d *schema.

return []*schema.ResourceData{d}, nil
}

func isEntryInProjectAccessList(ctx context.Context, conn *matlas.Client, projectID, entry string) (bool, error) {
currentPage := 1
exists := false
err := resource.RetryContext(ctx, 2*time.Minute, func() *resource.RetryError {
accessList, resp, err := conn.ProjectIPAccessList.List(ctx, projectID, &matlas.ListOptions{PageNum: currentPage})
if err != nil {
switch {
case strings.Contains(fmt.Sprint(err), "500"):
return resource.RetryableError(err)
case strings.Contains(fmt.Sprint(err), "404"):
return resource.RetryableError(err)
default:
return resource.NonRetryableError(fmt.Errorf(errorAccessListRead, err))
}
}

if accessList.TotalCount > 0 {
for _, result := range accessList.Results {
if result.IPAddress == entry || result.CIDRBlock == entry {
exists = true
break
}
}
}

if !exists {
currentPage, err = resp.CurrentPage()
if err != nil {
return resource.NonRetryableError(fmt.Errorf(errorAccessListRead, err))
}

if !resp.IsLastPage() {
currentPage++
return resource.RetryableError(fmt.Errorf("[DEBUG] Current page : %d Next page: %d, will retry again", currentPage-1, currentPage))
}
}

return nil
})
if err != nil {
if strings.Contains(fmt.Sprint(err), "Unexpected error") ||
strings.Contains(fmt.Sprint(err), "UNEXPECTED_ERROR") ||
strings.Contains(fmt.Sprint(err), "500") {
return exists, nil
}
return exists, err
}

return exists, nil
}
40 changes: 23 additions & 17 deletions mongodbatlas/resource_mongodbatlas_project_ip_access_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ func TestAccResourceMongoDBAtlasProjectIPAccessList_SettingAWSSecurityGroup(t *t

func TestAccResourceMongoDBAtlasProjectIPAccessList_SettingMultiple(t *testing.T) {
resourceName := "mongodbatlas_project_ip_access_list.test_%d"
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")

orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName := acctest.RandomWithPrefix("test-acc")
const ipWhiteListCount = 200
accessList := make([]map[string]string, 0)

for i := 0; i < 20; i++ {
for i := 0; i < ipWhiteListCount; i++ {
entry := make(map[string]string)
entryName := ""
ipAddr := ""
Expand All @@ -174,22 +175,23 @@ func TestAccResourceMongoDBAtlasProjectIPAccessList_SettingMultiple(t *testing.T

accessList = append(accessList, entry)
}

//TODO: make testAccCheckMongoDBAtlasProjectIPAccessListExists dynamic
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckMongoDBAtlasProjectIPAccessListDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasProjectIPAccessListConfigSettingMultiple(projectID, accessList, false),
Config: testAccMongoDBAtlasProjectIPAccessListConfigSettingMultiple(projectName, orgID, accessList, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasProjectIPAccessListExists(fmt.Sprintf(resourceName, 0)),
testAccCheckMongoDBAtlasProjectIPAccessListExists(fmt.Sprintf(resourceName, 1)),
testAccCheckMongoDBAtlasProjectIPAccessListExists(fmt.Sprintf(resourceName, 2)),
),
},
{
Config: testAccMongoDBAtlasProjectIPAccessListConfigSettingMultiple(projectID, accessList, true),
Config: testAccMongoDBAtlasProjectIPAccessListConfigSettingMultiple(projectName, orgID, accessList, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasProjectIPAccessListExists(fmt.Sprintf(resourceName, 0)),
testAccCheckMongoDBAtlasProjectIPAccessListExists(fmt.Sprintf(resourceName, 1)),
Expand Down Expand Up @@ -329,8 +331,12 @@ func testAccMongoDBAtlasProjectIPAccessListConfigSettingAWSSecurityGroup(project
`, projectID, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegion, awsSGroup, comment)
}

func testAccMongoDBAtlasProjectIPAccessListConfigSettingMultiple(projectID string, accessList []map[string]string, isUpdate bool) string {
config := ""
func testAccMongoDBAtlasProjectIPAccessListConfigSettingMultiple(projectName, orgID string, accessList []map[string]string, isUpdate bool) string {
config := fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = %[1]q
org_id = %[2]q
}`, projectName, orgID)

for i, entry := range accessList {
comment := entry["comment"]
Expand All @@ -341,20 +347,20 @@ func testAccMongoDBAtlasProjectIPAccessListConfigSettingMultiple(projectID strin

if cidr, ok := entry["cidr_block"]; ok {
config += fmt.Sprintf(`
resource "mongodbatlas_project_ip_access_list" "test_%d" {
project_id = "%s"
cidr_block = "%s"
comment = "%s"
resource "mongodbatlas_project_ip_access_list" "test_%[1]d" {
project_id = mongodbatlas_project.test.id
cidr_block = %[2]q
comment = %[3]q
}
`, i, projectID, cidr, comment)
`, i, cidr, comment)
} else {
config += fmt.Sprintf(`
resource "mongodbatlas_project_ip_access_list" "test_%d" {
project_id = "%s"
ip_address = "%s"
comment = "%s"
resource "mongodbatlas_project_ip_access_list" "test_%[1]d" {
project_id = mongodbatlas_project.test.id
ip_address = %[2]q
comment = %[3]q
}
`, i, projectID, entry["ip_address"], comment)
`, i, entry["ip_address"], comment)
}
}
return config
Expand Down

0 comments on commit 3e9b293

Please sign in to comment.