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

Add Serial Port support in VM resource and data source #45

Merged
merged 5 commits into from
Apr 16, 2019
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
9 changes: 9 additions & 0 deletions client/v3/v3_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type VMVnumaConfig struct {
NumVnumaNodes *int64 `json:"num_vnuma_nodes,omitempty"`
}

type VMSerialPort struct {
Index *int64 `json:"index,omitempty"`
IsConnected *bool `json:"is_connected,omitempty"`
}

// IPAddress An IP address.
type IPAddress struct {

Expand Down Expand Up @@ -258,6 +263,8 @@ type VMResources struct {

// Information regarding vNUMA configuration.
VMVnumaConfig *VMVnumaConfig `json:"vnuma_config,omitempty"`

SerialPortList []*VMSerialPort `json:"serial_port_list,omitempty"`
}

// VM An intentful representation of a vm spec
Expand Down Expand Up @@ -484,6 +491,8 @@ type VMResourcesDefStatus struct {

// Information regarding vNUMA configuration.
VnumaConfig *VMVnumaConfig `json:"vnuma_config,omitempty"`

SerialPortList []*VMSerialPort `json:"serial_port_list,omitempty"`
}

// VMDefStatus An intentful representation of a vm status
Expand Down
21 changes: 21 additions & 0 deletions nutanix/data_source_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,23 @@ func dataSourceNutanixVirtualMachine() *schema.Resource {
},
},
},

"serial_port_list": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"index": {
Type: schema.TypeInt,
Computed: true,
},
"is_connected": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -692,6 +709,10 @@ func dataSourceNutanixVirtualMachineRead(d *schema.ResourceData, meta interface{
return err
}

if err := d.Set("serial_port_list", resp.Status.Resources.SerialPortList); err != nil {
return err
}

d.Set("guest_customization_cloud_init_user_data", cloudInitUser)
d.Set("guest_customization_cloud_init_meta_data", cloudInitMeta)
d.Set("hardware_clock_timezone", utils.StringValue(resp.Status.Resources.HardwareClockTimezone))
Expand Down
67 changes: 66 additions & 1 deletion nutanix/resource_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"time"

"github.com/terraform-providers/terraform-provider-nutanix/client/v3"
v3 "github.com/terraform-providers/terraform-provider-nutanix/client/v3"
"github.com/terraform-providers/terraform-provider-nutanix/utils"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -700,6 +700,25 @@ func resourceNutanixVirtualMachine() *schema.Resource {
},
},
},
"serial_port_list": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"index": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"is_connected": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -829,6 +848,10 @@ func resourceNutanixVirtualMachineRead(d *schema.ResourceData, meta interface{})
if err := d.Set("nic_list_status", flattenNicListStatus(resp.Status.Resources.NicList)); err != nil {
return fmt.Errorf("error setting nic_list for Virtual Machine %s: %s", d.Id(), err)
}

if err := d.Set("serial_port_list", flattenSerialPortList(resp.Status.Resources.SerialPortList)); err != nil {
return fmt.Errorf("error setting serial_port_list for Virtual Machine %s: %s", d.Id(), err)
}
if err := d.Set("host_reference", flattenReferenceValues(resp.Status.Resources.HostReference)); err != nil {
return fmt.Errorf("error setting host_reference for Virtual Machine %s: %s", d.Id(), err)
}
Expand Down Expand Up @@ -1094,6 +1117,11 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
if d.HasChange("nic_list") {
res.NicList = expandNicList(d)
}

if d.HasChange("serial_port_list") {
res.SerialPortList = expandSerialPortList(d)
}

if d.HasChange("nutanix_guest_tools") {
_, n := d.GetChange("nutanix_guest_tools")
ngt := n.(map[string]interface{})
Expand Down Expand Up @@ -1465,6 +1493,7 @@ func getVMResources(d *schema.ResourceData, vm *v3.VMResources) error {
}

vm.DiskList = expandDiskList(d)
vm.SerialPortList = expandSerialPortList(d)

return nil
}
Expand Down Expand Up @@ -1596,6 +1625,29 @@ func expandDiskList(d *schema.ResourceData) []*v3.VMDisk {
return nil
}

func expandSerialPortList(d *schema.ResourceData) []*v3.VMSerialPort {
if v, ok := d.GetOk("serial_port_list"); ok {
spl := v.([]interface{})

if len(spl) > 0 {
serialPortList := make([]*v3.VMSerialPort, len(spl))
for k, val := range spl {
v1 := val.(map[string]interface{})
serialPort := &v3.VMSerialPort{}
if v1, ok1 := v1["index"]; ok1 {
serialPort.Index = utils.Int64Ptr(int64(v1.(int)))
}
if v1, ok1 := v1["is_connected"]; ok1 {
serialPort.IsConnected = utils.BoolPtr(v1.(bool))
}
serialPortList[k] = serialPort
}
return serialPortList
}
}
return nil
}

