Skip to content

Commit

Permalink
Removed automatic default group id search on user creation (#35)
Browse files Browse the repository at this point in the history
* removed automatic default user group id

* group_id made required & validated field in user datasource

* group_id made required & validated field in user resource
  • Loading branch information
michaelfmnk authored Aug 1, 2024
1 parent 1a7892e commit 4fe6952
Showing 1 changed file with 7 additions and 28 deletions.
35 changes: 7 additions & 28 deletions cloudconnexa/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cloudconnexa

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -46,9 +45,10 @@ func resourceUser() *schema.Resource {
Description: "User's last name.",
},
"group_id": {
Type: schema.TypeString,
Optional: true,
Description: "The UUID of a user's group.",
Type: schema.TypeString,
Required: true,
Description: "The UUID of a user's group.",
ValidateFunc: validation.IsUUID,
},
"role": {
Type: schema.TypeString,
Expand Down Expand Up @@ -145,16 +145,6 @@ func resourceUserRead(ctx context.Context, d *schema.ResourceData, m interface{}
userId := d.Id()
u, err := c.Users.Get(userId)

// If group_id is not set, CloudConnexa sets it to the default group.
var groupId string
if d.Get("group_id") == "" {
// The group has not been explicitly set.
// Set it to an empty string to keep the default group.
groupId = ""
} else {
groupId = u.GroupId
}

if err != nil {
return append(diags, diag.FromErr(err)...)
}
Expand All @@ -165,7 +155,7 @@ func resourceUserRead(ctx context.Context, d *schema.ResourceData, m interface{}
d.Set("email", u.Email)
d.Set("first_name", u.FirstName)
d.Set("last_name", u.LastName)
d.Set("group_id", groupId)
d.Set("group_id", u.GroupId)
d.Set("devices", u.Devices)
d.Set("role", u.Role)
}
Expand All @@ -189,25 +179,14 @@ func resourceUserUpdate(ctx context.Context, d *schema.ResourceData, m interface
_, lastName := d.GetChange("last_name")
_, role := d.GetChange("role")
status := u.Status
oldGroupId, newGroupId := d.GetChange("group_id")

groupId := newGroupId.(string)
// If both are empty strings, then the group has not been set explicitly.
// The update endpoint requires group_id to be set, so we should set it to the default group.
if oldGroupId.(string) == "" && groupId == "" {
g, err := c.UserGroups.GetByName("Default")
if err != nil {
return append(diags, diag.FromErr(err)...)
}
groupId = g.ID
}
_, groupId := d.GetChange("group_id")

err = c.Users.Update(cloudconnexa.User{
Id: d.Id(),
Email: email.(string),
FirstName: firstName.(string),
LastName: lastName.(string),
GroupId: groupId,
GroupId: groupId.(string),
Role: role.(string),
Status: status,
})
Expand Down

0 comments on commit 4fe6952

Please sign in to comment.