Skip to content

Commit

Permalink
fix update user roles + uppercase databases
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Nicole committed Oct 14, 2021
1 parent eab91d2 commit 48ab498
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ HOSTNAME=registry.terraform.io
NAMESPACE=headyj
NAME=neo4j
BINARY=terraform-provider-${NAME}
VERSION=0.2.0
VERSION=0.2.1
OS_ARCH=darwin_amd64

default: install
Expand Down
6 changes: 3 additions & 3 deletions docs/resources/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ the `neo4j_database` resource creates and manage databases on a Neo4j server.

```hcl
resource "neo4j_database" "my_database" {
name ="myDatabase"
name ="mydatabase"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the database.
* `name` - (Required) The name of the database. Only lowercase are accepted by Neo4j

## Attribute Reference

Expand All @@ -27,5 +27,5 @@ The following arguments are exported:
neo4j_database resource can be importe using the resource name, e.g.

```bash
terraform import neo4j_user.my_database myDatabase
terraform import neo4j_user.my_database mydatabase
```
2 changes: 1 addition & 1 deletion examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ provider "neo4j" {
}

resource "neo4j_database" "my_database" {
name = "myDatabase"
name = "mydatabase"
}

resource "neo4j_role" "my_role" {
Expand Down
9 changes: 9 additions & 0 deletions neo4j/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package neo4j

import (
"context"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -30,6 +31,10 @@ func resourceDatabaseCreate(ctx context.Context, d *schema.ResourceData, m inter

// Warning or errors can be collected in a slice type
var diags diag.Diagnostics

if strings.ToLower(d.Get("name").(string)) != d.Get("name").(string) {
return diag.Errorf("Neo4j does not allow uppercase in database name")
}
c, err := m.(*Neo4jConfiguration).GetDbConn()
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -81,6 +86,10 @@ func resourceDatabaseDelete(ctx context.Context, d *schema.ResourceData, m inter
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics

if strings.ToLower(d.Get("name").(string)) != d.Get("name").(string) {
return diag.Errorf("Neo4j does not allow uppercase in database name")
}

c, err := m.(*Neo4jConfiguration).GetDbConn()
if err != nil {
return diag.FromErr(err)
Expand Down
5 changes: 2 additions & 3 deletions neo4j/resource_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ func TestResourceDatabase(t *testing.T) {
{
Config: testResourceDatabaseConfig_basic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.neo4j_databases.all", "name", "mysql"),
resource.TestCheckResourceAttr("data.neo4j_databases.all", "pattern", "%"),
resource.TestCheckResourceAttr("neo4j_database.test", "name", "mydatabase2"),
),
},
},
Expand All @@ -31,7 +30,7 @@ func testResourceDatabaseConfig_basic() string {
password = "password"
}
resource "neo4j_database" "test" {
name = "test"
name = "myDatabase2"
}
`)
}
25 changes: 25 additions & 0 deletions neo4j/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@ func resourceUserUpdate(ctx context.Context, d *schema.ResourceData, m interface
d.GetChange("password")
}

if d.HasChange("roles") {
oldRoles, newRoles := d.GetChange("roles")
oldRolesSet := oldRoles.(*schema.Set).List()
newRolesSet := newRoles.(*schema.Set).List()
var roleList []string
if len(oldRolesSet) > 0 {
for _, role := range oldRolesSet {
roleList = append(roleList, role.(string))
}
_, err = session.Run("REVOKE ROLE $roles FROM $username", map[string]interface{}{"roles": strings.Join(roleList, ","), "username": d.Get("name")})
if err != nil {
return diag.FromErr(err)
}
}
if len(newRolesSet) > 0 {
for _, role := range newRolesSet {
roleList = append(roleList, role.(string))
}
_, err = session.Run("GRANT ROLE $roles TO $username", map[string]interface{}{"roles": strings.Join(roleList, ","), "username": d.Get("name")})
if err != nil {
return diag.FromErr(err)
}
}
}

return resourceUserRead(ctx, d, m)
}

Expand Down

0 comments on commit 48ab498

Please sign in to comment.