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..dfad042ff 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) +}