Skip to content

Commit

Permalink
Fixes #365: implemented template_disk_attachment_override parameter f…
Browse files Browse the repository at this point in the history
…or ovirt_vm
  • Loading branch information
Janos Bonic committed May 24, 2022
1 parent 39735aa commit 46fc5fe
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "ovirt_template_disks Data Source - terraform-provider-ovirt"
page_title: "ovirt_template_disk_attachments Data Source - terraform-provider-ovirt"
subcategory: ""
description: |-
A set of all disk attachments of a template.
---

# ovirt_template_disks (Data Source)
# ovirt_template_disk_attachments (Data Source)

A set of all disk attachments of a template.

## Example Usage

```terraform
data "ovirt_template_disks" "list" {
data "ovirt_template_disk_attachments" "list" {
template_id = ovirt_template.blueprint.id
depends_on = [ovirt_template.blueprint]
}
output "attachment_list" {
value = data.ovirt_template_disks.list
value = data.ovirt_template_disk_attachments.list
}
```

Expand Down
13 changes: 13 additions & 0 deletions docs/resources/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,25 @@ resource "ovirt_vm" "test" {
- `os_type` (String) Operating system type.
- `placement_policy_affinity` (String) Affinity for placement policies. Must be one of: migratable, pinned, user_migratable
- `placement_policy_host_ids` (Set of String) List of hosts to pin the VM to.
- `template_disk_attachment_override` (Block Set) Override parameters for disks obtained from templates. (see [below for nested schema](#nestedblock--template_disk_attachment_override))

### Read-Only

- `id` (String) oVirt ID of this VM.
- `status` (String) Status of the virtual machine. One of: `down`, `image_locked`, `migrating`, `not_responding`, `paused`, `powering_down`, `powering_up`, `reboot_in_progress`, `restoring_state`, `saving_state`, `suspended`, `unassigned`, `unknown`, `up`, `wait_for_launch`.

<a id="nestedblock--template_disk_attachment_override"></a>
### Nested Schema for `template_disk_attachment_override`

Required:

- `disk_id` (String) ID of the disk to be changed.

Optional:

- `format` (String) Disk format for the override. Can be 'raw' or 'cow'.
- `sparse` (Boolean) Sparse-provision the disk.

## Import

Import is supported using the following syntax:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
data "ovirt_template_disks" "list" {
data "ovirt_template_disk_attachments" "list" {
template_id = ovirt_template.blueprint.id

depends_on = [ovirt_template.blueprint]
}

output "attachment_list" {
value = data.ovirt_template_disks.list
value = data.ovirt_template_disk_attachments.list
}
4 changes: 2 additions & 2 deletions ovirt/data_source_ovirt_template_disk_attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ func TestTemplateDiskAttachmentsDataSource(t *testing.T) {
%s
%s
data "ovirt_template_disks" "list" {
data "ovirt_template_disk_attachments" "list" {
template_id = ovirt_template.blueprint.id
depends_on = [ovirt_template.blueprint]
}
output "attachment_list" {
value = data.ovirt_template_disks.list
value = data.ovirt_template_disk_attachments.list
}`,
configBaseVM, configTemplate)

Expand Down
8 changes: 4 additions & 4 deletions ovirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ func (p *provider) getProvider() *schema.Provider {
"ovirt_template": p.templateResource(),
},
DataSourcesMap: map[string]*schema.Resource{
"ovirt_blank_template": p.blankTemplateDataSource(),
"ovirt_disk_attachments": p.diskAttachmentsDataSource(),
"ovirt_template_disks": p.templateDiskAttachmentsDataSource(),
"ovirt_cluster_hosts": p.clusterHostsDataSource(),
"ovirt_blank_template": p.blankTemplateDataSource(),
"ovirt_disk_attachments": p.diskAttachmentsDataSource(),
"ovirt_template_disk_attachments": p.templateDiskAttachmentsDataSource(),
"ovirt_cluster_hosts": p.clusterHostsDataSource(),
},
}
}
Expand Down
78 changes: 77 additions & 1 deletion ovirt/resource_ovirt_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,36 @@ var vmSchema = map[string]*schema.Schema{
Type: schema.TypeString,
},
},
"template_disk_attachment_override": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Description: "Override parameters for disks obtained from templates.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"disk_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of the disk to be changed.",
ValidateDiagFunc: validateUUID,
},
"format": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Disk format for the override. Can be 'raw' or 'cow'.",
ValidateDiagFunc: validateFormat,
},
"sparse": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: "Sparse-provision the disk.",
},
},
},
},
}

func vmAffinityValues() []string {
Expand Down Expand Up @@ -134,7 +164,11 @@ func (p *provider) vmCreate(
ovirtclient.BuildableVMParameters,
diag.Diagnostics,
) diag.Diagnostics{
handleVMComment, handleVMCPUParameters, handleVMOSType, handleVMPlacementPolicy,
handleVMComment,
handleVMCPUParameters,
handleVMOSType,
handleVMPlacementPolicy,
handleTemplateDiskAttachmentOverride,
} {
diags = f(data, params, diags)
}
Expand All @@ -158,6 +192,48 @@ func (p *provider) vmCreate(
return vmResourceUpdate(vm, data)
}

func handleTemplateDiskAttachmentOverride(
data *schema.ResourceData,
params ovirtclient.BuildableVMParameters,
diags diag.Diagnostics,
) diag.Diagnostics {
templateDiskAttachments, ok := data.GetOk("template_disk_attachment_override")
if !ok {
return diags
}
templateDiskAttachmentsSet := templateDiskAttachments.(*schema.Set)
disks := make([]ovirtclient.OptionalVMDiskParameters, len(templateDiskAttachmentsSet.List()))
for i, item := range templateDiskAttachmentsSet.List() {
entry := item.(map[string]interface{})
diskID := entry["disk_id"].(string)
disk, err := ovirtclient.NewBuildableVMDiskParameters(ovirtclient.DiskID(diskID))
if err != nil {
diags = append(diags, errorToDiag("add disk to VM", err))
return diags
}
if formatRaw, ok := entry["format"]; ok {
disk, err = disk.WithFormat(ovirtclient.ImageFormat(formatRaw.(string)))
if err != nil {
diags = append(diags, errorToDiag("set format on disk", err))
return diags
}
}
if sparseRaw, ok := entry["sparse"]; ok {
disk, err = disk.WithSparse(sparseRaw.(bool))
if err != nil {
diags = append(diags, errorToDiag("set sparse on disk", err))
return diags
}
}
disks[i] = disk
}
_, err := params.WithDisks(disks)
if err != nil {
diags = append(diags, errorToDiag("set disks on VM", err))
}
return diags
}

func handleVMPlacementPolicy(
data *schema.ResourceData,
params ovirtclient.BuildableVMParameters,
Expand Down
103 changes: 103 additions & 0 deletions ovirt/resource_ovirt_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,106 @@ func compareResourceStringList(t *testing.T, data *schema.ResourceData, field st
}
}
}

func TestVMOverrideDisk(t *testing.T) {
t.Parallel()

p := newProvider(newTestLogger(t))
testHelper := p.getTestHelper()
clusterID := testHelper.GetClusterID()
templateID := testHelper.GetBlankTemplateID()
storageDomainID := testHelper.GetStorageDomainID()
config := fmt.Sprintf(
`
provider "ovirt" {
mock = true
}
resource "ovirt_vm" "source" {
cluster_id = "%s"
template_id = "%s"
name = "%s"
}
resource "ovirt_disk" "source" {
storagedomain_id = "%s"
format = "cow"
size = 1048576
alias = "test"
sparse = false
}
resource "ovirt_disk_attachment" "source" {
vm_id = ovirt_vm.source.id
disk_id = ovirt_disk.source.id
disk_interface = "virtio_scsi"
}
resource "ovirt_template" "source" {
vm_id = ovirt_disk_attachment.source.vm_id
name = "%s"
}
data "ovirt_template_disk_attachments" "source" {
template_id = ovirt_template.source.id
}
resource "ovirt_vm" "one" {
template_id = ovirt_template.source.id
cluster_id = "%s"
name = "%s"
dynamic "template_disk_attachment_override" {
for_each = data.ovirt_template_disk_attachments.source.disk_attachments
content {
disk_id = template_disk_attachment_override.value["disk_id"]
format = "raw"
sparse = true
}
}
}
`,
clusterID,
templateID,
p.getTestHelper().GenerateTestResourceName(t),
storageDomainID,
p.getTestHelper().GenerateTestResourceName(t),
clusterID,
p.getTestHelper().GenerateTestResourceName(t),
)

resource.UnitTest(
t, resource.TestCase{
ProviderFactories: p.getProviderFactories(),
Steps: []resource.TestStep{
{
Config: config,
Check: func(state *terraform.State) error {
client := testHelper.GetClient()
vmID := state.RootModule().Resources["ovirt_vm.one"].Primary.ID
diskAttachments, err := client.ListDiskAttachments(ovirtclient.VMID(vmID))
if err != nil {
return err
}
diskAttachment := diskAttachments[0]
disk, err := diskAttachment.Disk()
if err != nil {
return err
}
if disk.Format() != ovirtclient.ImageFormatRaw {
return fmt.Errorf("incorrect disk format: %s", disk.Format())
}
if !disk.Sparse() {
return fmt.Errorf("disk incorrectly created as sparse")
}
return nil
},
},
{
Config: config,
Destroy: true,
},
},
},
)
}

0 comments on commit 46fc5fe

Please sign in to comment.