diff --git a/docs/resources/postgresql.md b/docs/resources/postgresql.md index bfcdfdb..53d87aa 100644 --- a/docs/resources/postgresql.md +++ b/docs/resources/postgresql.md @@ -13,7 +13,15 @@ Manage [PostgreSQL](https://www.postgresql.org/) product. See [product specification](https://www.clever-cloud.com/postgresql-hosting/). - +## Example Usage + +```terraform +resource "clevercloud_postgresql" "postgresql_database" { + name = "postgresql_database" + plan = "dev" + region = "par" +} +``` ## Schema diff --git a/examples/resources/clevercloud_postgresql/resource.tf b/examples/resources/clevercloud_postgresql/resource.tf new file mode 100644 index 0000000..f7454b2 --- /dev/null +++ b/examples/resources/clevercloud_postgresql/resource.tf @@ -0,0 +1,5 @@ +resource "clevercloud_postgresql" "postgresql_database" { + name = "postgresql_database" + plan = "dev" + region = "par" +} \ No newline at end of file diff --git a/pkg/resources/postgresql/resource_postgresql_crud.go b/pkg/resources/postgresql/resource_postgresql_crud.go index 4d39afc..c7dbfd1 100644 --- a/pkg/resources/postgresql/resource_postgresql_crud.go +++ b/pkg/resources/postgresql/resource_postgresql_crud.go @@ -121,6 +121,12 @@ func (r *ResourcePostgreSQL) Read(ctx context.Context, req resource.ReadRequest, } addonPG := addonPGRes.Payload() + + if addonPG.Status == "TO_DELETE" { + resp.State.RemoveResource(ctx) + return + } + tflog.Info(ctx, "STATE", map[string]interface{}{"pg": pg}) tflog.Info(ctx, "API", map[string]interface{}{"pg": addonPG}) pg.Plan = pkg.FromStr(addonPG.Plan) diff --git a/pkg/resources/postgresql/resource_postgresql_test.go b/pkg/resources/postgresql/resource_postgresql_test.go index f81eb3a..f0a1127 100644 --- a/pkg/resources/postgresql/resource_postgresql_test.go +++ b/pkg/resources/postgresql/resource_postgresql_test.go @@ -72,3 +72,54 @@ func TestAccPostgreSQL_basic(t *testing.T) { }}, }) } + +func TestAccPostgreSQL_RefreshDeleted(t *testing.T) { + rName := fmt.Sprintf("tf-test-pg-%d", time.Now().UnixMilli()) + //fullName := fmt.Sprintf("clevercloud_postgresql.%s", rName) + cc := client.New(client.WithAutoOauthConfig()) + org := os.Getenv("ORGANISATION") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { + if org == "" { + t.Fatalf("missing ORGANISATION env var") + } + }, + ProtoV6ProviderFactories: protoV6Provider, + CheckDestroy: func(state *terraform.State) error { + for _, resource := range state.RootModule().Resources { + res := tmp.GetPostgreSQL(context.Background(), cc, resource.Primary.ID) + if res.IsNotFoundError() { + continue + } + if res.HasError() { + return fmt.Errorf("unexpectd error: %s", res.Error().Error()) + } + if res.Payload().Status == "TO_DELETE" { + continue + } + + return fmt.Errorf("expect resource '%s' to be deleted", resource.Primary.ID) + } + return nil + }, + Steps: []resource.TestStep{ + // create a database instance on first step + { + ResourceName: rName, + Config: fmt.Sprintf(providerBlock, org) + fmt.Sprintf(postgresqlBlock, rName, rName), + }, + { + ResourceName: rName, + PreConfig: func() { + // delete the database using an api call + tmp.DeleteAddon(context.Background(), cc, org, rName) + }, + // refreshing state + RefreshState: true, + // plan should contain database re-creation + ExpectNonEmptyPlan: true, + }, + }, + }) +}