From 50ba6173454ca4efbc6da0e632c806eb0c386e17 Mon Sep 17 00:00:00 2001 From: Michael Engel Date: Tue, 31 May 2022 14:20:31 +0200 Subject: [PATCH] Fixes #407 - implemented vm_type on ovirt_vm resource (#413) --- docs/resources/vm.md | 1 + internal/ovirt/resource_ovirt_vm.go | 35 ++++++++++++++++ internal/ovirt/resource_ovirt_vm_test.go | 53 ++++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/docs/resources/vm.md b/docs/resources/vm.md index 7ea1c044..1a84ae12 100644 --- a/docs/resources/vm.md +++ b/docs/resources/vm.md @@ -51,6 +51,7 @@ resource "ovirt_vm" "test" { - `placement_policy_host_ids` (Set of String) List of hosts to pin the VM to. - `serial_console` (Boolean) Enable or disable the serial console. - `template_disk_attachment_override` (Block Set) Override parameters for disks obtained from templates. (see [below for nested schema](#nestedblock--template_disk_attachment_override)) +- `vm_type` (String) Virtual machine type. Must be one of: desktop, server, high_performance ### Read-Only diff --git a/internal/ovirt/resource_ovirt_vm.go b/internal/ovirt/resource_ovirt_vm.go index 566ed853..f40b6232 100644 --- a/internal/ovirt/resource_ovirt_vm.go +++ b/internal/ovirt/resource_ovirt_vm.go @@ -91,6 +91,13 @@ var vmSchema = map[string]*schema.Schema{ ForceNew: true, Description: "Operating system type.", }, + "vm_type": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Description: "Virtual machine type. Must be one of: " + strings.Join(vmTypeValues(), ", "), + ValidateDiagFunc: validateEnum(vmTypeValues()), + }, "placement_policy_affinity": { Type: schema.TypeString, Optional: true, @@ -195,6 +202,15 @@ func vmAffinityValues() []string { return result } +func vmTypeValues() []string { + values := ovirtclient.VMTypeValues() + result := make([]string, len(values)) + for i, value := range values { + result[i] = string(value) + } + return result +} + func (p *provider) vmResource() *schema.Resource { return &schema.Resource{ CreateContext: p.vmCreate, @@ -230,6 +246,7 @@ func (p *provider) vmCreate( handleVMComment, handleVMCPUParameters, handleVMOSType, + handleVMType, handleVMInitialization, handleVMPlacementPolicy, handleTemplateDiskAttachmentOverride, @@ -460,6 +477,21 @@ func handleVMOSType( return diags } +func handleVMType( + _ ovirtclient.Client, + data *schema.ResourceData, + params ovirtclient.BuildableVMParameters, + diags diag.Diagnostics, +) diag.Diagnostics { + if vmType, ok := data.GetOk("vm_type"); ok { + _, err := params.WithVMType(ovirtclient.VMType(vmType.(string))) + if err != nil { + diags = append(diags, errorToDiag("set VM type", err)) + } + } + return diags +} + func handleVMCPUParameters( _ ovirtclient.Client, data *schema.ResourceData, @@ -598,6 +630,9 @@ func vmResourceUpdate(vm ovirtclient.VMData, data *schema.ResourceData) diag.Dia if _, ok := data.GetOk("os_type"); ok || vm.OS().Type() != "other" { diags = setResourceField(data, "os_type", vm.OS().Type(), diags) } + if _, ok := data.GetOk("vm_type"); ok { + diags = setResourceField(data, "vm_type", vm.VMType(), diags) + } if pp, ok := vm.PlacementPolicy(); ok { diags = setResourceField(data, "placement_policy_host_ids", pp.HostIDs(), diags) diags = setResourceField(data, "placement_policy_affinity", pp.Affinity(), diags) diff --git a/internal/ovirt/resource_ovirt_vm_test.go b/internal/ovirt/resource_ovirt_vm_test.go index 4edb1b9d..a538976c 100644 --- a/internal/ovirt/resource_ovirt_vm_test.go +++ b/internal/ovirt/resource_ovirt_vm_test.go @@ -187,6 +187,59 @@ resource "ovirt_vm" "foo" { }, ) } + +func TestVMResourceVMType(t *testing.T) { + t.Parallel() + + p := newProvider(newTestLogger(t)) + clusterID := p.getTestHelper().GetClusterID() + templateID := p.getTestHelper().GetBlankTemplateID() + config := fmt.Sprintf( + ` +provider "ovirt" { + mock = true +} + +resource "ovirt_vm" "foo" { + cluster_id = "%s" + template_id = "%s" + name = "test" + vm_type = "server" +} +`, + clusterID, + templateID, + ) + + resource.UnitTest( + t, resource.TestCase{ + ProviderFactories: p.getProviderFactories(), + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + func(state *terraform.State) error { + VMID := state.RootModule().Resources["ovirt_vm.foo"].Primary.ID + vm, err := p.getTestHelper().GetClient().GetVM(ovirtclient.VMID(VMID)) + if err != nil { + return fmt.Errorf("Failed to get VM: %w", err) + } + if vm.VMType() != ovirtclient.VMTypeServer { + return fmt.Errorf("Expected VM type to be %s, but got %s", ovirtclient.VMTypeServer, vm.VMType()) + } + return nil + }, + ), + }, + { + Config: config, + Destroy: true, + }, + }, + }, + ) +} + func TestVMResourceInitialization(t *testing.T) { t.Parallel()