From eecab350c71fcc7c620c615cd2d75cb6b837d517 Mon Sep 17 00:00:00 2001 From: Evgeny Slutsky Date: Tue, 31 May 2022 11:01:34 +0300 Subject: [PATCH] Fixes #406 - introduce instance_type_id to ovirt_vm --- docs/resources/vm.md | 1 + internal/ovirt/resource_ovirt_vm.go | 22 +++++++++ internal/ovirt/resource_ovirt_vm_test.go | 62 ++++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/docs/resources/vm.md b/docs/resources/vm.md index 1a84ae12..60d9eead 100644 --- a/docs/resources/vm.md +++ b/docs/resources/vm.md @@ -43,6 +43,7 @@ resource "ovirt_vm" "test" { - `cpu_threads` (Number) Number of CPU threads to allocate to the VM. If set, cpu_cores and cpu_sockets must also be specified. - `initialization_custom_script` (String) Custom script that passed to VM during initialization. - `initialization_hostname` (String) hostname that is set during initialization. +- `instance_type_id` (String) Defines the VM instance type ID overrides the hardware parameters of the created VM. - `maximum_memory` (Number) Maximum memory to assign to the VM in the memory policy in bytes. - `memory` (Number) Memory to assign to the VM in bytes. - `memory_ballooning` (Boolean) Turn memory ballooning on or off for the VM. diff --git a/internal/ovirt/resource_ovirt_vm.go b/internal/ovirt/resource_ovirt_vm.go index f40b6232..e8e1f639 100644 --- a/internal/ovirt/resource_ovirt_vm.go +++ b/internal/ovirt/resource_ovirt_vm.go @@ -177,6 +177,12 @@ var vmSchema = map[string]*schema.Schema{ Optional: true, Description: "Enable or disable the serial console.", }, + "instance_type_id": { + Type: schema.TypeString, + Optional: true, + Description: "Defines the VM instance type ID overrides the hardware parameters of the created VM.", + ValidateDiagFunc: validateUUID, + }, "clone": { Type: schema.TypeBool, Optional: true, @@ -254,6 +260,7 @@ func (p *provider) vmCreate( handleVMMemoryPolicy, handleVMSerialConsole, handleVMClone, + handleVMInstanceTypeID, } { diags = f(client, data, params, diags) } @@ -591,6 +598,21 @@ func handleVMInitialization( return diags } +func handleVMInstanceTypeID( + _ ovirtclient.Client, + data *schema.ResourceData, + params ovirtclient.BuildableVMParameters, + diags diag.Diagnostics, +) diag.Diagnostics { + if instanceTypeID, ok := data.GetOk("instance_type_id"); ok { + _, err := params.WithInstanceTypeID(ovirtclient.InstanceTypeID(instanceTypeID.(string))) + if err != nil { + diags = append(diags, errorToDiag("set instance_type_id on VM", err)) + } + } + return diags +} + func (p *provider) vmRead( ctx context.Context, data *schema.ResourceData, diff --git a/internal/ovirt/resource_ovirt_vm_test.go b/internal/ovirt/resource_ovirt_vm_test.go index 4babd37e..90c9f3b0 100644 --- a/internal/ovirt/resource_ovirt_vm_test.go +++ b/internal/ovirt/resource_ovirt_vm_test.go @@ -424,6 +424,68 @@ resource "ovirt_vm" "foo" { ) } +func TestVMResourceInstanceTypeID(t *testing.T) { + t.Parallel() + + p := newProvider(newTestLogger(t)) + clusterID := p.getTestHelper().GetClusterID() + templateID := p.getTestHelper().GetBlankTemplateID() + client := p.getTestHelper().GetClient().WithContext(context.Background()) + + instanceTypes, err := client.ListInstanceTypes() + testHelper := p.getTestHelper() + + if err != nil { + t.Fatalf("Failed to list instance types (%v)", err) + } + instanceType := instanceTypes[0].ID() + + config := fmt.Sprintf( + ` +provider "ovirt" { + mock = true +} + +resource "ovirt_vm" "foo" { + cluster_id = "%s" + template_id = "%s" + name = "test" + instance_type_id = "%s" +} +`, + clusterID, + templateID, + instanceType, + ) + + 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.foo"].Primary.ID + vm, err := client.GetVM(ovirtclient.VMID(vmID)) + if err != nil { + return err + } + if *vm.InstanceTypeID() != instanceType { + return fmt.Errorf("incorrect value for instance Type ID: %s ", *vm.InstanceTypeID()) + } + return nil + }, + }, + { + Config: config, + Destroy: true, + }, + }, + }, + ) +} + type testVM struct { id ovirtclient.VMID name string