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

Vdisk update #23 #54

Merged
merged 13 commits into from
Apr 30, 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
6 changes: 3 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ testacc: fmtcheck

fmt:
@echo "==> Fixing source code with gofmt..."
goimports -s -w ./$(PKG_NAME)
goimports -s -w ./client
goimports -s -w ./utils
goimports -w ./$(PKG_NAME)
goimports -w ./client
goimports -w ./utils

fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"
Expand Down
4 changes: 4 additions & 0 deletions client/v3/v3_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type VMNic struct {
// The NIC's UUID, which is used to uniquely identify this particular NIC. This UUID may be used to refer to the NIC
// outside the context of the particular VM it is attached to.
UUID *string `json:"uuid,omitempty"`

IsConnected *bool `json:"is_connected,omitempty"`
}

// DiskAddress Disk Address.
Expand Down Expand Up @@ -344,6 +346,8 @@ type VMNicOutputStatus struct {
// The NIC's UUID, which is used to uniquely identify this particular NIC. This UUID may be used to refer to the NIC
// outside the context of the particular VM it is attached to.
UUID *string `json:"uuid,omitempty"`

IsConnected *bool `json:"is_connected,omitempty"`
}

// NutanixGuestToolsStatus Information regarding Nutanix Guest Tools.
Expand Down
114 changes: 74 additions & 40 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# - nutanix_subnet
# - nutanix_image
# - data sources
# -
# - nutanix_clusters
# -
# -
# - script Variables
Expand All @@ -27,21 +27,23 @@
### While it may be possible to use Prism Element directly, Nutanix's
### provider is not structured or tested for this. Using Prism Central will
### give the broadest capabilities across the board
provider "nutanix" {
/* provider "nutanix" {
username = "admin"
password = "Nutanix/1234"
endpoint = "10.5.80.255"
insecure = true
port = 9440
}
} */

data "nutanix_clusters" "clusters" {}

### Define Script Local Variables
### This can be used for any manner of things, but is useful for like clusterid,
### to store a mapping of targets for provisioning
### TODO: Need to make clusters a data source object, such that consumers do
### not need to manually provision cluster ID
locals {
cluster1 = "00054051-250f-5ccc-0000-00000000cf0d"
cluster1 = "${data.nutanix_clusters.clusters.entities.1.metadata.uuid}"
}

##########################
Expand Down Expand Up @@ -97,9 +99,11 @@ locals {
# called in as data sources, which you can see in the data sources section
# above.
resource "nutanix_image" "cirros-034-disk" {
name = "cirros-034-disk"
source_uri = "http://endor.dyn.nutanix.com/acro_images/DISKs/cirros-0.3.4-x86_64-disk.img"
description = "heres a tiny linux image, not an iso, but a real disk!"
name = "cirros-034-disk"

#source_uri = "http://endor.dyn.nutanix.com/acro_images/DISKs/cirros-0.3.4-x86_64-disk.img"
source_uri = "http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img"
description = "heres a tiny linux image, not an iso, but a real disk!"
}

### Subnet Resources (Virtual Networks within AHV)
Expand Down Expand Up @@ -143,28 +147,33 @@ resource "nutanix_image" "cirros-034-disk" {
# ### Define Terraform Managed Subnets
resource "nutanix_subnet" "infra-managed-network-140" {
# What cluster will this VLAN live on?
cluster_reference = {
kind = "cluster"
uuid = "${local.cluster1}"
}
cluster_uuid = "${local.cluster1}"

# General Information
name = "infra-managed-network-140"
vlan_id = 140
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"
# domain_name_server_list = ["8.8.8.8", "4.2.2.2"]
# domain_search_list = ["terraform.nutanix.com", "terraform.unit.test.com"]
# tftp_server_name = "10.250.140.200"
# }
# Provision a Managed L3 Network
# This bit is only needed if you intend to turn on AHV's IPAM
subnet_ip = "172.21.32.0"

default_gateway_ip = "172.21.32.1"
prefix_length = 24

dhcp_options {
boot_file_name = "bootfile"
domain_name = "ntnxlab"
tftp_server_name = "172.21.32.200"
}

dhcp_server_address {
ip = "172.21.32.254"
}

dhcp_domain_name_server_list = ["172.21.30.223"]
dhcp_domain_search_list = ["ntnxlab.local"]
#ip_config_pool_list_ranges = ["172.21.32.3 172.21.32.253"]
}

### Virtual Machine Resources
Expand All @@ -186,23 +195,18 @@ resource "nutanix_virtual_machine" "demo-01-web" {
memory_size_mib = 4096

# What cluster will this VLAN live on?
cluster_reference = {
kind = "cluster"
uuid = "${local.cluster1}"
}
cluster_uuid = "${local.cluster1}"

# What networks will this be attached to?
nic_list = [{
# subnet_reference is saying, which VLAN/network do you want to attach here?
subnet_reference = {
kind = "subnet"
uuid = "${nutanix_subnet.infra-managed-network-140.id}"
}

# ip_endpoint_list = {
# ip = "${local.ip_haproxy}"
# type = "ASSIGNED"
# }
subnet_uuid = "${nutanix_subnet.infra-managed-network-140.id}"

# Used to set static IP.
# ip_endpoint_list = {
# ip = "172.21.32.20"
# type = "ASSIGNED"
# }
}]

# What disk/cdrom configuration will this have?
Expand All @@ -215,11 +219,41 @@ resource "nutanix_virtual_machine" "demo-01-web" {
uuid = "${nutanix_image.cirros-034-disk.id}"
}]

# defining an additional entry in the disk_list array will create another
# disk in addition to the image we're showing off above.
device_properties = [{
disk_address {
device_index = 0
adapter_type = "SCSI"
}

device_type = "DISK"
}]
disk_size_mib = 5000
}]
},
{
# defining an additional entry in the disk_list array will create another.

#disk_size_mib and disk_size_bytes must be set together.
disk_size_mib = 100000
disk_size_bytes = 104857600000
},
]

