Skip to content

Commit

Permalink
Fix examples (#395)
Browse files Browse the repository at this point in the history
* Fixed examples running

Co-authored-by: Janos Bonic <[email protected]>
  • Loading branch information
eslutsky and Janos Bonic authored May 25, 2022
1 parent d6375e9 commit 7b956a8
Show file tree
Hide file tree
Showing 18 changed files with 317 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dist
**/.terraform/*
crash.log
crash.*.log
testimage/full.qcow
2 changes: 1 addition & 1 deletion docs/resources/disk_from_image.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ resource "ovirt_disk_from_image" "test" {
format = "raw"
alias = "test"
sparse = true
source_file = "./testimage/testimage"
source_file = "./testimage/image"
}
```

Expand Down
5 changes: 5 additions & 0 deletions docs/resources/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ 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.
- `initialization_custom_script` (String) Custom script that passed to VM during initialization.
- `initialization_hostname` (String) hostname that is set during initialization.
- `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.
- `os_type` (String) Operating system type.
- `placement_policy_affinity` (String) Affinity for placement policies. Must be one of: migratable, pinned, user_migratable
- `placement_policy_host_ids` (Set of String) List of hosts to pin the VM to.
Expand Down
6 changes: 3 additions & 3 deletions docs/resources/vm_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ The ovirt_vm_start resource starts a VM in oVirt when created and stops the VM w
data "ovirt_blank_template" "blank" {
}
resource "ovirt_disk" "test" {
resource "ovirt_disk_from_image" "test" {
storagedomain_id = var.storagedomain_id
format = "raw"
size = 1048576
alias = "test"
sparse = true
source_file = "./testimage/image"
}
resource "ovirt_vm" "test" {
Expand All @@ -32,7 +32,7 @@ resource "ovirt_vm" "test" {
resource "ovirt_disk_attachment" "test" {
vm_id = ovirt_vm.test.id
disk_id = ovirt_disk.test.id
disk_id = ovirt_disk_from_image.test.id
disk_interface = "virtio_scsi"
}
Expand Down
1 change: 0 additions & 1 deletion examples/data-sources/ovirt_blank_template/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ provider "ovirt" {
tls_ca_dirs = var.tls_ca_dirs
tls_ca_files = var.tls_ca_files
tls_insecure = var.tls_insecure
mock = var.mock
}

1 change: 0 additions & 1 deletion examples/data-sources/ovirt_cluster_hosts/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ provider "ovirt" {
tls_ca_dirs = var.tls_ca_dirs
tls_ca_files = var.tls_ca_files
tls_insecure = var.tls_insecure
mock = var.mock
}

1 change: 0 additions & 1 deletion examples/data-sources/ovirt_disk_attachments/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ provider "ovirt" {
tls_ca_dirs = var.tls_ca_dirs
tls_ca_files = var.tls_ca_files
tls_insecure = var.tls_insecure
mock = var.mock
}

30 changes: 23 additions & 7 deletions examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
Expand Down Expand Up @@ -50,8 +51,9 @@ func TestExamples(t *testing.T) {
}
t.Run(
"provider", func(t *testing.T) {
env := startTerraform(t, "provider")
runTerraform(
t, "provider", tfVars,
t, "provider", tfVars, env,
)
},
)
Expand All @@ -67,8 +69,13 @@ func TestExamples(t *testing.T) {
if e.IsDir() {
t.Run(
e.Name(), func(t *testing.T) {
dir, err := filepath.Abs(path.Join(category, e.Name()))
if err != nil {
t.Fatalf("Failed to find absolute path for directory (%v)", err)
}
env := startTerraform(t, dir)
runTerraform(
t, path.Join(category, e.Name()), tfVars,
t, dir, tfVars, env,
)
},
)
Expand Down Expand Up @@ -115,17 +122,22 @@ func runTerraformCommand(t *testing.T, dir string, env []string, vars interface{
t.Logf("Successfully ran terrafom %s.", strings.Join(args, " "))
}

func runTerraform(t *testing.T, dir string, vars tfvars) {
func startTerraform(t *testing.T, dir string) []string {
t.Logf("Starting Terraform provider...")
cmd := exec.Command("go", "run", "../main.go", "-debug")
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to obtain current working directory (%v)", err)
}
cmdLine := []string{"go", "run", path.Join(cwd, "..", "main.go"), "-debug"}
cmd := exec.Command(cmdLine[0], cmdLine[1:]...)
logger := &tfReattachCaptureLogger{
t: t,
ready: make(chan string),
error: make(chan error, 1),
prefix: "[go run ../main.go -debug] ",
prefix: "[go run main.go -debug] ",
lock: &sync.Mutex{},
}

cmd.Dir = dir
cmd.Stderr = logger
cmd.Stdout = logger
go func() {
Expand Down Expand Up @@ -155,7 +167,7 @@ func runTerraform(t *testing.T, dir string, vars tfvars) {
t.Fatalf("Terraform provider closed the error channel without any output.")
}
t.Fatalf("Terraform provider exited with an error: %v", err)
case <-time.After(10 * time.Second):
case <-time.After(60 * time.Second):
t.Fatalf("Timeout while waiting for the TF_REATTACH_PROVIDERS line in the output.")
}
t.Cleanup(
Expand All @@ -166,6 +178,10 @@ func runTerraform(t *testing.T, dir string, vars tfvars) {
}
},
)
return env
}

func runTerraform(t *testing.T, dir string, vars tfvars, env []string) {
runTerraformCommand(t, dir, env, nil, "init")
t.Cleanup(
func() {
Expand Down
2 changes: 1 addition & 1 deletion examples/resources/ovirt_disk_from_image/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ resource "ovirt_disk_from_image" "test" {
format = "raw"
alias = "test"
sparse = true
source_file = "./testimage/testimage"
source_file = "./testimage/image"
}
6 changes: 3 additions & 3 deletions examples/resources/ovirt_vm_start/resource.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
data "ovirt_blank_template" "blank" {
}

resource "ovirt_disk" "test" {
resource "ovirt_disk_from_image" "test" {
storagedomain_id = var.storagedomain_id
format = "raw"
size = 1048576
alias = "test"
sparse = true
source_file = "./testimage/image"
}

resource "ovirt_vm" "test" {
Expand All @@ -17,7 +17,7 @@ resource "ovirt_vm" "test" {

resource "ovirt_disk_attachment" "test" {
vm_id = ovirt_vm.test.id
disk_id = ovirt_disk.test.id
disk_id = ovirt_disk_from_image.test.id
disk_interface = "virtio_scsi"
}

Expand Down
1 change: 1 addition & 0 deletions examples/resources/ovirt_vm_start/testimage
2 changes: 2 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package main

//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs

//go:generate go run scripts/get_test_image/get_test_image.go
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,6 @@ 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-alpha7 h1:LhQiiiPEzlcPVwzdw8K8VGPBmZWRtp4Kempxa/070m0=
github.com/ovirt/go-ovirt-client v1.0.0-alpha7/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8 h1:ATO+1zem2WLSFHKmNHBfCEZFH+GSSAZg8SFeI1GDhys=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8.0.20220524160043-adf38ca8ec41 h1:pI/axwLyWaWWHQTHQVqODGKoAEx3qCPSmay83WB7J9A=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8.0.20220524160043-adf38ca8ec41/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8.0.20220525091503-4f83e7145820 h1:ExuHW/6Y89ZY7u3+Sm5SoLeJNGkpwCkrdJHeCYYnvK4=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8.0.20220525091503-4f83e7145820/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client v1.0.0-alpha9 h1:HmO7pYokPBDswW56vwfSHepDd4Kyh5IY4RHtWT1Y+n8=
github.com/ovirt/go-ovirt-client v1.0.0-alpha9/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0 h1:uvACVHYhYPMkNJrrgWiABcfELB6qoFfsDDUTbpb4Jv4=
Expand Down
13 changes: 12 additions & 1 deletion ovirt/resource_ovirt_disk_from_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -70,7 +71,17 @@ func (p *provider) diskFromImageCreate(ctx context.Context, data *schema.Resourc
}
}
}
sourceFile := data.Get("source_file").(string)
sourceFileName := data.Get("source_file").(string)
sourceFile, err := filepath.Abs(sourceFileName)
if err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Failed to find absolute path for %s", sourceFileName),
Detail: err.Error(),
},
}
}
// We actually want to include the file here, so this is not gosec-relevant.
fh, err := os.Open(sourceFile) //nolint:gosec
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions ovirt/resource_ovirt_vm_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (p *provider) vmStartRead(
id := data.Get("vm_id").(string)
vm, err := client.GetVM(ovirtclient.VMID(id))
if err != nil {
if isNotFound(err) {
data.SetId("")
return nil
}
return errorToDiags("get VM status", err)
}
return vmStartResourceUpdate(vm, data)
Expand All @@ -142,10 +146,10 @@ func (p *provider) vmStartDelete(
_ interface{},
) diag.Diagnostics {
client := p.client.WithContext(ctx)
stopBehavior := data.Get("stop_behavior")
stopBehavior := data.Get("stop_behavior").(string)
force := data.Get("force_stop").(bool)
var err error
if stopBehavior == VMStopBehaviorStop {
if stopBehavior == string(VMStopBehaviorStop) {
err = client.StopVM(ovirtclient.VMID(data.Id()), force)
} else {
err = client.ShutdownVM(ovirtclient.VMID(data.Id()), force)
Expand Down
31 changes: 27 additions & 4 deletions ovirt/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ovirt
import (
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
Expand Down Expand Up @@ -38,25 +39,47 @@ func validateDiskSize(i interface{}, path cty.Path) diag.Diagnostics {
return nil
}

func validateLocalFile(i interface{}, path cty.Path) diag.Diagnostics {
func validateLocalFile(i interface{}, p cty.Path) diag.Diagnostics {
val, ok := i.(string)
if !ok {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Local file path must be a string.",
Detail: "The local file path is not a string.",
AttributePath: path,
AttributePath: p,
},
}
}
cwd, err := os.Getwd()
if err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to get determine current working directory",
Detail: err.Error(),
AttributePath: p,
},
}
}
if _, err := os.Stat(val); err != nil {
path, err := filepath.Abs(val)
if err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Failed to get absolute path for %s, cwd is %s", val, cwd),
Detail: err.Error(),
AttributePath: p,
},
}
}
if _, err := os.Stat(path); err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Failed to stat %s", val),
Detail: err.Error(),
AttributePath: path,
AttributePath: p,
},
}
}
Expand Down
Loading

0 comments on commit 7b956a8

Please sign in to comment.