diff --git a/nutanix/data_source_nutanix_address_group.go b/nutanix/data_source_nutanix_address_group.go index b0e1adcaf..0ed8cf97d 100644 --- a/nutanix/data_source_nutanix_address_group.go +++ b/nutanix/data_source_nutanix_address_group.go @@ -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 { @@ -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": { @@ -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 { diff --git a/nutanix/data_source_nutanix_address_group_test.go b/nutanix/data_source_nutanix_address_group_test.go new file mode 100644 index 000000000..12c9a0acd --- /dev/null +++ b/nutanix/data_source_nutanix_address_group_test.go @@ -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) +} diff --git a/nutanix/data_source_nutanix_address_groups.go b/nutanix/data_source_nutanix_address_groups.go index 7bad40f2a..e45f76aea 100644 --- a/nutanix/data_source_nutanix_address_groups.go +++ b/nutanix/data_source_nutanix_address_groups.go @@ -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": { @@ -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{ @@ -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 } diff --git a/nutanix/data_source_nutanix_address_groups_test.go b/nutanix/data_source_nutanix_address_groups_test.go new file mode 100644 index 000000000..940e2e970 --- /dev/null +++ b/nutanix/data_source_nutanix_address_groups_test.go @@ -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) +} diff --git a/nutanix/resource_nutanix_address_group_test.go b/nutanix/resource_nutanix_address_group_test.go new file mode 100644 index 000000000..bbc33b77f --- /dev/null +++ b/nutanix/resource_nutanix_address_group_test.go @@ -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) +} diff --git a/nutanix/resource_nutanix_service_group_test.go b/nutanix/resource_nutanix_service_group_test.go new file mode 100644 index 000000000..ef17d203a --- /dev/null +++ b/nutanix/resource_nutanix_service_group_test.go @@ -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) +}