Skip to content
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.

Feature/guest ip #38

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea
*.iml
vendor/
dist/
44 changes: 44 additions & 0 deletions examples/datasource_vm_guest_metrics/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# To execute with `credentials.tfvars` in the `examples` folder:
# terraform init && terraform plan --var-file=../credentials.tfvars && terraform apply --var-file=../credentials.tfvars && terraform destroy --var-file=../credentials.tfvars -force

variable "url" {}
variable "username" {}
variable "password" {}
variable "vm_uuid" {}

provider "xenserver" {
url = "${var.url}"
username = "${var.username}"
password = "${var.password}"
}

data "xenserver_vm_guest_metrics" "interfaces" {
vm_uuid = "${vm_uuid}"
}

data "template_file" "interfaces_written" {
template = "$${uuids}"

vars {
uuids = "${jsonencode(data.xenserver_vm_guest_metrics.interfaces.networks)}"
}
}

resource "null_resource" "interfaces_file" {
triggers {
content = "${data.template_file.interfaces_written.rendered}"
}

provisioner "local-exec" {
command = <<-EOC
tee ${path.cwd}/output.json <<EOF
${data.template_file.interfaces_written.rendered}
EOF
EOC
}
}

output "vm_main_interface" {
value = "${data.xenserver_vm_guest_metrics.interfaces.networks[0]}"
}

43 changes: 43 additions & 0 deletions examples/datasource_vm_networks/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# To execute with `credentials.tfvars` in the `examples` folder:
# terraform init && terraform plan --var-file=../credentials.tfvars && terraform apply --var-file=../credentials.tfvars && terraform destroy --var-file=../credentials.tfvars -force

variable "url" {}
variable "username" {}
variable "password" {}
variable "vm_uuid" {}

provider "xenserver" {
url = "${var.url}"
username = "${var.username}"
password = "${var.password}"
}

data "xenserver_vm_networks" "interfaces" {
vm_uuid = "${vm_uuid}"
}

data "template_file" "interfaces_written" {
template = "$${ips}"

vars {
ips = "${jsonencode(data.xenserver_vm_networks.interfaces.ip)}"
}
}

resource "null_resource" "interfaces_file" {
triggers {
content = "${data.template_file.interfaces_written.rendered}"
}

provisioner "local-exec" {
command = <<-EOC
tee ${path.cwd}/output.json <<EOF
${data.template_file.interfaces_written.rendered}
EOF
EOC
}
}

output "vm_main_ip" {
value = "${${element(data.xenserver_vm_networks.interfaces.ip[0],0)}}"
}
83 changes: 83 additions & 0 deletions examples/datasource_vm_networks/newvm.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# To execute with `credentials.tfvars` in the `examples` folder:
# terraform init && terraform plan --var-file=../credentials.tfvars && terraform apply --var-file=../credentials.tfvars && terraform destroy --var-file=../credentials.tfvars -force

variable "url" {}
variable "username" {}
variable "password" {}
variable "vm_uuid" {}

provider "xenserver" {
url = "${var.url}"
username = "${var.username}"
password = "${var.password}"
}

resource "xenserver_vm" "newvm" {
base_template_name = "CentOS 7.4 Template"
name_label = "New VM"
static_mem_min = 2147483648
static_mem_max = 2147483648
dynamic_mem_min = 2147483648
dynamic_mem_max = 2147483648
vcpus = 1
boot_order = "c"
hard_drive {
is_from_template = true
user_device = "0"
}
cdrom {
is_from_template = true
user_device = "3"
}
network_interface {
network_uuid = "92467b56-21a7-dfdd-b412-978181a69f32"
device = 0
mtu = 1500
mac = ""
other_config {
ethtool-gso = "off"
ethtool-ufo = "off"
ethtool-tso = "off"
ethtool-sg = "off"
ethtool-tx = "off"
ethtool-rx = "off"
}
}
network_interface {
network_uuid = "9318f024-7937-870f-32c7-171040f1fbd8"
device = 1
mtu = 1500
mac = ""
}
}

data "xenserver_vm_networks" "new_interfaces" {
vm_uuid = "${xenserver_vm.newvm.id}"
startup_delay = 10 # wait for VM to boot for 10 seconds
}

data "template_file" "new_interfaces_written" {
template = "$${ips}"

vars {
ips = "${jsonencode(data.xenserver_vm_networks.new_interfaces.ip)}"
}
}

