Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #344: added ovirt_disk_resize resource #431

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/resources/disk_resize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "ovirt_disk_resize Resource - terraform-provider-ovirt"
subcategory: ""
description: |-
The ovirtdiskresize resource resizes disks in oVirt to the specified size.
~> Only use this resource with disks created from templates. Otherwise, two terraform resources will handle the same disk resource.
---

# ovirt_disk_resize (Resource)

The ovirt_disk_resize resource resizes disks in oVirt to the specified size.

~> Only use this resource with disks created from templates. Otherwise, two terraform resources will handle the same disk resource.

## Example Usage

```terraform
# this data-source requires a VM created in another configuration and referenced here
data "ovirt_disk_attachments" "templated" {
vm_id = var.vm_id
}

resource "ovirt_disk_resize" "resized" {
# looping through all the disk attachments
# using the attachment id (a.id) as key and the disk id (a.disk_id) as value
for_each = {for a in data.ovirt_disk_attachments.templated.attachments: a.id => a.disk_id}

disk_id = "${each.value}"
size = 2*1048576
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `disk_id` (String) ID of the disk to resize.
- `size` (Number) Disk size in bytes.

### Read-Only

- `id` (String) The ID of this resource.


20 changes: 20 additions & 0 deletions examples/resources/ovirt_disk_resize/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
terraform {
required_providers {
ovirt = {
source = "ovirt/ovirt"
}
}

required_version = ">= 0.15"
}

provider "ovirt" {
url = var.url
username = var.username
password = var.password
tls_ca_bundle = var.tls_ca_bundle
tls_system = var.tls_system
tls_ca_dirs = var.tls_ca_dirs
tls_ca_files = var.tls_ca_files
tls_insecure = var.tls_insecure
}
13 changes: 13 additions & 0 deletions examples/resources/ovirt_disk_resize/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# this data-source requires a VM created in another configuration and referenced here
data "ovirt_disk_attachments" "templated" {
vm_id = var.vm_id
}

resource "ovirt_disk_resize" "resized" {
# looping through all the disk attachments
# using the attachment id (a.id) as key and the disk id (a.disk_id) as value
for_each = {for a in data.ovirt_disk_attachments.templated.attachments: a.id => a.disk_id}

disk_id = "${each.value}"
size = 2*1048576
}
39 changes: 39 additions & 0 deletions examples/resources/ovirt_disk_resize/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
variable "vm_id" {
type = string
description = "ID of the oVirt VM for which the disks are resized."
}

variable "username" {
type = string
}
variable "password" {
type = string
}
variable "url" {
type = string
}
variable "tls_ca_files" {
type = list(string)
default = []
}
variable "tls_ca_dirs" {
type = list(string)
default = []
}
variable "tls_insecure" {
type = bool
default = false
}
variable "tls_ca_bundle" {
type = string
default = ""
}
variable "tls_system" {
type = bool
default = true
description = "Take TLS CA certificates from system root. Does not work on Windows."
}
variable "mock" {
type = bool
default = true
}
1 change: 1 addition & 0 deletions internal/ovirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (p *provider) getProvider() *schema.Provider {
"ovirt_vm_tag": p.vmTagResource(),
"ovirt_vm_optimize_cpu_settings": p.vmOptimizeCPUSettingsResource(),
"ovirt_disk": p.diskResource(),
"ovirt_disk_resize": p.diskResizeResource(),
"ovirt_disk_from_image": p.diskFromImageResource(),
"ovirt_disk_attachment": p.diskAttachmentResource(),
"ovirt_disk_attachments": p.diskAttachmentsResource(),
Expand Down
119 changes: 119 additions & 0 deletions internal/ovirt/resource_ovirt_disk_resize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package ovirt

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ovirtclient "github.com/ovirt/go-ovirt-client"
)

var diskResizeSchema = map[string]*schema.Schema{
"disk_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of the disk to resize.",
ValidateDiagFunc: validateUUID,
},
"size": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "Disk size in bytes.",
ValidateDiagFunc: validateDiskSize,
},
}

func (p *provider) diskResizeResource() *schema.Resource {
return &schema.Resource{
CreateContext: p.diskResizeCreate,
ReadContext: p.diskResizeRead,
DeleteContext: p.diskResizeDelete,
Schema: diskResizeSchema,
Description: `The ovirt_disk_resize resource resizes disks in oVirt to the specified size.

