Skip to content

Commit

Permalink
Merge branch 'OpenNebula:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
shurkys authored May 20, 2024
2 parents 6b2fab1 + 4bc7990 commit c3e14c6
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 53 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 1.4.1 (Unreleased)

FEATURES:

* resources/opennebula_virtual_network: allow to modify the user owning the resource (#529)
* resources/opennebula_virtual_machine: add nil checks before type casting (#530)

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
43 changes: 31 additions & 12 deletions opennebula/resource_opennebula_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ func resourceOpennebulaVirtualNetwork() *schema.Resource {
Type: schema.TypeInt,
},
},
"user": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the user that onws the Virtual Network, If empty, it uses caller group",
},
"group": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -362,25 +367,39 @@ func getVirtualNetworkController(d *schema.ResourceData, meta interface{}) (*goc
return controller.VirtualNetwork(int(imgID)), nil
}

func changeVNetGroup(d *schema.ResourceData, meta interface{}) error {
func changVNetOwner(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Configuration)
controller := config.Controller
var gid int
var gid, uid int

vnc, err := getVirtualNetworkController(d, meta)
if err != nil {
return err
}

group := d.Get("group").(string)
gid, err = controller.Groups().ByName(group)
if err != nil {
return fmt.Errorf("Can't find a group with name `%s`: %s", group, err)
if len(group) > 0 {
gid, err = controller.Groups().ByName(group)
if err != nil {
return fmt.Errorf("can't find a group with name `%s`: %s", group, err)
}
} else {
gid = -1
}

err = vnc.Chown(-1, gid)
user := d.Get("user").(string)
if len(user) > 0 {
uid, err = controller.Users().ByName(user)
if err != nil {
return fmt.Errorf("can't find a group with name `%s`: %s", user, err)
}
} else {
uid = -1
}

err = vnc.Chown(uid, gid)
if err != nil {
return fmt.Errorf("Can't find a group with ID `%d`: %s", gid, err)
return fmt.Errorf("can't set the owners to user:`%d` group:`%d` %s", uid, gid, err)
}

return nil
Expand Down Expand Up @@ -607,12 +626,12 @@ func resourceOpennebulaVirtualNetworkCreate(ctx context.Context, d *schema.Resou
}
}

if d.Get("group") != "" {
err := changeVNetGroup(d, meta)
if d.Get("group") != "" || d.Get("user") != "" {
err := changVNetOwner(d, meta)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to change group",
Summary: "Failed to change user and group",
Detail: fmt.Sprintf("virtual network (ID: %s): %s", d.Id(), err),
})
return diags
Expand Down Expand Up @@ -1507,8 +1526,8 @@ func resourceOpennebulaVirtualNetworkUpdate(ctx context.Context, d *schema.Resou
log.Printf("[INFO] Successfully updated Vnet\n")
}

if d.HasChange("group") {
err = changeVNetGroup(d, meta)
if d.HasChange("group") || d.HasChange("user") {
err = changVNetOwner(d, meta)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Expand Down
Loading

0 comments on commit c3e14c6

Please sign in to comment.