Skip to content

Commit

Permalink
Fixes #363: Add os_type parameter to ovirt_vm
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Bonic committed May 17, 2022
1 parent b906698 commit 3e6bc6e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/resources/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ resource "ovirt_vm" "test" {
- `cpu_cores` (Number) Number of CPU cores to allocate to the VM. If set, cpu_threads and cpu_sockets must also be specified.
- `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.
- `os_type` (String) Operating system type.

### Read-Only

Expand Down
17 changes: 16 additions & 1 deletion ovirt/resource_ovirt_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ var vmSchema = map[string]*schema.Schema{
Description: "Number of CPU sockets to allocate to the VM. If set, cpu_cores and cpu_threads must also be specified.",
ValidateDiagFunc: validatePositiveInt,
},
"os_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Operating system type.",
},
}

func (p *provider) vmResource() *schema.Resource {
Expand Down Expand Up @@ -117,7 +123,13 @@ func (p *provider) vmCreate(
return errorToDiags("add CPU parameters", err)
}
}

if osType, ok := data.GetOk("os_type"); ok {
osParams, err := ovirtclient.NewVMOSParameters().WithType(osType.(string))
if err != nil {
errorToDiags("add OS type to VM", err)
}
params.WithOS(osParams)
}
vm, err := client.CreateVM(
ovirtclient.ClusterID(clusterID),
ovirtclient.TemplateID(templateID),
Expand Down Expand Up @@ -170,6 +182,9 @@ func vmResourceUpdate(vm ovirtclient.VMData, data *schema.ResourceData) diag.Dia
diags = setResourceField(data, "name", vm.Name(), diags)
diags = setResourceField(data, "comment", vm.Comment(), diags)
diags = setResourceField(data, "status", vm.Status(), diags)
if _, ok := data.GetOk("os_type"); ok || vm.OS().Type() != "other" {
diags = setResourceField(data, "os_type", vm.OS().Type(), diags)
}
return diags
}

Expand Down
70 changes: 67 additions & 3 deletions ovirt/resource_ovirt_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ provider "ovirt" {
}
resource "ovirt_vm" "foo" {
cluster_id = "%s"
cluster_id = "%s"
template_id = "%s"
name = "test"
name = "test"
}
`,
clusterID,
Expand Down Expand Up @@ -56,6 +56,11 @@ resource "ovirt_vm" "foo" {
"name",
regexp.MustCompile("^test$"),
),
resource.TestMatchResourceAttr(
"ovirt_vm.foo",
"os_type",
regexp.MustCompile("^$"),
),
),
},
{
Expand Down Expand Up @@ -135,13 +140,60 @@ resource "ovirt_vm" "foo" {
)
}

func TestVMResourceOSType(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"
os_type = "rhcos_x64"
}
`,
clusterID,
templateID,
)

resource.UnitTest(
t, resource.TestCase{
ProviderFactories: p.getProviderFactories(),
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"ovirt_vm.foo",
"os_type",
regexp.MustCompile("^rhcos_x64$"),
),
),
},
{
Config: config,
Destroy: true,
},
},
},
)
}

type testVM struct {
id ovirtclient.VMID
name string
comment string
clusterID ovirtclient.ClusterID
templateID ovirtclient.TemplateID
status ovirtclient.VMStatus
os ovirtclient.VMOS
}

func (t *testVM) InstanceTypeID() *ovirtclient.InstanceTypeID {
Expand All @@ -153,7 +205,7 @@ func (t *testVM) VMType() ovirtclient.VMType {
}

func (t *testVM) OS() ovirtclient.VMOS {
panic("not implemented for test input")
return t.os
}

func (t *testVM) Memory() int64 {
Expand Down Expand Up @@ -242,6 +294,14 @@ func (t *testVM) Status() ovirtclient.VMStatus {
return t.status
}

type testOS struct {
t string
}

func (t testOS) Type() string {
return t.t
}

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

Expand All @@ -252,6 +312,9 @@ func TestVMResourceUpdate(t *testing.T) {
clusterID: "cluster-1",
templateID: "template-1",
status: ovirtclient.VMStatusUp,
os: &testOS{
t: "linux",
},
}
resourceData := schema.TestResourceDataRaw(t, vmSchema, map[string]interface{}{})
diags := vmResourceUpdate(vm, resourceData)
Expand All @@ -263,6 +326,7 @@ func TestVMResourceUpdate(t *testing.T) {
compareResource(t, resourceData, "cluster_id", string(vm.clusterID))
compareResource(t, resourceData, "template_id", string(vm.templateID))
compareResource(t, resourceData, "status", string(vm.status))
compareResource(t, resourceData, "os_type", vm.os.Type())
}

func compareResource(t *testing.T, data *schema.ResourceData, field string, value string) {
Expand Down

0 comments on commit 3e6bc6e

Please sign in to comment.