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

B-491: template: add disk and nic update #541

Merged
merged 1 commit into from
Apr 18, 2024
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
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
19 changes: 19 additions & 0 deletions opennebula/resource_opennebula_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
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
Loading