Skip to content

Commit

Permalink
Fix #10, #63 and #68
Browse files Browse the repository at this point in the history
  • Loading branch information
Nosmoht committed Jul 4, 2020
1 parent 91000e8 commit 5794366
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
37 changes: 23 additions & 14 deletions nexus/resource_role.go
@@ -1,6 +1,8 @@
package nexus

import (
"strings"

nexus "github.com/datadrivers/go-nexus-client"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand Down Expand Up @@ -34,15 +36,25 @@ func resourceRole() *schema.Resource {
},
"privileges": {
Description: "The privileges of this role.",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Set: func(v interface{}) int {
return schema.HashString(strings.ToLower(v.(string)))
},
Type: schema.TypeSet,
},
"roles": {
Description: "The roles of this role.",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Set: func(v interface{}) int {
return schema.HashString(strings.ToLower(v.(string)))
},
Type: schema.TypeSet,
},
},
}
Expand All @@ -53,15 +65,14 @@ func getRoleFromResourceData(d *schema.ResourceData) nexus.Role {
ID: d.Get("roleid").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Privileges: resourceDataStringSlice(d, "privileges"),
Roles: resourceDataStringSlice(d, "roles"),
Privileges: interfaceSliceToStringSlice(d.Get("privileges").(*schema.Set).List()),
Roles: interfaceSliceToStringSlice(d.Get("roles").(*schema.Set).List()),
}
}

func resourceRoleCreate(d *schema.ResourceData, m interface{}) error {
nexusClient := m.(nexus.Client)
role := getRoleFromResourceData(d)

if err := nexusClient.RoleCreate(role); err != nil {
return err
}
Expand Down Expand Up @@ -96,11 +107,9 @@ func resourceRoleUpdate(d *schema.ResourceData, m interface{}) error {
nexusClient := m.(nexus.Client)
roleID := d.Get("roleid").(string)

if d.HasChange("name") || d.HasChange("description") || d.HasChange("privileges") || d.HasChange("roles") {
role := getRoleFromResourceData(d)
if err := nexusClient.RoleUpdate(roleID, role); err != nil {
return err
}
role := getRoleFromResourceData(d)
if err := nexusClient.RoleUpdate(roleID, role); err != nil {
return err
}

return resourceRoleRead(d, m)
Expand Down
7 changes: 3 additions & 4 deletions nexus/resource_role_test.go
Expand Up @@ -2,6 +2,7 @@ package nexus

import (
"fmt"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -31,10 +32,8 @@ func TestAccRoleBasic(t *testing.T) {
resource.TestCheckResourceAttr("nexus_role.acceptance", "name", roleName),
resource.TestCheckResourceAttr("nexus_role.acceptance", "roleid", roleID),
resource.TestCheckResourceAttr("nexus_role.acceptance", "description", roleDescription),
resource.TestCheckResourceAttr("nexus_role.acceptance", "roles.#", "1"),
resource.TestCheckResourceAttr("nexus_role.acceptance", "roles.0", roleRoles[0]),
resource.TestCheckResourceAttr("nexus_role.acceptance", "privileges.#", "1"),
resource.TestCheckResourceAttr("nexus_role.acceptance", "privileges.0", rolePrivileges[0]),
resource.TestCheckResourceAttr("nexus_role.acceptance", "privileges.#", strconv.Itoa(len(rolePrivileges))),
resource.TestCheckResourceAttr("nexus_role.acceptance", "roles.#", strconv.Itoa(len(roleRoles))),
),
},
{
Expand Down

0 comments on commit 5794366

Please sign in to comment.