From 9a85daf9bdcc994482d929b934c369f79c886459 Mon Sep 17 00:00:00 2001 From: Haroon-Dweikat-Ntx Date: Tue, 1 Oct 2024 12:32:59 +0300 Subject: [PATCH 1/2] fix ip_endpoint_list type , provide ASSIGNED as default type value with validation --- nutanix/resource_nutanix_virtual_machine.go | 15 +- .../resource_nutanix_virtual_machine_test.go | 182 ++++++++++++++++++ 2 files changed, 193 insertions(+), 4 deletions(-) diff --git a/nutanix/resource_nutanix_virtual_machine.go b/nutanix/resource_nutanix_virtual_machine.go index 6d5f6e382..1f8e4e47f 100644 --- a/nutanix/resource_nutanix_virtual_machine.go +++ b/nutanix/resource_nutanix_virtual_machine.go @@ -267,9 +267,10 @@ func resourceNutanixVirtualMachine() *schema.Resource { Computed: true, }, "type": { - Type: schema.TypeString, - Optional: true, - Computed: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + ValidateFunc: validation.StringInSlice([]string{"ASSIGNED"}, false), }, }, }, @@ -1664,9 +1665,15 @@ func expandIPAddressList(ipl []interface{}) []*v3.IPAddress { if ipset, ipsetok := v["ip"]; ipsetok { v3ip.IP = utils.StringPtr(ipset.(string)) } - if iptype, iptypeok := v["type"]; iptypeok { + // Check if 'type' is provided + if iptype, iptypeok := v["type"]; !iptypeok || iptype == "" { + // ip type not provided, set a default value to ASSIGNED + log.Printf("[DEBUG] IP type not provided, setting default value to ASSIGNED") + v3ip.Type = utils.StringPtr("ASSIGNED") + } else { v3ip.Type = utils.StringPtr(iptype.(string)) } + ip[k] = v3ip } return ip diff --git a/nutanix/resource_nutanix_virtual_machine_test.go b/nutanix/resource_nutanix_virtual_machine_test.go index 0d7ab9d6c..021153de1 100644 --- a/nutanix/resource_nutanix_virtual_machine_test.go +++ b/nutanix/resource_nutanix_virtual_machine_test.go @@ -204,6 +204,7 @@ func TestAccNutanixVirtualMachine_WithSubnet(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "num_vcpus_per_socket", "1"), resource.TestCheckResourceAttr(resourceName, "categories.#", "1"), resource.TestCheckResourceAttrSet(resourceName, "nic_list_status.0.ip_endpoint_list.0.ip"), + resource.TestCheckResourceAttr(resourceName, "nic_list_status.0.ip_endpoint_list.0.type", "ASSIGNED"), ), }, { @@ -618,6 +619,36 @@ func TestAccNutanixVirtualMachine_SecureBoot(t *testing.T) { }) } +func TestAccNutanixVirtualMachine_WithNicListIpType(t *testing.T) { + r := acctest.RandIntRange(101, 110) + resourceName := "nutanix_virtual_machine.vm3" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckNutanixVirtualMachineDestroy, + Steps: []resource.TestStep{ + { + Config: testaccnutanixvmconfigwithniclistIpType(r), + Check: resource.ComposeTestCheckFunc( + testAccCheckNutanixVirtualMachineExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "hardware_clock_timezone", "UTC"), + resource.TestCheckResourceAttr(resourceName, "power_state", "ON"), + resource.TestCheckResourceAttr(resourceName, "memory_size_mib", "186"), + resource.TestCheckResourceAttr(resourceName, "num_sockets", "1"), + resource.TestCheckResourceAttr(resourceName, "num_vcpus_per_socket", "1"), + resource.TestCheckResourceAttr(resourceName, "categories.#", "1"), + resource.TestCheckResourceAttrSet(resourceName, "nic_list_status.0.ip_endpoint_list.0.ip"), + resource.TestCheckResourceAttr(resourceName, "nic_list_status.0.ip_endpoint_list.0.type", "ASSIGNED"), + ), + }, + { + Config: testaccnutanixvmconfigwithniclistIpTypeWrongType(r), + ExpectError: regexp.MustCompile("Error: expected nic_list.0.ip_endpoint_list.0.type to be one of \\[ASSIGNED], got WRONG"), + }, + }, + }) +} + func TestAccNutanixVirtualMachine_SecureBootWithNoMachineType(t *testing.T) { r := acctest.RandInt() name := fmt.Sprintf("test-vm-%d", r) @@ -1602,3 +1633,154 @@ func testAccNutanixVMConfigWithSecureBootWithNoMachineType(name, desc, mem strin `, name, desc, mem) } + +func testaccnutanixvmconfigwithniclistIpType(r int) string { + return fmt.Sprintf(` + data "nutanix_clusters" "clusters" {} + + locals { + cluster1 = [ + for cluster in data.nutanix_clusters.clusters.entities : + cluster.metadata.uuid if cluster.service_list[0] != "PRISM_CENTRAL" + ][0] + } + + resource "nutanix_subnet" "sub" { + cluster_uuid = "${local.cluster1}" + + # General Information for subnet + name = "terraform-vm-with-subnet-%[1]d" + description = "Description of my unit test VLAN" + vlan_id = %[1]d + subnet_type = "VLAN" + + # Provision a Managed L3 Network + # This bit is only needed if you intend to turn on AHV's IPAM + subnet_ip = "10.250.140.0" + default_gateway_ip = "10.250.140.1" + prefix_length = 24 + dhcp_options = { + boot_file_name = "bootfile" + domain_name = "nutanix" + tftp_server_name = "10.250.140.200" + } + dhcp_domain_name_server_list = ["8.8.8.8", "4.2.2.2"] + dhcp_domain_search_list = ["terraform.nutanix.com", "terraform.unit.test.com"] + ip_config_pool_list_ranges = ["10.250.140.20 10.250.140.100"] + } + + resource "nutanix_image" "cirros-034-disk" { + name = "test-image-dou-%[1]d" + source_uri = "http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img" + description = "heres a tiny linux image, not an iso, but a real disk!" + } + + resource "nutanix_virtual_machine" "vm3" { + name = "test-dou-vm-%[1]d" + + categories { + name = "Environment" + value = "Staging" + } + + cluster_uuid = "${local.cluster1}" + num_vcpus_per_socket = 1 + num_sockets = 1 + memory_size_mib = 186 + + disk_list { + data_source_reference = { + kind = "image" + uuid = "${nutanix_image.cirros-034-disk.id}" + } + } + + nic_list { + subnet_uuid = "${nutanix_subnet.sub.id}" + ip_endpoint_list { + ip = "10.250.140.76" + type = "ASSIGNED" + } + } + } + + output "ip_address" { + value = "${lookup(nutanix_virtual_machine.vm3.nic_list_status.0.ip_endpoint_list[0], "ip")}" + } + `, r) +} +func testaccnutanixvmconfigwithniclistIpTypeWrongType(r int) string { + return fmt.Sprintf(` + data "nutanix_clusters" "clusters" {} + + locals { + cluster1 = [ + for cluster in data.nutanix_clusters.clusters.entities : + cluster.metadata.uuid if cluster.service_list[0] != "PRISM_CENTRAL" + ][0] + } + + resource "nutanix_subnet" "sub" { + cluster_uuid = "${local.cluster1}" + + # General Information for subnet + name = "terraform-vm-with-subnet-%[1]d" + description = "Description of my unit test VLAN" + vlan_id = %[1]d + subnet_type = "VLAN" + + # Provision a Managed L3 Network + # This bit is only needed if you intend to turn on AHV's IPAM + subnet_ip = "10.250.140.0" + default_gateway_ip = "10.250.140.1" + prefix_length = 24 + dhcp_options = { + boot_file_name = "bootfile" + domain_name = "nutanix" + tftp_server_name = "10.250.140.200" + } + dhcp_domain_name_server_list = ["8.8.8.8", "4.2.2.2"] + dhcp_domain_search_list = ["terraform.nutanix.com", "terraform.unit.test.com"] + ip_config_pool_list_ranges = ["10.250.140.20 10.250.140.100"] + } + + resource "nutanix_image" "cirros-034-disk" { + name = "test-image-dou-%[1]d" + source_uri = "http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img" + description = "heres a tiny linux image, not an iso, but a real disk!" + } + + resource "nutanix_virtual_machine" "vm3" { + name = "test-dou-vm-%[1]d" + + categories { + name = "Environment" + value = "Staging" + } + + cluster_uuid = "${local.cluster1}" + num_vcpus_per_socket = 1 + num_sockets = 1 + memory_size_mib = 186 + + disk_list { + data_source_reference = { + kind = "image" + uuid = "${nutanix_image.cirros-034-disk.id}" + } + } + + nic_list { + subnet_uuid = "${nutanix_subnet.sub.id}" + ip_endpoint_list { + ip = "10.250.140.76" + type = "WRONG" + } + } + } + + output "ip_address" { + value = "${lookup(nutanix_virtual_machine.vm3.nic_list_status.0.ip_endpoint_list[0], "ip")}" + } + `, r) +} From b854d8133e8a7b50b40306937c0ca2471a624357 Mon Sep 17 00:00:00 2001 From: Haroon-Dweikat-Ntx Date: Tue, 1 Oct 2024 15:04:44 +0300 Subject: [PATCH 2/2] fix lint for acc test cases --- nutanix/resource_nutanix_virtual_machine_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/nutanix/resource_nutanix_virtual_machine_test.go b/nutanix/resource_nutanix_virtual_machine_test.go index 021153de1..dfad042ff 100644 --- a/nutanix/resource_nutanix_virtual_machine_test.go +++ b/nutanix/resource_nutanix_virtual_machine_test.go @@ -628,7 +628,7 @@ func TestAccNutanixVirtualMachine_WithNicListIpType(t *testing.T) { CheckDestroy: testAccCheckNutanixVirtualMachineDestroy, Steps: []resource.TestStep{ { - Config: testaccnutanixvmconfigwithniclistIpType(r), + Config: testaccnutanixvmconfigwithniclistIPType(r), Check: resource.ComposeTestCheckFunc( testAccCheckNutanixVirtualMachineExists(resourceName), resource.TestCheckResourceAttr(resourceName, "hardware_clock_timezone", "UTC"), @@ -642,8 +642,8 @@ func TestAccNutanixVirtualMachine_WithNicListIpType(t *testing.T) { ), }, { - Config: testaccnutanixvmconfigwithniclistIpTypeWrongType(r), - ExpectError: regexp.MustCompile("Error: expected nic_list.0.ip_endpoint_list.0.type to be one of \\[ASSIGNED], got WRONG"), + Config: testaccnutanixvmconfigwithniclistIPTypeWrongType(r), + ExpectError: regexp.MustCompile(`Error: expected nic_list.0.ip_endpoint_list.0.type to be one of ` + `[ASSIGNED], got WRONG`), }, }, }) @@ -1634,7 +1634,7 @@ func testAccNutanixVMConfigWithSecureBootWithNoMachineType(name, desc, mem strin `, name, desc, mem) } -func testaccnutanixvmconfigwithniclistIpType(r int) string { +func testaccnutanixvmconfigwithniclistIPType(r int) string { return fmt.Sprintf(` data "nutanix_clusters" "clusters" {} @@ -1709,7 +1709,7 @@ func testaccnutanixvmconfigwithniclistIpType(r int) string { } `, r) } -func testaccnutanixvmconfigwithniclistIpTypeWrongType(r int) string { +func testaccnutanixvmconfigwithniclistIPTypeWrongType(r int) string { return fmt.Sprintf(` data "nutanix_clusters" "clusters" {}