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 storage_domain_id to template_disk_attachment_override #511

Open
wants to merge 1 commit into
base: main
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
1 change: 1 addition & 0 deletions docs/resources/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Optional:

- `format` (String) Disk format for the override. Can be 'raw' or 'cow'.
- `provisioning` (String) Provisioning the disk. Must be one of sparse,non-sparse
- `storage_domain_id` (String) ID of the storage domain where the new disk will be placed.

## Import

Expand Down
15 changes: 15 additions & 0 deletions internal/ovirt/resource_ovirt_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ var vmSchema = map[string]*schema.Schema{
Description: fmt.Sprintf("Provisioning the disk. Must be one of %s", strings.Join(provisioningValues(), ",")),
ValidateDiagFunc: validateEnum(provisioningValues()),
},
"storage_domain_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "ID of the storage domain where the new disk will be placed.",
ValidateDiagFunc: validateUUID,
},
},
},
},
Expand Down Expand Up @@ -417,6 +424,14 @@ func handleTemplateDiskAttachmentOverride(
return diags
}
}
if storageDomainID, ok := entry["storage_domain_id"]; ok && storageDomainID != "" {
storageDomainObj := ovirtclient.StorageDomainID(storageDomainID.(string))
disk, err = disk.WithStorageDomainID(storageDomainObj)
if err != nil {
diags = append(diags, errorToDiag("set storage domain id", err))
return diags
}
}
disks[i] = disk
}
_, err := params.WithDisks(disks)
Expand Down
59 changes: 41 additions & 18 deletions internal/ovirt/resource_ovirt_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ func TestVMOverrideDisk(t *testing.T) {
clusterID := testHelper.GetClusterID()
templateID := testHelper.GetBlankTemplateID()
storageDomainID := testHelper.GetStorageDomainID()
secondaryStorageDomainID := testHelper.GetSecondaryStorageDomainID(t)

baseConfig := fmt.Sprintf(
`
Expand Down Expand Up @@ -731,37 +732,54 @@ func TestVMOverrideDisk(t *testing.T) {
template_id = ovirt_template.source.id
cluster_id = "%s"
name = "%s"
clone = true

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 = %s
provisioning = %s
disk_id = template_disk_attachment_override.value["disk_id"]
format = %s
provisioning = %s
storage_domain_id = %s
}
}
}`

testcases := []struct {
name string
inputFormat string
inputProvisioning string
expectedFormat ovirtclient.ImageFormat
expectedSparse bool
name string
inputFormat string
inputProvisioning string
inputStorageDomainID string
expectedFormat ovirtclient.ImageFormat
expectedSparse bool
expectedStorageDomainID string
}{
{
name: "override sparse",
inputFormat: "null",
inputProvisioning: "\"sparse\"",
expectedFormat: ovirtclient.ImageFormatCow,
expectedSparse: true,
name: "override sparse",
inputFormat: "null",
inputProvisioning: "\"sparse\"",
inputStorageDomainID: "null",
expectedFormat: ovirtclient.ImageFormatCow,
expectedSparse: true,
expectedStorageDomainID: string(storageDomainID),
},
{
name: "override format",
inputFormat: "\"raw\"",
inputProvisioning: "null",
expectedFormat: ovirtclient.ImageFormatRaw,
expectedSparse: false,
name: "override format",
inputFormat: "\"raw\"",
inputProvisioning: "null",
inputStorageDomainID: "null",
expectedFormat: ovirtclient.ImageFormatRaw,
expectedSparse: false,
expectedStorageDomainID: string(storageDomainID),
},
{
name: "set storage_domain_id",
inputFormat: "null",
inputProvisioning: "null",
inputStorageDomainID: "\"" + string(secondaryStorageDomainID) + "\"",
expectedFormat: ovirtclient.ImageFormatCow,
expectedSparse: false,
expectedStorageDomainID: string(secondaryStorageDomainID),
},
}

Expand All @@ -772,6 +790,7 @@ func TestVMOverrideDisk(t *testing.T) {
p.getTestHelper().GenerateTestResourceName(t),
testcase.inputFormat,
testcase.inputProvisioning,
testcase.inputStorageDomainID,
)

resource.UnitTest(
Expand All @@ -798,6 +817,10 @@ func TestVMOverrideDisk(t *testing.T) {
if disk.Sparse() != testcase.expectedSparse {
return fmt.Errorf("disk incorrectly created as sparse")
}
storageDomainID := disk.StorageDomainIDs()[0]
if string(storageDomainID) != testcase.expectedStorageDomainID {
return fmt.Errorf("disk not created on correct storage domain")
}
return nil
},
},
Expand Down