Skip to content

Commit

Permalink
Fixes #409 - added huge pages to ovirt_vm resource (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
engelmi authored Jun 1, 2022
1 parent 065d26e commit f38d046
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/resources/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ resource "ovirt_vm" "test" {
- `cpu_mode` (String) Sets the CPU mode for the VM. Can be one of: custom, host_model, host_passthrough
- `cpu_sockets` (Number) Number of CPU sockets to allocate to the VM. If set, cpu_cores and cpu_threads must also be specified.
- `cpu_threads` (Number) Number of CPU threads to allocate to the VM. If set, cpu_cores and cpu_sockets must also be specified.
- `huge_pages` (Number) Sets the HugePages setting for the VM. Must be one of: 2048, 1048576
- `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.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/ovirt/go-ovirt-client v1.0.0-alpha10
github.com/ovirt/go-ovirt-client v1.0.0-beta1
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0
github.com/posener/complete v1.2.3 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/ovirt/go-ovirt v0.0.0-20220427092237-114c47f2835c h1:jXRFpl7+W0YZj/fghoYuE4vJWW/KeQGvdrhnRwRGtAY=
github.com/ovirt/go-ovirt v0.0.0-20220427092237-114c47f2835c/go.mod h1:Zkdj9/rW6eyuw0uOeEns6O3pP5G2ak+bI/tgkQ/tEZI=
github.com/ovirt/go-ovirt-client v1.0.0-alpha10 h1:V33VkDeuLfTEJVX7IQDQCR99ba53LF30ERfsM0Rsx9c=
github.com/ovirt/go-ovirt-client v1.0.0-alpha10/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client v1.0.0-beta1 h1:sjZncrFAMMFj4gDW2q50q2gm9dyKHUMKWLmbqYnVAU0=
github.com/ovirt/go-ovirt-client v1.0.0-beta1/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0 h1:uvACVHYhYPMkNJrrgWiABcfELB6qoFfsDDUTbpb4Jv4=
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0/go.mod h1:chKKxCv4lRjxezrTG+EIhkWXGhDAWByglPVXh/iYdnQ=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
33 changes: 33 additions & 0 deletions internal/ovirt/resource_ovirt_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ var vmSchema = map[string]*schema.Schema{
Optional: true,
Description: "If true, the VM is cloned from the template instead of linked. As a result, the template can be removed and the VM still exists.",
},
"huge_pages": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: "Sets the HugePages setting for the VM. Must be one of: " + strings.Join(vmHugePagesValues(), ", "),
ValidateDiagFunc: validateHugePages,
},
}

func cpuModeValues() []string {
Expand Down Expand Up @@ -228,6 +235,15 @@ func vmTypeValues() []string {
return result
}

func vmHugePagesValues() []string {
values := ovirtclient.VMHugePagesValues()
result := make([]string, len(values))
for i, value := range values {
result[i] = fmt.Sprintf("%d", value)
}
return result
}

func (p *provider) vmResource() *schema.Resource {
return &schema.Resource{
CreateContext: p.vmCreate,
Expand Down Expand Up @@ -272,6 +288,7 @@ func (p *provider) vmCreate(
handleVMSerialConsole,
handleVMClone,
handleVMInstanceTypeID,
handleVMHugePages,
} {
diags = f(client, data, params, diags)
}
Expand Down Expand Up @@ -510,6 +527,21 @@ func handleVMType(
return diags
}

func handleVMHugePages(
_ ovirtclient.Client,
data *schema.ResourceData,
params ovirtclient.BuildableVMParameters,
diags diag.Diagnostics,
) diag.Diagnostics {
if hugePages, ok := data.GetOk("huge_pages"); ok {
_, err := params.WithHugePages(ovirtclient.VMHugePages(hugePages.(int)))
if err != nil {
diags = append(diags, errorToDiag("set huge pages", err))
}
}
return diags
}

func handleVMCPUParameters(
_ ovirtclient.Client,
data *schema.ResourceData,
Expand Down Expand Up @@ -606,6 +638,7 @@ func handleVMInitialization(
diags = append(diags, errorToDiag("add Initialization parameters", err))
}
}

return diags
}

Expand Down
57 changes: 57 additions & 0 deletions internal/ovirt/resource_ovirt_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1038,3 +1038,60 @@ resource "ovirt_vm" "second_vm" {
)
}
}

func TestHugePages(t *testing.T) {
t.Parallel()

p := newProvider(newTestLogger(t))
testHelper := p.getTestHelper()
clusterID := testHelper.GetClusterID()

config := fmt.Sprintf(`
provider "ovirt" {
mock = true
}
data "ovirt_blank_template" "blank" {
}
resource "ovirt_vm" "test" {
template_id = data.ovirt_blank_template.blank.id
cluster_id = "%s"
name = "%s"
huge_pages = %d
}`,
clusterID,
p.getTestHelper().GenerateTestResourceName(t),
1048576)

resource.UnitTest(
t, resource.TestCase{
ProviderFactories: p.getProviderFactories(),
Steps: []resource.TestStep{
{
Config: config,
Check: func(state *terraform.State) error {
VMID := state.RootModule().Resources["ovirt_vm.test"].Primary.ID

client := testHelper.GetClient()
VM, err := client.GetVM(ovirtclient.VMID(VMID))
if err != nil {
return err
}
if VM.HugePages() == nil {
return fmt.Errorf("Expected huge pages to be set, but got <nil>")
}
if *VM.HugePages() != ovirtclient.VMHugePages1G {
return fmt.Errorf("Expected huge pages to be %d, but got %d",
ovirtclient.VMHugePages1G, *VM.HugePages())
}

return nil
},
},
{
Config: config,
Destroy: true,
},
},
},
)
}
15 changes: 15 additions & 0 deletions internal/ovirt/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,18 @@ func validateEnum(values []string) schema.SchemaValidateDiagFunc {
}
}
}

func validateHugePages(i interface{}, path cty.Path) diag.Diagnostics {
err := ovirtclient.VMHugePages(i.(int)).Validate()
if err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Not a valid huge page value.",
Detail: err.Error(),
AttributePath: path,
},
}
}
return nil
}

0 comments on commit f38d046

Please sign in to comment.