Skip to content

Commit

Permalink
Fixes #406 - introduce instance_type_id to ovirt_vm
Browse files Browse the repository at this point in the history
  • Loading branch information
eslutsky committed Jun 1, 2022
1 parent 4e7dfc7 commit eecab35
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions internal/ovirt/resource_ovirt_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -254,6 +260,7 @@ func (p *provider) vmCreate(
handleVMMemoryPolicy,
handleVMSerialConsole,
handleVMClone,
handleVMInstanceTypeID,
} {
diags = f(client, data, params, diags)
}
Expand Down Expand Up @@ -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,
Expand Down
62 changes: 62 additions & 0 deletions internal/ovirt/resource_ovirt_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eecab35

Please sign in to comment.