Skip to content

Commit

Permalink
feat(pg): allow to modify user replication settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Serpentiel committed Sep 19, 2023
1 parent 698ccc4 commit 2e37e12
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ nav_order: 1

## [MAJOR.MINOR.PATCH] - YYYY-MM-DD

- Allow to modify `pg_user` replication settings

## [4.9.0] - 2023-09-18

- Fix IP Filter migrations error
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/aiven/terraform-provider-aiven

go 1.19
go 1.21.1

require (
github.com/aiven/aiven-go-client v1.36.0
Expand Down
25 changes: 19 additions & 6 deletions internal/sdkprovider/service/pg/pg_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ var aivenPGUserSchema = map[string]*schema.Schema{
Sensitive: true,
Computed: true,
DiffSuppressFunc: schemautil.EmptyObjectDiffSuppressFunc,
Description: "The password of the PG User ( not applicable for all services ).",
Description: "The password of the PG User (not applicable for all services).",
},
"pg_allow_replication": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: userconfig.Desc("Defines whether replication is allowed.").ForceNew().Build(),
Elem: &schema.Schema{
Type: schema.TypeBool,
},
Description: "Defines whether replication is allowed.",
},

// computed fields
Expand Down Expand Up @@ -128,6 +124,23 @@ func resourcePGUserUpdate(ctx context.Context, d *schema.ResourceData, m interfa
return diag.FromErr(err)
}

if d.HasChange("pg_allow_replication") {
allowReplication := d.Get("pg_allow_replication").(bool)

op := "set-access-control"

_, err = client.ServiceUsers.Update(projectName, serviceName, username,
aiven.ModifyServiceUserRequest{
AccessControl: &aiven.AccessControl{
PostgresAllowReplication: &allowReplication,
},
Operation: &op,
})
if err != nil {
return diag.FromErr(err)
}
}

return resourcePGUserRead(ctx, d, m)
}

Expand Down
86 changes: 86 additions & 0 deletions internal/sdkprovider/service/pg/pg_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ func TestAccAivenPGUser_pg_replica(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "pg_allow_replication", "true"),
),
},
{
Config: testAccPGUserPgReplicationDisableResource(rName),
Check: resource.ComposeTestCheckFunc(
schemautil.TestAccCheckAivenServiceUserAttributes("data.aiven_pg_user.user"),
resource.TestCheckResourceAttr(resourceName, "service_name", fmt.Sprintf("test-acc-sr-%s", rName)),
resource.TestCheckResourceAttr(resourceName, "project", os.Getenv("AIVEN_PROJECT_NAME")),
resource.TestCheckResourceAttr(resourceName, "username", fmt.Sprintf("user-%s", rName)),
resource.TestCheckResourceAttr(resourceName, "password", "Test$1234"),
resource.TestCheckResourceAttr(resourceName, "pg_allow_replication", "false"),
),
},
{
Config: testAccPGUserPgReplicationEnableResource(rName),
Check: resource.ComposeTestCheckFunc(
schemautil.TestAccCheckAivenServiceUserAttributes("data.aiven_pg_user.user"),
resource.TestCheckResourceAttr(resourceName, "service_name", fmt.Sprintf("test-acc-sr-%s", rName)),
resource.TestCheckResourceAttr(resourceName, "project", os.Getenv("AIVEN_PROJECT_NAME")),
resource.TestCheckResourceAttr(resourceName, "username", fmt.Sprintf("user-%s", rName)),
resource.TestCheckResourceAttr(resourceName, "password", "Test$1234"),
resource.TestCheckResourceAttr(resourceName, "pg_allow_replication", "true"),
),
},
},
})
}
Expand Down Expand Up @@ -143,6 +165,70 @@ data "aiven_pg_user" "user" {
}`, os.Getenv("AIVEN_PROJECT_NAME"), name, name)
}

func testAccPGUserPgReplicationDisableResource(name string) string {
return fmt.Sprintf(`
data "aiven_project" "foo" {
project = "%s"
}
resource "aiven_pg" "bar" {
project = data.aiven_project.foo.project
cloud_name = "google-europe-west1"
plan = "startup-4"
service_name = "test-acc-sr-%s"
}
resource "aiven_pg_user" "foo" {
service_name = aiven_pg.bar.service_name
project = aiven_pg.bar.project
username = "user-%s"
password = "Test$1234"
pg_allow_replication = false
depends_on = [aiven_pg.bar]
}
data "aiven_pg_user" "user" {
service_name = aiven_pg_user.foo.service_name
project = aiven_pg_user.foo.project
username = aiven_pg_user.foo.username
depends_on = [aiven_pg_user.foo]
}`, os.Getenv("AIVEN_PROJECT_NAME"), name, name)
}

func testAccPGUserPgReplicationEnableResource(name string) string {
return fmt.Sprintf(`
data "aiven_project" "foo" {
project = "%s"
}
resource "aiven_pg" "bar" {
project = data.aiven_project.foo.project
cloud_name = "google-europe-west1"
plan = "startup-4"
service_name = "test-acc-sr-%s"
}
resource "aiven_pg_user" "foo" {
service_name = aiven_pg.bar.service_name
project = aiven_pg.bar.project
username = "user-%s"
password = "Test$1234"
pg_allow_replication = true
depends_on = [aiven_pg.bar]
}
data "aiven_pg_user" "user" {
service_name = aiven_pg_user.foo.service_name
project = aiven_pg_user.foo.project
username = aiven_pg_user.foo.username
depends_on = [aiven_pg_user.foo]
}`, os.Getenv("AIVEN_PROJECT_NAME"), name, name)
}

func testAccPGUserNewPasswordResource(name string) string {
return fmt.Sprintf(`
data "aiven_project" "foo" {
Expand Down

0 comments on commit 2e37e12

Please sign in to comment.