From c69a462b7e2f3e897e50012b499b5dba8226c7cd Mon Sep 17 00:00:00 2001 From: Pierre Lafievre Date: Thu, 18 Apr 2024 11:46:21 +0200 Subject: [PATCH] B #491: template: add disk and nic update --- CHANGELOG.md | 4 + opennebula/resource_opennebula_template.go | 19 ++++ .../resource_opennebula_template_test.go | 91 ++++++++++++++++++- .../resource_opennebula_virtual_machine.go | 6 +- opennebula/shared_schemas.go | 12 ++- 5 files changed, 124 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ad24983d..04976166a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ FEATURES: * resources/opennebula_virtual_network: allow to modify the user owning the resource (#529) +ENHANCEMENTS: + +* resources/opennebula_template: enable disk and nic update (#491) + # 1.4.0 (January 22nd, 2024) FEATURES: diff --git a/opennebula/resource_opennebula_template.go b/opennebula/resource_opennebula_template.go index db2889fba..4ddd08a60 100644 --- a/opennebula/resource_opennebula_template.go +++ b/opennebula/resource_opennebula_template.go @@ -718,6 +718,25 @@ func resourceOpennebulaTemplateUpdateCustom(ctx context.Context, d *schema.Resou update = true } + if d.HasChange("disk") { + newTpl.Del("DISK") + + err := addDisks(d, &newTpl) + if err != nil { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: "Failed to update disks", + Detail: fmt.Sprintf("template (ID: %s): %s", d.Id(), err), + }) + return diags + } + } + + if d.HasChange("nic") { + newTpl.Del("NIC") + addNICs(d, &newTpl) + } + if d.HasChange("tags") { oldTagsIf, newTagsIf := d.GetChange("tags") diff --git a/opennebula/resource_opennebula_template_test.go b/opennebula/resource_opennebula_template_test.go index 3dad57f85..5ef3d808a 100644 --- a/opennebula/resource_opennebula_template_test.go +++ b/opennebula/resource_opennebula_template_test.go @@ -29,6 +29,11 @@ func TestAccTemplate(t *testing.T) { resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.keymap", "en-us"), resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.listen", "0.0.0.0"), resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.type", "VNC"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.#", "1"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.0.target", "vda"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.0.size", "16"), + resource.TestCheckResourceAttr("opennebula_template.template", "nic.#", "1"), + resource.TestCheckResourceAttr("opennebula_template.template", "nic.0.ip", "172.16.100.131"), resource.TestCheckResourceAttr("opennebula_template.template", "os.#", "1"), resource.TestCheckResourceAttr("opennebula_template.template", "os.0.arch", "x86_64"), resource.TestCheckResourceAttr("opennebula_template.template", "os.0.boot", ""), @@ -72,6 +77,11 @@ func TestAccTemplate(t *testing.T) { resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.keymap", "en-us"), resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.listen", "0.0.0.0"), resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.type", "VNC"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.#", "1"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.0.target", "vda"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.0.size", "16"), + resource.TestCheckResourceAttr("opennebula_template.template", "nic.#", "1"), + resource.TestCheckResourceAttr("opennebula_template.template", "nic.0.ip", "172.16.100.131"), resource.TestCheckResourceAttr("opennebula_template.template", "os.#", "1"), resource.TestCheckResourceAttr("opennebula_template.template", "os.0.arch", "x86_64"), resource.TestCheckResourceAttr("opennebula_template.template", "os.0.boot", ""), @@ -111,6 +121,11 @@ func TestAccTemplate(t *testing.T) { resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.keymap", "en-us"), resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.listen", "0.0.0.0"), resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.type", "VNC"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.#", "1"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.0.target", "vda"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.0.size", "32"), + resource.TestCheckResourceAttr("opennebula_template.template", "nic.#", "1"), + resource.TestCheckResourceAttr("opennebula_template.template", "nic.0.ip", "172.16.100.132"), resource.TestCheckResourceAttr("opennebula_template.template", "os.#", "1"), resource.TestCheckResourceAttr("opennebula_template.template", "os.0.arch", "x86_64"), resource.TestCheckResourceAttr("opennebula_template.template", "os.0.boot", ""), @@ -153,6 +168,11 @@ func TestAccTemplate(t *testing.T) { resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.keymap", "en-us"), resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.listen", "0.0.0.0"), resource.TestCheckResourceAttr("opennebula_template.template", "graphics.0.type", "VNC"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.#", "1"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.0.target", "vda"), + resource.TestCheckResourceAttr("opennebula_template.template", "disk.0.size", "32"), + resource.TestCheckResourceAttr("opennebula_template.template", "nic.#", "1"), + resource.TestCheckResourceAttr("opennebula_template.template", "nic.0.ip", "172.16.100.132"), resource.TestCheckResourceAttr("opennebula_template.template", "os.#", "1"), resource.TestCheckResourceAttr("opennebula_template.template", "os.0.arch", "x86_64"), resource.TestCheckResourceAttr("opennebula_template.template", "os.0.boot", ""), @@ -239,7 +259,26 @@ func testAccCheckTemplateDestroy(s *terraform.State) error { return nil } -var testAccTemplateConfigBasic = ` +var testTemplateNICVNetResources = ` + +resource "opennebula_virtual_network" "network" { + name = "test-net1" + type = "dummy" + bridge = "onebr" + mtu = 1500 + ar { + ar_type = "IP4" + size = 12 + ip4 = "172.16.100.130" + } + permissions = "642" + group = "oneadmin" + security_groups = [0] + cluster_ids = [0] + } +` + +var testAccTemplateConfigBasic = testTemplateNICVNetResources + ` resource "opennebula_template" "template" { name = "terra-tpl" permissions = "660" @@ -265,6 +304,17 @@ resource "opennebula_template" "template" { type = "VNC" } + disk { + volatile_type = "swap" + size = 16 + target = "vda" + } + + nic { + network_id = opennebula_virtual_network.network.id + ip = "172.16.100.131" + } + os { arch = "x86_64" boot = "" @@ -292,7 +342,7 @@ resource "opennebula_template" "template" { } ` -var testAccTemplateCPUModel = ` +var testAccTemplateCPUModel = testTemplateNICVNetResources + ` resource "opennebula_template" "template" { name = "terra-tpl-cpumodel" permissions = "660" @@ -313,6 +363,17 @@ resource "opennebula_template" "template" { type = "VNC" } + disk { + volatile_type = "swap" + size = 16 + target = "vda" + } + + nic { + network_id = opennebula_virtual_network.network.id + ip = "172.16.100.131" + } + cpumodel { model = "host-passthrough" } @@ -340,7 +401,7 @@ resource "opennebula_template" "template" { } ` -var testAccTemplateConfigUpdate = ` +var testAccTemplateConfigUpdate = testTemplateNICVNetResources + ` resource "opennebula_template" "template" { name = "terratplupdate" permissions = "642" @@ -367,6 +428,17 @@ resource "opennebula_template" "template" { type = "VNC" } + disk { + volatile_type = "swap" + size = 32 + target = "vda" + } + + nic { + network_id = opennebula_virtual_network.network.id + ip = "172.16.100.132" + } + os { arch = "x86_64" boot = "" @@ -391,7 +463,7 @@ resource "opennebula_template" "template" { } ` -var testAccTemplateConfigDelete = ` +var testAccTemplateConfigDelete = testTemplateNICVNetResources + ` resource "opennebula_template" "template" { name = "terratplupdate" permissions = "642" @@ -412,6 +484,17 @@ resource "opennebula_template" "template" { type = "VNC" } + disk { + volatile_type = "swap" + size = 32 + target = "vda" + } + + nic { + network_id = opennebula_virtual_network.network.id + ip = "172.16.100.132" + } + os { arch = "x86_64" boot = "" diff --git a/opennebula/resource_opennebula_virtual_machine.go b/opennebula/resource_opennebula_virtual_machine.go index a09ad5e05..949408609 100644 --- a/opennebula/resource_opennebula_virtual_machine.go +++ b/opennebula/resource_opennebula_virtual_machine.go @@ -291,7 +291,7 @@ func resourceOpennebulaVirtualMachineCreate(ctx context.Context, d *schema.Resou return diags } - generateVMNIC(d, vmTpl) + addNICs(d, vmTpl) log.Printf("[DEBUG] VM template: %s", vmTpl.String()) @@ -357,7 +357,7 @@ func resourceOpennebulaVirtualMachineCreate(ctx context.Context, d *schema.Resou return diags } - generateVMNIC(d, vmTpl) + addNICs(d, vmTpl) log.Printf("[DEBUG] VM template: %s", vmTpl.String()) @@ -2259,7 +2259,7 @@ func generateVm(d *schema.ResourceData, meta interface{}, templateContent *vm.Te return tpl, nil } -func generateVMNIC(d *schema.ResourceData, tpl *vm.Template) { +func addNICs(d *schema.ResourceData, tpl *vm.Template) { //Generate NIC definition nics := d.Get("nic").([]interface{}) log.Printf("Number of NICs: %d", len(nics)) diff --git a/opennebula/shared_schemas.go b/opennebula/shared_schemas.go index 61bf8ed7a..7224dcdda 100644 --- a/opennebula/shared_schemas.go +++ b/opennebula/shared_schemas.go @@ -641,7 +641,7 @@ func addGraphic(tpl *vm.Template, graphics []interface{}) { } } -func generateVMTemplate(d *schema.ResourceData, tpl *vm.Template) error { +func addDisks(d *schema.ResourceData, tpl *vm.Template) error { //Generate DISK definition disks := d.Get("disk").([]interface{}) @@ -668,6 +668,16 @@ func generateVMTemplate(d *schema.ResourceData, tpl *vm.Template) error { tpl.Elements = append(tpl.Elements, disk) } + return nil +} + +func generateVMTemplate(d *schema.ResourceData, tpl *vm.Template) error { + + err := addDisks(d, tpl) + if err != nil { + return err + } + //Generate GRAPHICS definition addGraphic(tpl, d.Get("graphics").([]interface{}))