Skip to content

Commit

Permalink
Allow editing enable_backups for Replicas (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelaossa authored Jan 21, 2025
1 parent b6ab304 commit e324ad5
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
49 changes: 48 additions & 1 deletion aptible/resource_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func resourceReplica() *schema.Resource {
Computed: true,
Sensitive: true,
},
"enable_backups": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
}
}
Expand All @@ -83,6 +88,7 @@ func resourceReplicaCreate(d *schema.ResourceData, meta interface{}) error {
containerSize := int32(d.Get("container_size").(int))
diskSize := int32(d.Get("disk_size").(int))
profile := d.Get("container_profile").(string)
enableBackups := d.Get("enable_backups").(bool)
envID := int32(d.Get("env_id").(int))
ctx := context.Background()
ctx = meta.(*providerMetadata).APIContext(ctx)
Expand Down Expand Up @@ -140,6 +146,22 @@ func resourceReplicaCreate(d *schema.ResourceData, meta interface{}) error {

_ = d.Set("replica_id", repl.ID)
d.SetId(strconv.Itoa(int(repl.ID)))

// Enable backups defaults to `true` in the backend so we only need to do something if it is being set to false.
// A replica is created in the backend via sweetness, so we need to wait until after the replication is complete to update the db object.
if !enableBackups {
_, err := client.
DatabasesAPI.
PatchDatabase(ctx, int32(repl.ID)).
UpdateDatabaseRequest(
aptibleapi.UpdateDatabaseRequest{EnableBackups: &enableBackups},
).
Execute()
if err != nil {
return err
}
}

return resourceReplicaRead(d, meta)
}

Expand Down Expand Up @@ -198,6 +220,7 @@ func resourceReplicaRead(d *schema.ResourceData, meta interface{}) error {
_ = d.Set("primary_database_id", primaryDatabaseID)
_ = d.Set("container_profile", profile)
_ = d.Set("iops", database.Embedded.Disk.GetProvisionedIops())
_ = d.Set("enable_backups", database.GetEnableBackups())
d.SetId(strconv.Itoa(int(database.Id)))

return nil
Expand All @@ -214,24 +237,48 @@ func resourceReplicaUpdate(ctx context.Context, d *schema.ResourceData, meta int
diskSize := int32(d.Get("disk_size").(int))
iops := int32(d.Get("iops").(int))
handle := d.Get("handle").(string)
enableBackups := d.Get("enable_backups").(bool)
needsOperation := false
var diags diag.Diagnostics

ctx = meta.(*providerMetadata).APIContext(ctx)
payload := aptibleapi.NewCreateOperationRequest("restart")

if d.HasChange("container_size") {
needsOperation = true
payload.SetContainerSize(containerSize)
}
if d.HasChange("iops") {
needsOperation = true
payload.SetProvisionedIops(iops)
}
if d.HasChange("container_profile") {
needsOperation = true
payload.SetInstanceProfile(profile)
}
if d.HasChange("disk_size") {
needsOperation = true
payload.SetDiskSize(diskSize)
}

if d.HasChange("enable_backups") {
_, err := client.
DatabasesAPI.
PatchDatabase(ctx, databaseID).
UpdateDatabaseRequest(
aptibleapi.UpdateDatabaseRequest{EnableBackups: &enableBackups},
).
Execute()
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "There was an error when trying to set enable_backups.",
Detail: err.Error(),
})
return diags
}
}

if d.HasChange("handle") {
_, err := client.
DatabasesAPI.
Expand All @@ -250,7 +297,7 @@ func resourceReplicaUpdate(ctx context.Context, d *schema.ResourceData, meta int
}
}

if d.HasChangeExcept("handle") {
if needsOperation {
op, _, err := client.
OperationsAPI.
CreateOperationForDatabase(ctx, databaseID).
Expand Down
62 changes: 62 additions & 0 deletions aptible/resource_replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,48 @@ func TestAccResourceReplica_basic(t *testing.T) {
})
}

