Skip to content

Commit

Permalink
fix TestAccV2NutanixDirectoryServicesResource_CreateACTIVE_DIRECTORYA…
Browse files Browse the repository at this point in the history
…lreadyExists : expected an error but got none
  • Loading branch information
Haroon-Dweikat-Ntx committed Dec 12, 2024
1 parent f0b3268 commit c4db9e4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
29 changes: 29 additions & 0 deletions nutanix/services/iamv2/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iamv2_test

import (
"fmt"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
"strconv"
"strings"

Expand Down Expand Up @@ -59,3 +60,31 @@ func testAccCheckNutanixUserDestroy(s *terraform.State) error {
}
return nil
}

func testAccCheckNutanixDirectoryServicesV2Destroy(s *terraform.State) error {

conn := acc.TestAccProvider.Meta().(*conns.Client)

for _, rs := range s.RootModule().Resources {
if rs.Type != "nutanix_volume_group_v2" {
continue
}

readResp, errRead := conn.IamAPI.DirectoryServiceAPIInstance.GetDirectoryServiceById(utils.StringPtr(rs.Primary.ID))
if errRead != nil {
return errRead
}
// get etag value from read response to pass in update request If-Match header, Required for update request
etagValue := conn.IamAPI.DirectoryServiceAPIInstance.ApiClient.GetEtag(readResp)
headers := make(map[string]interface{})
headers["If-Match"] = utils.StringPtr(etagValue)

if _, err := conn.IamAPI.DirectoryServiceAPIInstance.DeleteDirectoryServiceById(utils.StringPtr(rs.Primary.ID), headers); err != nil {
if strings.Contains(fmt.Sprint(err), "Directory service not found") {
return nil
}
return err
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ const resourceNameDirectoryServices = "nutanix_directory_services_v2.test"

func TestAccV2NutanixDirectoryServicesResource_CreateACTIVE_DIRECTORYService(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccFoundationPreCheck(t) },
Providers: acc.TestAccProviders,
PreCheck: func() { acc.TestAccFoundationPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckNutanixDirectoryServicesV2Destroy,
Steps: []resource.TestStep{
{
Config: testDirectoryServicesResourceConfig(filepath),
Config: testDirectoryServicesResourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceNameDirectoryServices, "ext_id"),
resource.TestCheckResourceAttr(resourceNameDirectoryServices, "name", testVars.Iam.DirectoryServices.Name),
Expand All @@ -31,7 +32,7 @@ func TestAccV2NutanixDirectoryServicesResource_CreateACTIVE_DIRECTORYService(t *
),
},
{
Config: testDirectoryServicesUpdateResourceConfig(filepath),
Config: testDirectoryServicesUpdateResourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceNameDirectoryServices, "ext_id"),
resource.TestCheckResourceAttr(resourceNameDirectoryServices, "name", testVars.Iam.DirectoryServices.Name),
Expand All @@ -53,11 +54,12 @@ func TestAccV2NutanixDirectoryServicesResource_CreateOpenLDAPService(t *testing.
name := fmt.Sprintf("tf-test-openldap-%d", acctest.RandInt())

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccFoundationPreCheck(t) },
Providers: acc.TestAccProviders,
PreCheck: func() { acc.TestAccFoundationPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckNutanixDirectoryServicesV2Destroy,
Steps: []resource.TestStep{
{
Config: testDirectoryOpenLDAPServicesResourceConfig(name, filepath),
Config: testDirectoryOpenLDAPServicesResourceConfig(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceNameDirectoryServices, "ext_id"),
resource.TestCheckResourceAttr(resourceNameDirectoryServices, "name", name),
Expand All @@ -74,11 +76,12 @@ func TestAccV2NutanixDirectoryServicesResource_CreateOpenLDAPService(t *testing.

func TestAccV2NutanixDirectoryServicesResource_CreateACTIVE_DIRECTORYAlreadyExists(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccFoundationPreCheck(t) },
Providers: acc.TestAccProviders,
PreCheck: func() { acc.TestAccFoundationPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckNutanixDirectoryServicesV2Destroy,
Steps: []resource.TestStep{
{
Config: testDirectoryServicesResourceConfig(filepath),
Config: testDirectoryServicesResourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceNameDirectoryServices, "ext_id"),
resource.TestCheckResourceAttr(resourceNameDirectoryServices, "name", testVars.Iam.DirectoryServices.Name),
Expand All @@ -90,7 +93,7 @@ func TestAccV2NutanixDirectoryServicesResource_CreateACTIVE_DIRECTORYAlreadyExis
),
},
{
Config: testDirectoryServicesDuplicatedResourceConfig(filepath),
Config: testDirectoryServicesResourceConfig() + testDirectoryServicesDuplicatedResourceConfig(),
ExpectError: regexp.MustCompile("Failed to create directory service as directory service with name " + testVars.Iam.DirectoryServices.Name + " already exists"),
},
},
Expand All @@ -103,7 +106,7 @@ func TestAccV2NutanixDirectoryServicesResource_WithNoName(t *testing.T) {
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testDirectoryServicesResourceWithoutNameConfig(filepath),
Config: testDirectoryServicesResourceWithoutNameConfig(),
ExpectError: regexp.MustCompile("Missing required argument"),
},
},
Expand All @@ -116,7 +119,7 @@ func TestAccV2NutanixDirectoryServicesResource_WithNoUrl(t *testing.T) {
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testDirectoryServicesResourceWithoutURLConfig(filepath),
Config: testDirectoryServicesResourceWithoutURLConfig(),
ExpectError: regexp.MustCompile("Missing required argument"),
},
},
Expand All @@ -129,7 +132,7 @@ func TestAccV2NutanixDirectoryServicesResource_WithNoDomainName(t *testing.T) {
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testDirectoryServicesResourceWithoutDomainNameConfig(filepath),
Config: testDirectoryServicesResourceWithoutDomainNameConfig(),
ExpectError: regexp.MustCompile("Missing required argument"),
},
},
Expand All @@ -142,7 +145,7 @@ func TestAccV2NutanixDirectoryServicesResource_WithNoDirectoryType(t *testing.T)
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testDirectoryServicesResourceWithoutDirectoryTypeConfig(filepath),
Config: testDirectoryServicesResourceWithoutDirectoryTypeConfig(),
ExpectError: regexp.MustCompile("Missing required argument"),
},
},
Expand All @@ -155,14 +158,14 @@ func TestAccV2NutanixDirectoryServicesResource_WithNoServiceAccount(t *testing.T
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testDirectoryServicesResourceWithoutServiceAccountConfig(filepath),
Config: testDirectoryServicesResourceWithoutServiceAccountConfig(),
ExpectError: regexp.MustCompile("Insufficient service_account blocks"),
},
},
})
}

