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 parent and id arguments to the VDI data source #291

Merged
merged 1 commit into from
Jan 14, 2024
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
28 changes: 28 additions & 0 deletions client/vdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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
}
8 changes: 3 additions & 5 deletions docs/data-sources/vdi.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ resource "xenorchestra_vm" "demo-vm" {
<!-- schema generated by tfplugindocs -->
## 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.
4 changes: 2 additions & 2 deletions xoa/acc_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
17 changes: 16 additions & 1 deletion xoa/data_source_xenorchestra_vdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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),
Expand All @@ -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
}
52 changes: 50 additions & 2 deletions xoa/data_source_xenorchestra_vdi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""),
),
},
},
Expand Down Expand Up @@ -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" {
Expand Down
8 changes: 4 additions & 4 deletions xoa/resource_xenorchestra_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Loading