func TestAccResourceReplica_withoutBackups(t *testing.T) {
dbHandle := acctest.RandString(10)
replicaHandle := acctest.RandString(10)

WithTestAccEnvironment(t, func(env aptible.Environment) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckReplicaDestroy,
Steps: []resource.TestStep{
{
Config: testAccAptibleReplicaWithoutBackups(env.ID, dbHandle, replicaHandle),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aptible_database.test", "handle", dbHandle),
resource.TestCheckResourceAttr("aptible_database.test", "env_id", strconv.Itoa(int(env.ID))),
resource.TestCheckResourceAttr("aptible_database.test", "database_type", "postgresql"),
resource.TestCheckResourceAttr("aptible_database.test", "disk_size", "10"),
resource.TestCheckResourceAttr("aptible_database.test", "enable_backups", "true"),
resource.TestCheckResourceAttrSet("aptible_database.test", "database_id"),
resource.TestCheckResourceAttrSet("aptible_database.test", "default_connection_url"),

resource.TestCheckResourceAttr("aptible_replica.test", "handle", replicaHandle),
resource.TestCheckResourceAttr("aptible_replica.test", "env_id", strconv.Itoa(int(env.ID))),
resource.TestCheckResourceAttr("aptible_replica.test", "container_size", "1024"),
resource.TestCheckResourceAttr("aptible_replica.test", "iops", "3000"),
resource.TestCheckResourceAttr("aptible_replica.test", "container_profile", "m5"),
resource.TestCheckResourceAttr("aptible_replica.test", "disk_size", "10"),
resource.TestCheckResourceAttr("aptible_replica.test", "enable_backups", "false"),
resource.TestCheckResourceAttrSet("aptible_replica.test", "replica_id"),
resource.TestCheckResourceAttrSet("aptible_replica.test", "default_connection_url"),
),
},
{
ResourceName: "aptible_database.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
})
}

func TestAccResourceReplica_update(t *testing.T) {
dbHandle := acctest.RandString(10)
replicaHandle := acctest.RandString(10)
Expand All @@ -72,6 +114,7 @@ func TestAccResourceReplica_update(t *testing.T) {
resource.TestCheckResourceAttr("aptible_database.test", "database_type", "postgresql"),
resource.TestCheckResourceAttr("aptible_database.test", "container_size", "1024"),
resource.TestCheckResourceAttr("aptible_database.test", "disk_size", "10"),
resource.TestCheckResourceAttr("aptible_database.test", "enable_backups", "true"),
resource.TestCheckResourceAttrSet("aptible_database.test", "database_id"),
resource.TestCheckResourceAttrSet("aptible_database.test", "default_connection_url"),

Expand All @@ -81,6 +124,7 @@ func TestAccResourceReplica_update(t *testing.T) {
resource.TestCheckResourceAttr("aptible_replica.test", "container_profile", "m5"),
resource.TestCheckResourceAttr("aptible_replica.test", "iops", "3000"),
resource.TestCheckResourceAttr("aptible_replica.test", "disk_size", "10"),
resource.TestCheckResourceAttr("aptible_replica.test", "enable_backups", "true"),
resource.TestCheckResourceAttrSet("aptible_replica.test", "replica_id"),
resource.TestCheckResourceAttrSet("aptible_replica.test", "default_connection_url"),
),
Expand All @@ -97,6 +141,7 @@ func TestAccResourceReplica_update(t *testing.T) {
resource.TestCheckResourceAttr("aptible_replica.test", "iops", "4000"),
resource.TestCheckResourceAttr("aptible_replica.test", "container_size", "512"),
resource.TestCheckResourceAttr("aptible_replica.test", "disk_size", "20"),
resource.TestCheckResourceAttr("aptible_replica.test", "enable_backups", "false"),
),
},
},
Expand Down Expand Up @@ -225,6 +270,22 @@ func testAccAptibleReplicaBasic(envId int64, dbHandle string, replicaHandle stri
`, envId, dbHandle, envId, replicaHandle)
}

func testAccAptibleReplicaWithoutBackups(envId int64, dbHandle string, replicaHandle string) string {
return fmt.Sprintf(`
resource "aptible_database" "test" {
env_id = %d
handle = "%v"
}
resource "aptible_replica" "test" {
env_id = %d
handle = "%v"
primary_database_id = aptible_database.test.database_id
enable_backups = false
}
`, envId, dbHandle, envId, replicaHandle)
}

func testAccAptibleReplicaUpdate(envId int64, dbHandle string, repHandle string) string {
return fmt.Sprintf(`
resource "aptible_database" "test" {
Expand All @@ -240,6 +301,7 @@ func testAccAptibleReplicaUpdate(envId int64, dbHandle string, repHandle string)
disk_size = %d
container_profile = "r5"
iops = 4000
enable_backups = false
}
`, envId, dbHandle, envId, repHandle, 512, 20)
}
Expand Down

0 comments on commit e324ad5

Please sign in to comment.