func testDirectoryServicesResourceConfig(filepath string) string {
func testDirectoryServicesResourceConfig() string {
return fmt.Sprintf(`
locals{
Expand All @@ -188,7 +191,7 @@ func testDirectoryServicesResourceConfig(filepath string) string {
}`, filepath)
}

func testDirectoryServicesUpdateResourceConfig(filepath string) string {
func testDirectoryServicesUpdateResourceConfig() string {
return fmt.Sprintf(`
locals{
Expand All @@ -214,7 +217,7 @@ func testDirectoryServicesUpdateResourceConfig(filepath string) string {
}`, filepath)
}

func testDirectoryOpenLDAPServicesResourceConfig(name, filepath string) string {
func testDirectoryOpenLDAPServicesResourceConfig(name string) string {
return fmt.Sprintf(`
locals{
Expand Down Expand Up @@ -252,14 +255,8 @@ func testDirectoryOpenLDAPServicesResourceConfig(name, filepath string) string {
}`, name, filepath)
}

func testDirectoryServicesDuplicatedResourceConfig(filepath string) string {
return fmt.Sprintf(`
locals{
config = (jsondecode(file("%s")))
directory_services = local.config.iam.directory_services
}
func testDirectoryServicesDuplicatedResourceConfig() string {
return `
resource "nutanix_directory_services_v2" "test_1" {
name = local.directory_services.name
url = local.directory_services.url
Expand All @@ -275,10 +272,10 @@ func testDirectoryServicesDuplicatedResourceConfig(filepath string) string {
service_account.0.password,
]
}
}`, filepath)
}`
}

func testDirectoryServicesResourceWithoutNameConfig(filepath string) string {
func testDirectoryServicesResourceWithoutNameConfig() string {
return fmt.Sprintf(`
locals{
Expand All @@ -302,7 +299,7 @@ func testDirectoryServicesResourceWithoutNameConfig(filepath string) string {
}`, filepath)
}

func testDirectoryServicesResourceWithoutURLConfig(filepath string) string {
func testDirectoryServicesResourceWithoutURLConfig() string {
return fmt.Sprintf(`
locals{
Expand All @@ -326,7 +323,7 @@ func testDirectoryServicesResourceWithoutURLConfig(filepath string) string {
}`, filepath)
}

func testDirectoryServicesResourceWithoutDomainNameConfig(filepath string) string {
func testDirectoryServicesResourceWithoutDomainNameConfig() string {
return fmt.Sprintf(`
locals{
Expand All @@ -350,7 +347,7 @@ func testDirectoryServicesResourceWithoutDomainNameConfig(filepath string) strin
}`, filepath)
}

func testDirectoryServicesResourceWithoutDirectoryTypeConfig(filepath string) string {
func testDirectoryServicesResourceWithoutDirectoryTypeConfig() string {
return fmt.Sprintf(`
locals{
Expand All @@ -374,7 +371,7 @@ func testDirectoryServicesResourceWithoutDirectoryTypeConfig(filepath string) st
}`, filepath)
}

func testDirectoryServicesResourceWithoutServiceAccountConfig(filepath string) string {
func testDirectoryServicesResourceWithoutServiceAccountConfig() string {
return fmt.Sprintf(`
locals{
Expand Down

0 comments on commit c4db9e4

Please sign in to comment.