Skip to content

Commit

Permalink
fix: manage deleted databases (#51)
Browse files Browse the repository at this point in the history
* fix: manage deleted databases

This commit fixes the detection of deleted database with the status
'TO_DELETE'.

* chore: add example use for a postgresql_database resource
  • Loading branch information
juwit authored Jan 5, 2024
1 parent b0737ba commit 164bb21
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
10 changes: 9 additions & 1 deletion docs/resources/postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 generated by tfplugindocs -->
## Schema
Expand Down
5 changes: 5 additions & 0 deletions examples/resources/clevercloud_postgresql/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "clevercloud_postgresql" "postgresql_database" {
name = "postgresql_database"
plan = "dev"
region = "par"
}
6 changes: 6 additions & 0 deletions pkg/resources/postgresql/resource_postgresql_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions pkg/resources/postgresql/resource_postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
})
}

0 comments on commit 164bb21

Please sign in to comment.