Skip to content
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

Service groups and address groups #341

Merged
merged 9 commits into from
Feb 14, 2022
Prev Previous commit
Next Next commit
Adding service group and Address group testcases
  • Loading branch information
abhimutant authored and siddharth-nutanix committed Feb 14, 2022
commit b110e7bbb1fc1a9530bcb72796dc4a51646caac8
45 changes: 45 additions & 0 deletions nutanix/data_source_nutanix_address_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package nutanix

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccNutanixAddressGroupDataSource_basic(t *testing.T) {
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAddressGroupDataSourceConfig(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.nutanix_address_group.addr_group", "ip_address_block_list.#", "1"),
resource.TestCheckResourceAttr("data.nutanix_address_group.addr_group", "ip_address_block_list.0.prefix_length", "24"),
resource.TestCheckResourceAttr("data.nutanix_address_group.addr_group", "description", "test address group resource"),
),
},
},
})
}

func testAccAddressGroupDataSourceConfig(r int) string {
return fmt.Sprintf(`
resource "nutanix_address_group" "test_address" {
name = "test-%[1]d"
description = "test address group resource"

ip_address_block_list {
ip = "10.0.0.0"
prefix_length = 24
}
}

data "nutanix_address_group" "addr_group" {
uuid = "${nutanix_address_group.test_address.id}"
}
`, r)
}
49 changes: 49 additions & 0 deletions nutanix/data_source_nutanix_address_groups_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package nutanix

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccNutanixAddressGroupsDataSource_basic(t *testing.T) {
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAddressGroupsDataSourceConfig(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.nutanix_address_group.addr_group", "ip_address_block_list.#", "1"),
resource.TestCheckResourceAttr("data.nutanix_address_group.addr_group", "description", "test address groups resource"),
resource.TestCheckResourceAttr("data.nutanix_address_groups.addr_groups", "entities.#", "1"),
resource.TestCheckResourceAttr("data.nutanix_address_groups.addr_groups", "entities.0.address_group.#", "1"),
),
},
},
})
}

func testAccAddressGroupsDataSourceConfig(r int) string {
return fmt.Sprintf(`
resource "nutanix_address_group" "test_address" {
name = "test-%[1]d"
description = "test address groups resource"

ip_address_block_list {
ip = "10.0.0.0"
prefix_length = 24
}
}

data "nutanix_address_group" "addr_group" {
uuid = nutanix_address_group.test_address.id
}

data "nutanix_address_groups" "addr_groups" {}
`, r)
}
76 changes: 76 additions & 0 deletions nutanix/resource_nutanix_address_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package nutanix

import (
"fmt"
"strings"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

const resourcesAddressGroup = "nutanix_address_group.test"

func TestAccNutanixAddressGroup(t *testing.T) {
name := acctest.RandomWithPrefix("nutanix_address_gr")
description := "this is nutanix address group"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNutanixAddressGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixAddressGroupConfig(name, description),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourcesAddressGroup, "name", name),
resource.TestCheckResourceAttr(resourcesAddressGroup, "description", description),
resource.TestCheckResourceAttr(resourcesAddressGroup, "ip_address_block_list.#", "1"),
resource.TestCheckResourceAttr(resourcesAddressGroup, "ip_address_block_list.0.prefix_length", "24"),
),
},
{
ResourceName: resourcesAddressGroup,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckNutanixAddressGroupDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*Client)

for _, rs := range s.RootModule().Resources {
if rs.Type != "nutanix_address_grp" {
continue
}
for {
_, err := conn.API.V3.GetVM(rs.Primary.ID)
if err != nil {
if strings.Contains(fmt.Sprint(err), "ENTITY_NOT_FOUND") {
return nil
}
return err
}
time.Sleep(3000 * time.Millisecond)
}
}

return nil
}

func testAccNutanixAddressGroupConfig(name, description string) string {
return fmt.Sprintf(`
resource "nutanix_address_group" "test" {
name = "%[1]s"
description = "%[2]s"
ip_address_block_list {
ip = "10.0.0.0"
prefix_length = 24
}
}
`, name, description)
}
84 changes: 84 additions & 0 deletions nutanix/resource_nutanix_service_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package nutanix

import (
"fmt"
"strings"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

const resourceServiceGroup = "nutanix_service_group.test"

func TestAccNutanixServiceGroup(t *testing.T) {
name := acctest.RandomWithPrefix("nutanix_service_gr")
description := "this is nutanix service group"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNutanixServiceGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixServiceGroupConfig(name, description),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceServiceGroup, "name", name),
resource.TestCheckResourceAttr(resourceServiceGroup, "description", description),
resource.TestCheckResourceAttr(resourceServiceGroup, "service_list.#", "1"),
resource.TestCheckResourceAttr(resourceServiceGroup, "service_list.0.protocol", "TCP"),
),
},
{
ResourceName: resourceServiceGroup,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckNutanixServiceGroupDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*Client)

for _, rs := range s.RootModule().Resources {
if rs.Type != "nutanix_service_grp" {
continue
}
for {
_, err := conn.API.V3.GetVM(rs.Primary.ID)
if err != nil {
if strings.Contains(fmt.Sprint(err), "ENTITY_NOT_FOUND") {
return nil
}
return err
}
time.Sleep(3000 * time.Millisecond)
}
}

return nil
}

func testAccNutanixServiceGroupConfig(name, description string) string {
return fmt.Sprintf(`
resource "nutanix_service_group" "test" {
name = "%[1]s"
description = "%[2]s"

service_list {
protocol = "TCP"
tcp_port_range_list {
start_port = 22
end_port = 22
}
tcp_port_range_list {
start_port = 2222
end_port = 2222
}
}
}
`, name, description)
}