Skip to content

Commit

Permalink
refactor(alloydbomni_user): rename alloydbomni_allow_replication
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Dec 3, 2024
1 parent f3f56a7 commit b030c32
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/data-sources/alloydbomni_user.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ the `PROVIDER_AIVEN_ENABLE_BETA` environment variable to use the resource.

- `access_cert` (String, Sensitive) The access certificate for the servie user.
- `access_key` (String, Sensitive) The access certificate key for the service user.
- `alloydbomni_allow_replication` (Boolean) Allows replication. For the default avnadmin user this attribute is required and is always `true`.
- `id` (String) The ID of this resource.
- `password` (String, Sensitive) The password of the service user.
- `pg_allow_replication` (Boolean) Allows replication. For the default avnadmin user this attribute is required and is always `true`.
- `type` (String) The service user account type, either primary or regular.
2 changes: 1 addition & 1 deletion docs/resources/alloydbomni_user.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ the `PROVIDER_AIVEN_ENABLE_BETA` environment variable to use the resource.

### Optional

- `alloydbomni_allow_replication` (Boolean) Allows replication. For the default avnadmin user this attribute is required and is always `true`.
- `password` (String, Sensitive) The password of the service user.
- `pg_allow_replication` (Boolean) Allows replication. For the default avnadmin user this attribute is required and is always `true`.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only
Expand Down
24 changes: 18 additions & 6 deletions internal/sdkprovider/service/alloydbomni/alloydbomni_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package alloydbomni

