From e181e0e7d5b1d4c6d53c678203555733dac97773 Mon Sep 17 00:00:00 2001 From: Pierre Lafievre Date: Fri, 16 Feb 2024 17:32:49 +0100 Subject: [PATCH] F #529: allow to modify resource user --- CHANGELOG.md | 6 +++ .../resource_opennebula_virtual_network.go | 43 +++++++++++++------ website/docs/r/virtual_network.html.markdown | 1 + 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0eeeb5f5c..1ad24983d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/opennebula/resource_opennebula_virtual_network.go b/opennebula/resource_opennebula_virtual_network.go index 7d3d465e3..309b588e1 100644 --- a/opennebula/resource_opennebula_virtual_network.go +++ b/opennebula/resource_opennebula_virtual_network.go @@ -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, @@ -362,10 +367,10 @@ 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 { @@ -373,14 +378,28 @@ func changeVNetGroup(d *schema.ResourceData, meta interface{}) error { } 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 @@ -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 @@ -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, diff --git a/website/docs/r/virtual_network.html.markdown b/website/docs/r/virtual_network.html.markdown index c740b1ae2..764e1fbd0 100644 --- a/website/docs/r/virtual_network.html.markdown +++ b/website/docs/r/virtual_network.html.markdown @@ -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`.