From c138553994d77ade4cb08f7a09b22a01cafdab85 Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Tue, 3 Dec 2024 15:29:46 +0100 Subject: [PATCH] refactor(alloydbomni_user): rename alloydbomni_allow_replication --- docs/data-sources/alloydbomni_user.md | 2 +- docs/resources/alloydbomni_user.md | 2 +- .../service/alloydbomni/alloydbomni_user.go | 24 ++++++++++++++----- .../alloydbomni/alloydbomni_user_test.go | 12 +++++----- internal/sdkprovider/service/pg/pg_user.go | 13 +++++++++- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/docs/data-sources/alloydbomni_user.md b/docs/data-sources/alloydbomni_user.md index 6c7662b63..9e4328335 100644 --- a/docs/data-sources/alloydbomni_user.md +++ b/docs/data-sources/alloydbomni_user.md @@ -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. diff --git a/docs/resources/alloydbomni_user.md b/docs/resources/alloydbomni_user.md index 49cfbba25..07c6fb1a4 100644 --- a/docs/resources/alloydbomni_user.md +++ b/docs/resources/alloydbomni_user.md @@ -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 diff --git a/internal/sdkprovider/service/alloydbomni/alloydbomni_user.go b/internal/sdkprovider/service/alloydbomni/alloydbomni_user.go index 65e59bf8f..8da0d0f63 100644 --- a/internal/sdkprovider/service/alloydbomni/alloydbomni_user.go +++ b/internal/sdkprovider/service/alloydbomni/alloydbomni_user.go @@ -2,6 +2,7 @@ package alloydbomni import ( "context" + "fmt" avngen "github.com/aiven/go-client-codegen" "github.com/aiven/go-client-codegen/handler/service" @@ -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`.", @@ -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, @@ -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{ @@ -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 } diff --git a/internal/sdkprovider/service/alloydbomni/alloydbomni_user_test.go b/internal/sdkprovider/service/alloydbomni/alloydbomni_user_test.go index 3ccb978bf..6bf6381cb 100644 --- a/internal/sdkprovider/service/alloydbomni/alloydbomni_user_test.go +++ b/internal/sdkprovider/service/alloydbomni/alloydbomni_user_test.go @@ -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"), ), }, { @@ -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"), ), }, { @@ -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"), ), }, }, @@ -157,7 +157,7 @@ resource "aiven_alloydbomni_user" "foo" { project = aiven_alloydbomni.bar.project username = "user-%s" password = "Test$1234" - alloydbomni_allow_replication = true + pg_allow_replication = true depends_on = [aiven_alloydbomni.bar] } @@ -189,7 +189,7 @@ resource "aiven_alloydbomni_user" "foo" { project = aiven_alloydbomni.bar.project username = "user-%s" password = "Test$1234" - alloydbomni_allow_replication = false + pg_allow_replication = false depends_on = [aiven_alloydbomni.bar] } @@ -221,7 +221,7 @@ resource "aiven_alloydbomni_user" "foo" { project = aiven_alloydbomni.bar.project username = "user-%s" password = "Test$1234" - alloydbomni_allow_replication = true + pg_allow_replication = true depends_on = [aiven_alloydbomni.bar] } diff --git a/internal/sdkprovider/service/pg/pg_user.go b/internal/sdkprovider/service/pg/pg_user.go index 69919d5f8..639eea661 100644 --- a/internal/sdkprovider/service/pg/pg_user.go +++ b/internal/sdkprovider/service/pg/pg_user.go @@ -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,