Skip to content

Commit

Permalink
Merge branch 'main' into 345-data-source-cluster-hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
eslutsky authored May 17, 2022
2 parents 0a8ac77 + 3e6bc6e commit b52b855
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 46 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
84 changes: 42 additions & 42 deletions ovirt/resource_ovirt_disk_from_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,48 +51,48 @@ func (p *provider) diskFromImageCreate(ctx context.Context, data *schema.Resourc
if err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Invalid alias value.",
Detail: err.Error(),
},
}
}
}
if sparse, ok := data.GetOk("sparse"); ok {
params, err = params.WithSparse(sparse.(bool))
if err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Invalid sparse value.",
Detail: err.Error(),
},
}
}
}
sourceFile := data.Get("source_file").(string)
// We actually want to include the file here, so this is not gosec-relevant.
fh, err := os.Open(sourceFile) //nolint:gosec
if err != nil {
return errorToDiags(fmt.Sprintf("opening file %s", sourceFile), err)
}
stat, err := fh.Stat()
if err != nil {
return errorToDiags(fmt.Sprintf("opening file %s", sourceFile), err)
}
upload, err := client.UploadToNewDisk(
ovirtclient.StorageDomainID(storageDomainID),
ovirtclient.ImageFormat(format),
uint64(stat.Size()),
params,
fh,
)
var disk ovirtclient.Disk
if upload != nil {
disk = upload.Disk()
}
if err != nil {
diags := diag.Diagnostics{
Severity: diag.Error,
Summary: "Invalid alias value.",
Detail: err.Error(),
},
}
}
}
if sparse, ok := data.GetOk("sparse"); ok {
params, err = params.WithSparse(sparse.(bool))
if err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Invalid sparse value.",
Detail: err.Error(),
},
}
}
}
sourceFile := data.Get("source_file").(string)
// We actually want to include the file here, so this is not gosec-relevant.
fh, err := os.Open(sourceFile) //nolint:gosec
if err != nil {
return errorToDiags(fmt.Sprintf("opening file %s", sourceFile), err)
}
stat, err := fh.Stat()
if err != nil {
return errorToDiags(fmt.Sprintf("opening file %s", sourceFile), err)
}
upload, err := client.UploadToNewDisk(
ovirtclient.StorageDomainID(storageDomainID),
ovirtclient.ImageFormat(format),
uint64(stat.Size()),
params,
fh,
)
var disk ovirtclient.Disk
if upload != nil {
disk = upload.Disk()
}
if err != nil {
diags := diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to create disk.",
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 b52b855

Please sign in to comment.