vatesfr / terraform-provider-xenorchestra

Xen Orchestra provider for Terraform
MIT License
150 stars 33 forks source link

Add vms data source #165

Closed gohumble closed 3 years ago

gohumble commented 3 years ago

Continuation of #161. Rebased by @ddelnano.

I've tested these changes in the past week in my development environment. Seems to be working as expected. I was not able to identify any bugs or side effects (yet).

Todo

Index: xoa/data_source_vms.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/xoa/data_source_vms.go b/xoa/data_source_vms.go
--- a/xoa/data_source_vms.go    (revision 470c245132d51d70fee07e45aa0ba9a6a2c37b73)
+++ b/xoa/data_source_vms.go    (revision 8a31b0c725d48cbe88cf930fd0c695285e7ac72d)
@@ -1,11 +1,13 @@
 package xoa

 import (
+   "fmt"
+   "github.com/ddelnano/terraform-provider-xenorchestra/xoa/internal"
    "log"
+   "strconv"
    "strings"

    "github.com/ddelnano/terraform-provider-xenorchestra/client"
-   "github.com/ddelnano/terraform-provider-xenorchestra/xoa/internal"
    "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
 )

@@ -51,7 +53,10 @@
    if err = d.Set("vms", vmToMapList(vms)); err != nil {
        return err
    }
-   d.SetId(internal.Strings([]string{searchVm.PowerState, searchVm.PoolId, searchVm.Host}))
+   hash := internal.String(fmt.Sprintf(
+       "%s-%s-%s-", searchVm.PowerState, searchVm.PoolId, searchVm.Host,
+   ))
+   d.SetId(strconv.Itoa(hash))
    return nil

 }
Index: xoa/internal/hashcode.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/xoa/internal/hashcode.go b/xoa/internal/hashcode.go
--- a/xoa/internal/hashcode.go  (revision 470c245132d51d70fee07e45aa0ba9a6a2c37b73)
+++ b/xoa/internal/hashcode.go  (revision 267cd3075b2c6a42607e1fce01202b15162b04d4)
@@ -29,12 +29,8 @@
 func Strings(strings []string) string {
    var buf bytes.Buffer

-   for i, s := range strings {
-       var format = "%s"
-       if i != len(strings)-1 {
-           format = fmt.Sprint(format, "-")
-       }
-       buf.WriteString(fmt.Sprintf(format, s))
+   for _, s := range strings {
+       buf.WriteString(fmt.Sprintf("%s-", s))
    }

    return fmt.Sprintf("%d", String(buf.String()))

Please note, that this patch is using "%s-%s-%s-" for the formatting directive, to match the output of internal.Strings() function.

Example

Example shows how to retrieve CPU and memory information for running VM's. In combination with #160 users will be able to get information about free CPU's and memory, grouped by host. I am using this feature to take an automated decision, on which host the next VM resource should be created.

Terraform configuration

data "xenorchestra_pool" "pool" {
  name_label = "MY-POOL"
}

data "xenorchestra_hosts" "hosts" {
  pool_id = data.xenorchestra_pool.pool.id
}

data "xenorchestra_vms" "vms" {
  pool_id = data.xenorchestra_pool.pool.id
  power_state = "Running"
}

output "host-info" {
  value = local.hostInfo
}

locals {
  hostInfo = tomap({
  for k, host in data.xenorchestra_hosts.hosts.hosts : host.name_label => {
    max_memory : host.memory,
    max_cpu : host.cpus.cores,
    usage_memory: sum(tolist([for k, vm in data.xenorchestra_vms.vms.vms : vm.memory_max if vm.host == host.id])),
    usage_cpu: sum(tolist([for k, vm in data.xenorchestra_vms.vms.vms : vm.cpus if vm.host == host.id]))
  }})

}

Output


host-info = tomap({
  "xcp-ng01" = {
    "max_cpu" = 64
    "max_memory" = 274485751808
    "usage_cpu" = 46
    "usage_memory" = 223338295296
  }
  "xcp-ng02" = {
    "max_cpu" = 64
    "max_memory" = 274485751808
    "usage_cpu" = 54
    "usage_memory" = 156766302208
  }
  "xcp-ng03" = {
    "max_cpu" = 64
    "max_memory" = 274485751808
    "usage_cpu" = 56
    "usage_memory" = 151397584896
  }
  "xcp-ng05" = {
    "max_cpu" = 64
    "max_memory" = 274485911552
    "usage_cpu" = 36
    "usage_memory" = 99857977344
  }
  "xcp-ng06" = {
    "max_cpu" = 64
    "max_memory" = 274485911552
    "usage_cpu" = 48
    "usage_memory" = 241591898112
  }
  "xcp-ng07" = {
    "max_cpu" = 48
    "max_memory" = 274484908032
    "usage_cpu" = 48
    "usage_memory" = 201326579712
  }
})
ddelnano commented 3 years ago

This lgtm once the crc code change is reverted. I will run the entire acceptance test suite myself before merging this as a final step as well.

ddelnano commented 3 years ago

make testacc passes and make testclient passes with the exception of a single, unrelated test. I'll merge this for now and drill into that test failure before releasing.

ddelnano commented 3 years ago

Thanks for all the hard work on this :+1: