diff --git a/internal/ovirt/resource_ovirt_vm.go b/internal/ovirt/resource_ovirt_vm.go index 438814a2..dc3eef66 100644 --- a/internal/ovirt/resource_ovirt_vm.go +++ b/internal/ovirt/resource_ovirt_vm.go @@ -164,6 +164,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, + }, } func vmAffinityValues() []string { @@ -216,6 +222,7 @@ func (p *provider) vmCreate( handleVMMemory, handleVMMemoryPolicy, handleVMSerialConsole, + handleVMInstanceTypeID, } { diags = f(client, data, params, diags) } @@ -521,6 +528,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 8a2cfd29..a88f0c18 100644 --- a/internal/ovirt/resource_ovirt_vm_test.go +++ b/internal/ovirt/resource_ovirt_vm_test.go @@ -371,6 +371,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