Skip to content

Commit

Permalink
Fixes #407 - implemented vm_type on ovirt_vm resource (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
engelmi authored May 31, 2022
1 parent 8f72e7a commit c9f0b00
Show file tree
Hide file tree
Showing 3 changed files with 89 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 @@ -50,6 +50,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

Expand Down
35 changes: 35 additions & 0 deletions internal/ovirt/resource_ovirt_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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,
Expand Down Expand Up @@ -175,6 +182,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,
Expand Down Expand Up @@ -210,6 +226,7 @@ func (p *provider) vmCreate(
handleVMComment,
handleVMCPUParameters,
handleVMOSType,
handleVMType,
handleVMInitialization,
handleVMPlacementPolicy,
handleTemplateDiskAttachmentOverride,
Expand Down Expand Up @@ -422,6 +439,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,
Expand Down Expand Up @@ -557,6 +589,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)
Expand Down
53 changes: 53 additions & 0 deletions internal/ovirt/resource_ovirt_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit c9f0b00

Please sign in to comment.