From 163acf11d17465edec055ae7f0c94a675125176a Mon Sep 17 00:00:00 2001 From: Dom Del Nano Date: Sat, 13 Jan 2024 23:23:49 -0800 Subject: [PATCH] Add parent and id arguments to the VDI data source --- client/vdi.go | 28 +++++++++++++ docs/data-sources/vdi.md | 8 ++-- xoa/acc_setup_test.go | 4 +- xoa/data_source_xenorchestra_vdi.go | 17 +++++++- xoa/data_source_xenorchestra_vdi_test.go | 52 +++++++++++++++++++++++- xoa/resource_xenorchestra_vm_test.go | 8 ++-- 6 files changed, 103 insertions(+), 14 deletions(-) diff --git a/client/vdi.go b/client/vdi.go index f1b93790..65d538e8 100644 --- a/client/vdi.go +++ b/client/vdi.go @@ -31,6 +31,7 @@ type VDI struct { NameDescription string `json:"name_description"` Size int `json:"size"` VBDs []string `json:"$VBDs"` + Parent string `json:"parent",omitempty` PoolId string `json:"$poolId"` Tags []string `json:"tags,omitempty"` } @@ -397,3 +398,30 @@ func RemoveVDIsWithPrefix(prefix string) func(string) error { return nil } } + +func FindVDIForTests(pool Pool, isoVdi *VDI, isoNameEnvVar string) { + isoName, found := os.LookupEnv(isoNameEnvVar) + if !found { + fmt.Println(fmt.Sprintf("The %s environment variable must be set for the tests", isoNameEnvVar)) + os.Exit(-1) + } + + c, err := NewClient(GetConfigFromEnv()) + if err != nil { + fmt.Printf("failed to create client with error: %v", err) + os.Exit(-1) + } + + vdiReq := VDI{ + PoolId: pool.Id, + NameLabel: isoName, + } + vdi, err := c.GetVDI(vdiReq) + + if err != nil { + fmt.Printf("failed to find an iso vdi with error: %v\n", err) + os.Exit(-1) + } + + *isoVdi = vdi +} diff --git a/docs/data-sources/vdi.md b/docs/data-sources/vdi.md index d4cd7f95..15178621 100644 --- a/docs/data-sources/vdi.md +++ b/docs/data-sources/vdi.md @@ -30,15 +30,13 @@ resource "xenorchestra_vm" "demo-vm" { ## Schema -### Required - -- `name_label` (String) The name of the VDI to look up. - ### Optional +- `id` (String) The ID of the VDI. +- `name_label` (String) The name of the VDI to look up. - `pool_id` (String) The ID of the pool the VDI belongs to. This is useful if you have a VDI with the same name on different pools. - `tags` (Set of String) The tags (labels) applied to the given entity. ### Read-Only -- `id` (String) The ID of this resource. +- `parent` (String) The ID of the parent VDI if one exists. An example of when a VDI will have a parent is when it was created from a VM fast clone. diff --git a/xoa/acc_setup_test.go b/xoa/acc_setup_test.go index 71dd4adc..19a58c01 100644 --- a/xoa/acc_setup_test.go +++ b/xoa/acc_setup_test.go @@ -21,7 +21,7 @@ var accUser client.User = client.User{Email: fmt.Sprintf("%s-%s", accTestPrefix, var testTemplate client.Template var accTestPIF client.PIF var disklessTestTemplate client.Template -var testIsoName string +var testIso client.VDI func TestMain(m *testing.M) { // This leverages the existing flag defined in the terraform-plugin-sdk @@ -46,7 +46,7 @@ func TestMain(m *testing.M) { client.FindStorageRepositoryForTests(accTestPool, &accDefaultSr, accTestPrefix) client.FindIsoStorageRepositoryForTests(accTestPool, &accIsoSr, accTestPrefix, "XOA_ISO_SR") client.CreateUser(&accUser) - testIsoName = os.Getenv("XOA_ISO") + client.FindVDIForTests(accTestPool, &testIso, "XOA_ISO") fmt.Printf("Found the following pool: %v sr: %v\n", accTestPool, accDefaultSr) code := m.Run() diff --git a/xoa/data_source_xenorchestra_vdi.go b/xoa/data_source_xenorchestra_vdi.go index ce4c818c..ec3af4cb 100644 --- a/xoa/data_source_xenorchestra_vdi.go +++ b/xoa/data_source_xenorchestra_vdi.go @@ -16,10 +16,22 @@ func dataSourceXoaVDI() *schema.Resource { Ensure that your name_label, pool_id and tags identify a unique VDI.`, Read: dataSourceVDIRead, Schema: map[string]*schema.Schema{ + "id": &schema.Schema{ + Type: schema.TypeString, + Description: "The ID of the VDI.", + Computed: true, + Optional: true, + ExactlyOneOf: []string{"name_label"}, + }, "name_label": &schema.Schema{ Type: schema.TypeString, Description: "The name of the VDI to look up.", - Required: true, + Optional: true, + }, + "parent": &schema.Schema{ + Type: schema.TypeString, + Description: "The ID of the parent VDI if one exists. An example of when a VDI will have a parent is when it was created from a VM fast clone.", + Computed: true, }, "pool_id": &schema.Schema{ Description: "The ID of the pool the VDI belongs to. This is useful if you have a VDI with the same name on different pools.", @@ -34,11 +46,13 @@ Ensure that your name_label, pool_id and tags identify a unique VDI.`, func dataSourceVDIRead(d *schema.ResourceData, m interface{}) error { c := m.(client.XOClient) + id := d.Get("id").(string) nameLabel := d.Get("name_label").(string) poolId := d.Get("pool_id").(string) tags := d.Get("tags").(*schema.Set).List() vdi := client.VDI{ + VDIId: id, NameLabel: nameLabel, PoolId: poolId, Tags: tagsFromInterfaceSlice(tags), @@ -61,5 +75,6 @@ func dataSourceVDIRead(d *schema.ResourceData, m interface{}) error { d.Set("name_label", vdi.NameLabel) d.Set("pool_id", vdi.PoolId) d.Set("tags", vdi.Tags) + d.Set("parent", vdi.Parent) return nil } diff --git a/xoa/data_source_xenorchestra_vdi_test.go b/xoa/data_source_xenorchestra_vdi_test.go index c33c2a72..59b3b607 100644 --- a/xoa/data_source_xenorchestra_vdi_test.go +++ b/xoa/data_source_xenorchestra_vdi_test.go @@ -9,18 +9,39 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) -func TestAccXenorchestraDataSource_vdi(t *testing.T) { +func TestAccXenorchestraDataSource_vdiById(t *testing.T) { resourceName := "data.xenorchestra_vdi.vdi" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccXenorchestraDataSourceVDIConfig(testIsoName), + Config: testAccXenorchestraDataSourceVDIConfigById(testIso.VDIId), Check: resource.ComposeAggregateTestCheckFunc( testAccCheckXenorchestraDataSourceVDI(resourceName), resource.TestCheckResourceAttrSet(resourceName, "id"), resource.TestCheckResourceAttrSet(resourceName, "name_label"), + resource.TestCheckResourceAttr(resourceName, "parent", ""), + ), + }, + }, + }, + ) +} + +func TestAccXenorchestraDataSource_vdiByNameLabel(t *testing.T) { + resourceName := "data.xenorchestra_vdi.vdi" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccXenorchestraDataSourceVDIConfig(testIso.NameLabel), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckXenorchestraDataSourceVDI(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "id"), + resource.TestCheckResourceAttrSet(resourceName, "name_label"), + resource.TestCheckResourceAttr(resourceName, "parent", ""), ), }, }, @@ -48,6 +69,33 @@ func TestAccXenorchestraDataSource_vdiNotFound(t *testing.T) { ) } +func TestAccXenorchestraDataSource_vdiExactlyOneOfIdOrNameLabelRequired(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: ` +data "xenorchestra_vdi" "vdi" { + id = "test" + name_label = "test" +} + `, + ExpectError: regexp.MustCompile(`Invalid combination of arguments`), + }, + }, + }, + ) +} + +func testAccXenorchestraDataSourceVDIConfigById(id string) string { + return fmt.Sprintf(` +data "xenorchestra_vdi" "vdi" { + id = "%s" +} +`, id) +} + func testAccXenorchestraDataSourceVDIConfig(nameLabel string) string { return fmt.Sprintf(` data "xenorchestra_vdi" "vdi" { diff --git a/xoa/resource_xenorchestra_vm_test.go b/xoa/resource_xenorchestra_vm_test.go index 20381250..fc80abbb 100644 --- a/xoa/resource_xenorchestra_vm_test.go +++ b/xoa/resource_xenorchestra_vm_test.go @@ -1816,7 +1816,7 @@ resource "xenorchestra_vm" "bar" { size = 10001317888 } } -`, testIsoName, accTestPool.Id, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id) +`, testIso.NameLabel, accTestPool.Id, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id) } func testAccVmConfigWithoutISO(vmName string) string { @@ -1849,7 +1849,7 @@ resource "xenorchestra_vm" "bar" { size = 10001317888 } } -`, testIsoName, accTestPool.Id, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id) +`, testIso.NameLabel, accTestPool.Id, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id) } func testAccVmConfigWithTags(vmName, tag, secondTag string) string { @@ -2072,7 +2072,7 @@ resource "xenorchestra_vm" "bar" { size = 10001317888 } } -`, testIsoName, accTestPool.Id, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id) +`, testIso.NameLabel, accTestPool.Id, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id) } func testAccVmConfigWithShortTimeout(vmName string) string { @@ -2140,7 +2140,7 @@ resource "xenorchestra_vm" "bar" { size = 10001317888 } } -`, testIsoName, accTestPool.Id, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id) +`, testIso.NameLabel, accTestPool.Id, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id) } func testAccVmConfigWaitForIp(vmName string) string {