Skip to content

Commit

Permalink
B #491: template: add disk and nic update
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Apr 18, 2024
1 parent 83a9cb5 commit f28c04a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions opennebula/resource_opennebula_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,20 @@ 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("tags") {

oldTagsIf, newTagsIf := d.GetChange("tags")
Expand Down
91 changes: 87 additions & 4 deletions opennebula/resource_opennebula_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""),
Expand Down Expand Up @@ -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", ""),
Expand Down Expand Up @@ -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", ""),
Expand Down Expand Up @@ -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", ""),
Expand Down Expand Up @@ -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"
Expand All @@ -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 = ""
Expand Down Expand Up @@ -292,7 +342,7 @@ resource "opennebula_template" "template" {
}
`

var testAccTemplateCPUModel = `
var testAccTemplateCPUModel = testTemplateNICVNetResources + `
resource "opennebula_template" "template" {
name = "terra-tpl-cpumodel"
permissions = "660"
Expand All @@ -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"
}
Expand Down Expand Up @@ -340,7 +401,7 @@ resource "opennebula_template" "template" {
}
`

var testAccTemplateConfigUpdate = `
var testAccTemplateConfigUpdate = testTemplateNICVNetResources + `
resource "opennebula_template" "template" {
name = "terratplupdate"
permissions = "642"
Expand All @@ -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 = ""
Expand All @@ -391,7 +463,7 @@ resource "opennebula_template" "template" {
}
`

var testAccTemplateConfigDelete = `
var testAccTemplateConfigDelete = testTemplateNICVNetResources + `
resource "opennebula_template" "template" {
name = "terratplupdate"
permissions = "642"
Expand All @@ -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 = ""
Expand Down
6 changes: 3 additions & 3 deletions opennebula/resource_opennebula_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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))
Expand Down
12 changes: 11 additions & 1 deletion opennebula/shared_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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{}))

Expand Down

0 comments on commit f28c04a

Please sign in to comment.