import (
"context"
"fmt"

avngen "github.com/aiven/go-client-codegen"
"github.com/aiven/go-client-codegen/handler/service"
Expand Down Expand Up @@ -30,7 +31,7 @@ var aivenAlloyDBOmniUserSchema = map[string]*schema.Schema{
DiffSuppressFunc: schemautil.EmptyObjectDiffSuppressFunc,
Description: "The password of the service user.",
},
"alloydbomni_allow_replication": {
"pg_allow_replication": {
Type: schema.TypeBool,
Optional: true,
Description: "Allows replication. For the default avnadmin user this attribute is required and is always `true`.",
Expand Down Expand Up @@ -75,9 +76,20 @@ func ResourceAlloyDBOmniUser() *schema.Resource {
func resourceAlloyDBOmniUserCreate(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
projectName := d.Get("project").(string)
serviceName := d.Get("service_name").(string)

// Validates that the service is an AlloyDBOmni service
alloydb, err := client.ServiceGet(ctx, projectName, serviceName)
if err != nil {
return err
}

if alloydb.ServiceType != schemautil.ServiceTypeAlloyDBOmni {
return fmt.Errorf("expected service type %q, got %q", schemautil.ServiceTypeAlloyDBOmni, alloydb.ServiceType)
}

username := d.Get("username").(string)
allowReplication := d.Get("alloydbomni_allow_replication").(bool)
_, err := client.ServiceUserCreate(
allowReplication := d.Get("pg_allow_replication").(bool)
_, err = client.ServiceUserCreate(
ctx,
projectName,
serviceName,
Expand Down Expand Up @@ -127,8 +139,8 @@ func resourceAlloyDBOmniUserUpdate(ctx context.Context, d *schema.ResourceData,
return err
}

if d.HasChange("alloydbomni_allow_replication") {
allowReplication := d.Get("alloydbomni_allow_replication").(bool)
if d.HasChange("pg_allow_replication") {
allowReplication := d.Get("pg_allow_replication").(bool)
_, err = client.ServiceUserCredentialsModify(
ctx, projectName, serviceName, username,
&service.ServiceUserCredentialsModifyIn{
Expand Down Expand Up @@ -163,7 +175,7 @@ func resourceAlloyDBOmniUserRead(ctx context.Context, d *schema.ResourceData, cl
}

if user.AccessControl != nil && user.AccessControl.PgAllowReplication != nil {
err = d.Set("alloydbomni_allow_replication", *user.AccessControl.PgAllowReplication)
err = d.Set("pg_allow_replication", *user.AccessControl.PgAllowReplication)
if err != nil {
return err
}
Expand Down
36 changes: 18 additions & 18 deletions internal/sdkprovider/service/alloydbomni/alloydbomni_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestAccAivenAlloyDBOmniUser_alloydbomni_replica(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "project", project),
resource.TestCheckResourceAttr(resourceName, "username", fmt.Sprintf("user-%s", rName)),
resource.TestCheckResourceAttr(resourceName, "password", "Test$1234"),
resource.TestCheckResourceAttr(resourceName, "alloydbomni_allow_replication", "true"),
resource.TestCheckResourceAttr(resourceName, "pg_allow_replication", "true"),
),
},
{
Expand All @@ -89,7 +89,7 @@ func TestAccAivenAlloyDBOmniUser_alloydbomni_replica(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "project", project),
resource.TestCheckResourceAttr(resourceName, "username", fmt.Sprintf("user-%s", rName)),
resource.TestCheckResourceAttr(resourceName, "password", "Test$1234"),
resource.TestCheckResourceAttr(resourceName, "alloydbomni_allow_replication", "false"),
resource.TestCheckResourceAttr(resourceName, "pg_allow_replication", "false"),
),
},
{
Expand All @@ -100,7 +100,7 @@ func TestAccAivenAlloyDBOmniUser_alloydbomni_replica(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "project", project),
resource.TestCheckResourceAttr(resourceName, "username", fmt.Sprintf("user-%s", rName)),
resource.TestCheckResourceAttr(resourceName, "password", "Test$1234"),
resource.TestCheckResourceAttr(resourceName, "alloydbomni_allow_replication", "true"),
resource.TestCheckResourceAttr(resourceName, "pg_allow_replication", "true"),
),
},
},
Expand Down Expand Up @@ -153,11 +153,11 @@ resource "aiven_alloydbomni" "bar" {
}
resource "aiven_alloydbomni_user" "foo" {
service_name = aiven_alloydbomni.bar.service_name
project = aiven_alloydbomni.bar.project
username = "user-%s"
password = "Test$1234"
alloydbomni_allow_replication = true
service_name = aiven_alloydbomni.bar.service_name
project = aiven_alloydbomni.bar.project
username = "user-%s"
password = "Test$1234"
pg_allow_replication = true
depends_on = [aiven_alloydbomni.bar]
}
Expand Down Expand Up @@ -185,11 +185,11 @@ resource "aiven_alloydbomni" "bar" {
}
resource "aiven_alloydbomni_user" "foo" {
service_name = aiven_alloydbomni.bar.service_name
project = aiven_alloydbomni.bar.project
username = "user-%s"
password = "Test$1234"
alloydbomni_allow_replication = false
service_name = aiven_alloydbomni.bar.service_name
project = aiven_alloydbomni.bar.project
username = "user-%s"
password = "Test$1234"
pg_allow_replication = false
depends_on = [aiven_alloydbomni.bar]
}
Expand Down Expand Up @@ -217,11 +217,11 @@ resource "aiven_alloydbomni" "bar" {
}
resource "aiven_alloydbomni_user" "foo" {
service_name = aiven_alloydbomni.bar.service_name
project = aiven_alloydbomni.bar.project
username = "user-%s"
password = "Test$1234"
alloydbomni_allow_replication = true
service_name = aiven_alloydbomni.bar.service_name
project = aiven_alloydbomni.bar.project
username = "user-%s"
password = "Test$1234"
pg_allow_replication = true
depends_on = [aiven_alloydbomni.bar]
}
Expand Down
13 changes: 12 additions & 1 deletion internal/sdkprovider/service/pg/pg_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,20 @@ func resourcePGUserCreate(ctx context.Context, d *schema.ResourceData, m interfa

projectName := d.Get("project").(string)
serviceName := d.Get("service_name").(string)

// Validates that the service is an Pg service
pg, err := client.Services.Get(ctx, projectName, serviceName)
if err != nil {
return diag.FromErr(err)
}

if pg.Type != schemautil.ServiceTypePG {
return diag.Errorf("expected service type %q, got %q", schemautil.ServiceTypePG, pg.Type)
}

username := d.Get("username").(string)
allowReplication := d.Get("pg_allow_replication").(bool)
_, err := client.ServiceUsers.Create(
_, err = client.ServiceUsers.Create(
ctx,
projectName,
serviceName,
Expand Down

0 comments on commit b030c32

Please sign in to comment.