resource "null_resource" "new_interfaces_file" {
triggers {
content = "${data.template_file.new_interfaces_written.rendered}"
}

provisioner "local-exec" {
command = <<-EOC
tee ${path.cwd}/newvm_output.json <<EOF
${data.template_file.new_interfaces_written.rendered}
EOF
EOC
}
}

output "new_vm_main_ip" {
value = "${${element(data.xenserver_vm_networks.new_interfaces.ip[0],0)}}"
}
5 changes: 5 additions & 0 deletions examples/datasource_vm_networks/provision.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- hosts: all
tasks:
- hostname:
name: "{{ provisioned_host_name }}"
63 changes: 63 additions & 0 deletions examples/datasource_vm_networks/provision_ansible.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# To execute with `credentials.tfvars` in the `examples` folder:
# terraform init && terraform plan --var-file=../credentials.tfvars && terraform apply --var-file=../credentials.tfvars && terraform destroy --var-file=../credentials.tfvars -force

variable "url" {}
variable "username" {}
variable "password" {}

provider "xenserver" {
url = "${var.url}"
username = "${var.username}"
password = "${var.password}"
}

resource "xenserver_vm" "ansiblevm" {
base_template_name = "CentOS 7.4 Template"
name_label = "Ansible VM"
static_mem_min = 2147483648
static_mem_max = 2147483648
dynamic_mem_min = 2147483648
dynamic_mem_max = 2147483648
vcpus = 1
boot_order = "c"
hard_drive {
is_from_template = true
user_device = "0"
}
cdrom {
is_from_template = true
user_device = "3"
}
network_interface {
network_uuid = "92467b56-21a7-dfdd-b412-978181a69f32"
device = 0
mtu = 1500
mac = ""
other_config {
ethtool-gso = "off"
ethtool-ufo = "off"
ethtool-tso = "off"
ethtool-sg = "off"
ethtool-tx = "off"
ethtool-rx = "off"
}
}
network_interface {
network_uuid = "9318f024-7937-870f-32c7-171040f1fbd8"
device = 1
mtu = 1500
mac = ""
}
}

data "xenserver_vm_networks" "ansible_interfaces" {
vm_uuid = "${xenserver_vm.ansiblevm.id}"
startup_delay = 10 # wait for VM to boot for 10 seconds
}

resource "null_resource" "provision_ansiblevm" {
provisioner "local-exec" {
command =
"sleep 120; ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i '${element(data.xenserver_vm_networks.ansible_interfaces.ip[0],0)},' --extra-vars 'provisioned_host_name=ansible.local' ${path.cwd}/provision.yaml"
}
}
30 changes: 30 additions & 0 deletions website/docs/d/vm_guest_metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: "xenserver_vm_data_metrics"
---

Provides metrics reported by guest tools.

## Parameters
* [in] vm_uuid - UUID of the VM to obtain metrics from
* [out] disks
* [out] networks
* [out] memory
* [out] os_version
* [out] pv_driver_version
* [out] is_pv_driver_present
* [out] can_use_hotplug_vbd
* [out] can_use_hotplug_vif
* [out] is_live
* [out] last_updated

## Example Usage

```hcl
data "xenserver_vm_guest_metrics" "interfaces" {
vm_uuid = "${vm_uuid}"
}

output "vm_main_interface" {
value = "${data.xenserver_vm_guest_metrics.interfaces.networks[0]}"
}
```
23 changes: 23 additions & 0 deletions website/docs/d/vm_networks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: "xenserver_vm_networks"
---

Query information on IP addresses reported by guest tools.

## Parameters
* [in] vm_uuid - UUID of the VM to query information about
* [in] [optional] startup_delay - how many seconds should be passed after VM start before information could be queried. Useful when new VM was just created to wait until it boots and guest tools are started
* [out] ip array of arrays with ip addresses where first index corresponds to interface and second to address
* [out] ipv6 array of arrays with IPv6 addresses where first index corresponds to interface and second to address

## Example Usage

```hcl
data "xenserver_vm_networks" "interfaces" {
vm_uuid = "${vm_uuid}"
}

output "vm_main_ip" {
value = "${${element(data.xenserver_vm_networks.interfaces.ip[0],0)}}"
}
```
Loading