#Using provisioners
#Use as the following provisioner block if you know that you are geeting an reachable IP address.
#Get ssh connection and execute commands.
# provisioner "remote-exec" {
# connection {
# user = "cirros" # user from the image attached
# password = "cubswin:)" #password from the user
# #host = "172.21.32.20" #Set if you know
# }

# inline = [
# "echo \"Hello World\"",
# ]
# }
}

# Show IP address
output "ip_address" {
value = "${lookup(nutanix_virtual_machine.demo-01-web.nic_list_status.0.ip_endpoint_list[0], "ip")}"
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 h1:zwtduBRr5SSW
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4/go.mod h1:Izgrg8RkN3rCIMLGE9CyYmU9pY2Jer6DgANEnZ/L/cQ=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.1.1-0.20171002171727-8ebdfab36c66/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
Expand Down
3 changes: 1 addition & 2 deletions nutanix/data_source_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func dataSourceNutanixVirtualMachine() *schema.Resource {
Computed: true,
},
"disk_address": {
Type: schema.TypeList,
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -582,7 +582,6 @@ func dataSourceNutanixVirtualMachine() *schema.Resource {
},
},
},

"serial_port_list": {
Type: schema.TypeList,
Computed: true,
Expand Down
78 changes: 78 additions & 0 deletions nutanix/data_source_nutanix_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ func TestAccNutanixVirtualMachineDataSource_basic(t *testing.T) {
})
}

func TestAccNutanixVirtualMachineDataSource_WithDisk(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccVMDataSourceConfigWithDisk(acctest.RandIntRange(0, 500)),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.nutanix_virtual_machine.nutanix_virtual_machine", "num_vcpus_per_socket", "1"),
resource.TestCheckResourceAttr(
"data.nutanix_virtual_machine.nutanix_virtual_machine", "num_sockets", "1"),
),
},
},
})
}

func testAccVMDataSourceConfig(r int) string {
return fmt.Sprintf(`
data "nutanix_clusters" "clusters" {}
Expand All @@ -49,3 +68,62 @@ data "nutanix_virtual_machine" "nutanix_virtual_machine" {
}
`, r)
}

func testAccVMDataSourceConfigWithDisk(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_image" "cirros-034-disk" {
name = "test-image-dou-vm-create-%[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" "vm1" {
name = "test-dou-vm-%[1]d"
cluster_uuid = "${local.cluster1}"
num_vcpus_per_socket = 1
num_sockets = 1
memory_size_mib = 186

disk_list = [{
# data_source_reference in the Nutanix API refers to where the source for
# the disk device will come from. Could be a clone of a different VM or a
# image like we're doing here
data_source_reference = [{
kind = "image"
uuid = "${nutanix_image.cirros-034-disk.id}"
}]

device_properties = [{
disk_address {
device_index = 0,
adapter_type = "SCSI"
}
device_type = "DISK"
}]
#disk_size_bytes = 42950144
#disk_size_mib = 41
},
{
disk_size_mib = 100
},
{
disk_size_mib = 200
},
{
disk_size_mib = 300
}]
}

data "nutanix_virtual_machine" "nutanix_virtual_machine" {
vm_id = "${nutanix_virtual_machine.vm1.id}"
}

`, r)
}
16 changes: 14 additions & 2 deletions nutanix/resource_nutanix_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,12 @@ func resourceNutanixImageCreate(d *schema.ResourceData, meta interface{}) error
}

if _, errw := stateConf.WaitForState(); errw != nil {
return fmt.Errorf("error waiting for image (%s) to create: %s", d.Id(), errw)
delErr := resourceNutanixImageDelete(d, meta)
if delErr != nil {
return fmt.Errorf("error waiting for image (%s) to delete in creation: %s", d.Id(), delErr)
}
d.SetId("")
return fmt.Errorf("error waiting for image (%s) to create: %s", UUID, errw)
}

// if we need to upload an image, we do it now
Expand Down Expand Up @@ -502,7 +507,13 @@ func resourceNutanixImageUpdate(d *schema.ResourceData, meta interface{}) error
}

if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("error waiting for image (%s) to update: %s", d.Id(), err)
delErr := resourceNutanixImageDelete(d, meta)
if delErr != nil {
return fmt.Errorf("error waiting for image (%s) to delete in update: %s", d.Id(), delErr)
}
uuid := d.Id()
d.SetId("")
return fmt.Errorf("error waiting for image (%s) to update: %s", uuid, err)
}

return resourceNutanixImageRead(d, meta)
Expand Down Expand Up @@ -542,6 +553,7 @@ func resourceNutanixImageDelete(d *schema.ResourceData, meta interface{}) error
}

if _, err := stateConf.WaitForState(); err != nil {
d.SetId("")
return fmt.Errorf("error waiting for image (%s) to delete: %s", d.Id(), err)
}

Expand Down
5 changes: 5 additions & 0 deletions nutanix/resource_nutanix_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,11 @@ func resourceNutanixSubnetRead(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}
errDel := resourceNutanixSubnetDelete(d, meta)
if errDel != nil {
return fmt.Errorf("error deleting subnet (%s) after read error: %+v", id, errDel)
}
d.SetId("")
return fmt.Errorf("error reading subnet id (%s): %+v", id, err)
}

Expand Down
Loading