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

Make disks optional in VM resource #195

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 23 additions & 18 deletions client/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,28 +184,33 @@ func (c *Client) CreateVm(vmReq Vm, createTime time.Duration) (*Vm, error) {
return nil, errors.New(fmt.Sprintf("cannot create VM when multiple templates are returned: %v", tmpl))
}

useExistingDisks := tmpl[0].isDiskTemplate()
var vdis = []interface{}{}
var existingDisks = map[string]interface{}{}
installation := vmReq.Installation
if !useExistingDisks && installation.Method != "cdrom" && installation.Method != "network" {
return nil, errors.New("cannot create a VM from a diskless template without an ISO")
}

existingDisks := map[string]interface{}{}
vdis := []interface{}{}
disks := vmReq.Disks
if len(vmReq.Disks) != 0 {
useExistingDisks := tmpl[0].isDiskTemplate()

firstDisk := createVdiMap(disks[0])
// Treat the first disk differently. This covers the
// case where we are using a template with an already
// installed OS or a diskless template.
if useExistingDisks {
existingDisks["0"] = firstDisk
} else {
vdis = append(vdis, firstDisk)
}
if !useExistingDisks && installation.Method != "cdrom" && installation.Method != "network" {
return nil, errors.New("cannot create a VM from a diskless template without an ISO")
}

disks := vmReq.Disks

for i := 1; i < len(disks); i++ {
vdis = append(vdis, createVdiMap(disks[i]))
firstDisk := createVdiMap(disks[0])

// Treat the first disk differently. This covers the
// case where we are using a template with an already
// installed OS or a diskless template.
if useExistingDisks {
existingDisks["0"] = firstDisk
} else {
vdis = append(vdis, firstDisk)
}

for i := 1; i < len(disks); i++ {
vdis = append(vdis, createVdiMap(disks[i]))
}
}

params := map[string]interface{}{
Expand Down
2 changes: 1 addition & 1 deletion xoa/resource_xenorchestra_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func resourceVmSchema() map[string]*schema.Schema {
},
"disk": &schema.Schema{
Type: schema.TypeList,
Required: true,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sr_id": &schema.Schema{
Expand Down
51 changes: 51 additions & 0 deletions xoa/resource_xenorchestra_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,27 @@ func TestAccXenorchestraVm_createWithDisklessTemplateAndISO(t *testing.T) {
})
}

func TestAccXenorchestraVm_createWithoutDisks(t *testing.T) {
resourceName := "xenorchestra_vm.bar"
vmName := fmt.Sprintf("%s - %s", accTestPrefix, t.Name())
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckXenorchestraVmDestroy,
Steps: []resource.TestStep{
{
Config: testAccVmConfigWithoutDisks(vmName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "cdrom.#", "1"),
internal.TestCheckTypeSetElemAttrPair(resourceName, "cdrom.0.*", "data.xenorchestra_vdi.iso", "id"),
),
},
},
})
}

func TestAccXenorchestraVm_insertAndEjectCd(t *testing.T) {
resourceName := "xenorchestra_vm.bar"
vmName := fmt.Sprintf("%s - %s", accTestPrefix, t.Name())
Expand Down Expand Up @@ -1542,6 +1563,36 @@ resource "xenorchestra_vm" "bar" {
`, testIsoName, accTestPool.Id, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id)
}

func testAccVmConfigWithoutDisks(vmName string) string {
return testAccCloudConfigConfig(fmt.Sprintf("vm-template-%s", vmName), "template") + testAccNonDefaultTemplateConfig(disklessTestTemplate.NameLabel) + fmt.Sprintf(`
data "xenorchestra_vdi" "iso" {
name_label = "%s"
pool_id = "%s"
}

data "xenorchestra_network" "network" {
name_label = "%s"
pool_id = "%s"
}

resource "xenorchestra_vm" "bar" {
memory_max = 4295000000
cpus = 1
cloud_config = "${xenorchestra_cloud_config.bar.template}"
name_label = "%s"
name_description = "description"
template = "${data.xenorchestra_template.template.id}"
network {
network_id = "${data.xenorchestra_network.network.id}"
}

cdrom {
id = data.xenorchestra_vdi.iso.id
}
}
`, testIsoName, accTestPool.Id, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id)
}

func testAccVmConfigWithoutISO(vmName string) string {
return testAccCloudConfigConfig(fmt.Sprintf("vm-template-%s", vmName), "template") + testAccNonDefaultTemplateConfig(disklessTestTemplate.NameLabel) + fmt.Sprintf(`

Expand Down