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 group fix #340

Merged
merged 3 commits into from
Feb 11, 2022
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
17 changes: 9 additions & 8 deletions nutanix/data_source_nutanix_address_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)

func dataSourceNutanixAddressGroup() *schema.Resource {
Expand All @@ -21,11 +22,11 @@ func dataSourceNutanixAddressGroup() *schema.Resource {
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"ip_address_block_list": {
Type: schema.TypeList,
Required: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Expand Down Expand Up @@ -60,16 +61,16 @@ func dataSourceNutanixAddressGroupRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("error reading user with error %s", reqErr)
}

if name, nameOk := d.GetOk("name"); nameOk {
d.Set("name", name.(string))
if err := d.Set("name", utils.StringValue(group.AddressGroup.Name)); err != nil {
return err
}

if desc, descOk := d.GetOk("description"); descOk {
d.Set("description", desc.(string))
if err := d.Set("description", utils.StringValue(group.AddressGroup.Description)); err != nil {
return err
}

if str, strOk := d.GetOk("address_group_string"); strOk {
d.Set("address_group_string", str.(string))
if err := d.Set("address_group_string", utils.StringValue(group.AddressGroup.AddressGroupString)); err != nil {
return err
}

if err := d.Set("ip_address_block_list", flattenAddressEntry(group.AddressGroup.BlockList)); err != nil {
Expand Down
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)
}
18 changes: 10 additions & 8 deletions nutanix/data_source_nutanix_address_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ func dataSourceNutanixAddressGroups() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address_group": {
Type: schema.TypeMap,
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -93,7 +94,7 @@ func dataSourceNutanixAddressGroups() *schema.Resource {
},
},
"associated_policies_list": {
Type: schema.TypeMap,
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -147,15 +148,16 @@ func flattenAddressGroup(entries []*v3.AddressGroupListEntry) interface{} {

for i, entry := range entries {
entities[i] = map[string]interface{}{
"address_group": map[string]interface{}{
"name": entry.AddressGroup.Name,
"description": entry.AddressGroup.Description,
"address_group_string": entry.AddressGroup.AddressGroupString,
"ip_address_block_list": flattenAddressEntry(entry.AddressGroup.BlockList),
"address_group": []map[string]interface{}{
{
"name": entry.AddressGroup.Name,
"description": entry.AddressGroup.Description,
"address_group_string": entry.AddressGroup.AddressGroupString,
"ip_address_block_list": flattenAddressEntry(entry.AddressGroup.BlockList),
},
},
"associated_policies_list": flattenReferenceList(entry.AssociatedPoliciesList),
}
}

return entities
}
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)
}