~> Only use this resource with disks created from templates. Otherwise, two terraform resources will handle the same disk resource.
`,
}
}

func (p *provider) diskResizeCreate(
ctx context.Context,
data *schema.ResourceData,
_ interface{},
) diag.Diagnostics {
client := p.client.WithContext(ctx)
return resizeDisk(client, data)
}

func (p *provider) diskResizeRead(ctx context.Context, data *schema.ResourceData, _ interface{}) diag.Diagnostics {
client := p.client.WithContext(ctx)

diskID := data.Get("disk_id").(string)
disk, err := client.GetDisk(ovirtclient.DiskID(diskID))
if err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to retrieve disk.",
Detail: err.Error(),
},
}
}

data.SetId(diskID)
diags := diag.Diagnostics{}
diags = setResourceField(data, "size", disk.ProvisionedSize(), diags)

return diags
}

func (p *provider) diskResizeDelete(_ context.Context, data *schema.ResourceData, _ interface{}) diag.Diagnostics {
data.SetId("")
return nil
}

func resizeDisk(client ovirtclient.Client, data *schema.ResourceData) diag.Diagnostics {
diskID := data.Get("disk_id").(string)
newSize := data.Get("size").(int)

params := ovirtclient.UpdateDiskParams()
_, err := params.WithProvisionedSize(uint64(newSize))
if err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to set parameters for updating disk size.",
Detail: err.Error(),
},
}
}

updateFailedDiag := diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to update disk size.",
}
diskUpdate, err := client.StartUpdateDisk(ovirtclient.DiskID(diskID), params)
if err != nil {
if isNotFound(err) {
data.SetId("")
}
updateFailedDiag.Detail = err.Error()
return diag.Diagnostics{updateFailedDiag}
}
_, err = diskUpdate.Wait()
if err != nil {
if isNotFound(err) {
data.SetId("")
}
updateFailedDiag.Detail = err.Error()
return diag.Diagnostics{updateFailedDiag}
}

data.SetId(diskID)
diags := diag.Diagnostics{}
diags = setResourceField(data, "size", newSize, diags)

return diags
}
55 changes: 55 additions & 0 deletions internal/ovirt/resource_ovirt_disk_resize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ovirt

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
ovirtclient "github.com/ovirt/go-ovirt-client"
)

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

p := newProvider(newTestLogger(t))

helper := p.getTestHelper()
disk, err := helper.GetClient().CreateDisk(
helper.GetStorageDomainID(), ovirtclient.ImageFormatRaw, 1048576,
ovirtclient.CreateDiskParams().MustWithAlias("TestDisk").MustWithSparse(true))
if err != nil {
t.Fatal(err)
}

resource.UnitTest(t, resource.TestCase{
ProviderFactories: p.getProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
provider "ovirt" {
mock = true
}
resource "ovirt_disk_resize" "resized" {
disk_id = "%s"
size = 2*1048576
}`,
disk.ID(),
),
Check: resource.ComposeTestCheckFunc(
func(state *terraform.State) error {
diskID := state.RootModule().Resources["ovirt_disk_resize.resized"].Primary.ID
disk, err := p.getTestHelper().GetClient().GetDisk(ovirtclient.DiskID(diskID))
if err != nil {
return err
}
if disk.ProvisionedSize() != 2097152 {
return fmt.Errorf("Expected disk size to be 2097152, but got %d", disk.ProvisionedSize())
}
return nil
},
),
},
},
})
}