func expandGPUList(d *schema.ResourceData) []*v3.VMGpu {
if v, ok := d.GetOk("gpu_list"); ok {
if len(v.([]interface{})) > 0 {
Expand Down Expand Up @@ -1652,6 +1704,19 @@ func preFillResUpdateRequest(res *v3.VMResources, response *v3.VMIntentResponse)
}
res.NicList = nold

var spl []*v3.VMSerialPort
if len(response.Status.Resources.SerialPortList) > 0 {
spl = make([]*v3.VMSerialPort, len(response.Status.Resources.SerialPortList))
for k, v := range response.Status.Resources.SerialPortList {
spl[k] = &v3.VMSerialPort{
Index: v.Index,
IsConnected: v.IsConnected,
}

}
}
res.SerialPortList = spl

gold := make([]*v3.VMGpu, len(response.Status.Resources.GpuList))
if len(response.Status.Resources.GpuList) > 0 {
for k, v := range response.Status.Resources.GpuList {
Expand Down
63 changes: 63 additions & 0 deletions nutanix/resource_nutanix_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,39 @@ func TestAccNutanixVirtualMachine_WithSubnet(t *testing.T) {
})
}

func TestAccNutanixVirtualMachine_WithSerialPortList(t *testing.T) {
r := acctest.RandInt()
resourceName := "nutanix_virtual_machine.vm5"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNutanixVirtualMachineDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixVMConfigWithSerialPortList(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.TestCheckResourceAttr(resourceName, "categories.Environment", "Staging"),
resource.TestCheckResourceAttr(resourceName, "serial_port_list.0.index", "1"),
resource.TestCheckResourceAttr(resourceName, "serial_port_list.0.is_connected", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"disk_list"},
},
},
})
}

func testAccCheckNutanixVirtualMachineExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -449,3 +482,33 @@ output "ip_address" {
}
`, r)
}

func testAccNutanixVMConfigWithSerialPortList(r int) string {
return fmt.Sprintf(`
data "nutanix_clusters" "clusters" {}

locals {
cluster1 = "${data.nutanix_clusters.clusters.entities.0.service_list.0 == "PRISM_CENTRAL"
? data.nutanix_clusters.clusters.entities.1.metadata.uuid : data.nutanix_clusters.clusters.entities.0.metadata.uuid}"
}

resource "nutanix_virtual_machine" "vm5" {
name = "test-dou-%d"
cluster_uuid = "${local.cluster1}"

num_vcpus_per_socket = 1
num_sockets = 1
memory_size_mib = 186

serial_port_list = [{
index = 1
is_connected = true
}]


categories {
Environment = "Staging"
}
}
`, r)
}
16 changes: 15 additions & 1 deletion nutanix/structure.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nutanix

import (
"github.com/terraform-providers/terraform-provider-nutanix/client/v3"
v3 "github.com/terraform-providers/terraform-provider-nutanix/client/v3"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)

Expand Down Expand Up @@ -90,6 +90,20 @@ func flattenNicList(nics []*v3.VMNic) []map[string]interface{} {
return nicLists
}

func flattenSerialPortList(serialPorts []*v3.VMSerialPort) []map[string]interface{} {
serialPortList := make([]map[string]interface{}, 0)
if serialPorts != nil {
serialPortList = make([]map[string]interface{}, len(serialPorts))
for k, v := range serialPorts {
serialPort := make(map[string]interface{})
serialPort["index"] = utils.Int64Value(v.Index)
serialPort["is_connected"] = utils.BoolValue(v.IsConnected)
serialPortList[k] = serialPort
}
}
return serialPortList
}

func flattenGPUList(gpu []*v3.VMGpuOutputStatus) []map[string]interface{} {
gpuList := make([]map[string]interface{}, 0)
if gpu != nil {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/d/virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The following attributes are exported:
* `description`: - A description for vm.
* `num_vnuma_nodes`: - Number of vNUMA nodes. 0 means vNUMA is disabled.
* `nic_list`: - NICs attached to the VM.
* `serial_port_list`: - (Optional) Serial Ports configured on the VM.
* `guest_os_id`: - Guest OS Identifier. For ESX, refer to VMware documentation [link](https://www.vmware.com/support/developer/converter-sdk/conv43_apireference/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html) for the list of guest OS identifiers.
* `power_state`: - The current or desired power state of the VM. (Options : ON , OFF)
* `nutanix_guest_tools`: - Information regarding Nutanix Guest Tools.
Expand Down Expand Up @@ -164,6 +165,13 @@ The nic_list attribute supports the following:
* `subnet_uuid`: - The reference to a subnet.
* `subnet_name`: - The name of the subnet reference to.

### Serial Port List

The `serial_port_list` attribute supports the following:

* `index`: - Index of the serial port (int).
* `is_connected`: - Indicates whether the serial port connection is connected or not (`true` or `false`).

### ip_endpoint_list

The following attributes are exported:
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The following arguments are supported:
* `description`: - (Optional) A description for vm.
* `num_vnuma_nodes`: - (Optional) Number of vNUMA nodes. 0 means vNUMA is disabled.
* `nic_list`: - (Optional) Spec NICs attached to the VM.
* `serial_port_list`: - (Optional) Serial Ports configured on the VM.
* `guest_os_id`: - (Optional) Guest OS Identifier. For ESX, refer to VMware documentation [link](https://www.vmware.com/support/developer/converter-sdk/conv43_apireference/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html) for the list of guest OS identifiers.
* `power_state`: - (Optional) The current or desired power state of the VM. (Options : ON , OFF)
* `nutanix_guest_tools`: - (Optional) Information regarding Nutanix Guest Tools.
Expand Down Expand Up @@ -160,6 +161,13 @@ The nic_list attribute supports the following:
* `subnet_name`: - The name of the subnet reference to.
* `floating_ip`: - The Floating IP associated with the vnic. (Only in `nic_list_status`)

### Serial Port List

The `serial_port_list` attribute supports the following:

* `index`: - Index of the serial port (int).
* `is_connected`: - Indicates whether the serial port connection is connected or not (`true` or `false`).

### ip_endpoint_list

The following attributes are exported:
Expand Down