Skip to content

Commit

Permalink
F #529: allow to modify resource user
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Feb 16, 2024
1 parent 9ac3407 commit e181e0e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.4.1 (Unreleased)

FEATURES:

* resources/opennebula_virtual_network: allow to modify the user owning the resource (#529)

# 1.4.0 (January 22nd, 2024)

FEATURES:
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
1 change: 1 addition & 0 deletions website/docs/r/virtual_network.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ The following arguments are supported:
* `search_domain` - (Optional) Default search domains for DNS resolution. Conflicts with `reservation_vnet` and `reservation_size`.
* `ar` - (Deprecated) List of address ranges. See [Address Range Parameters](#address-range-parameters) below for more details. Conflicts with `reservation_vnet` and `reservation_size`.
* `hold_ips` - (Deprecated) Hold Ips from any Address Range of the Virtual Network. The IP must be available to be held`. Conflicts with`reservation_vnet` and `reservation_size`.
* `user` - (Optional) Name of the user which owns the virtual network.
* `group` - (Optional) Name of the group which owns the virtual network. Defaults to the caller primary group.
* `tags` - (Optional) Map of tags (`key=value`) assigned to the resource. Override matching tags present in the `default_tags` atribute when configured in the `provider` block. See [tags usage related documentation](https://registry.terraform.io/providers/OpenNebula/opennebula/latest/docs#using-tags) for more information.
* `lock` - (Optional) Lock the virtual network with a specific lock level. Supported values: `USE`, `MANAGE`, `ADMIN`, `ALL` or `UNLOCK`.
Expand Down

0 comments on commit e181e0e

Please sign in to comment.