Skip to content

Commit

Permalink
Support scalable storage settings separately for read-replica vs. pri…
Browse files Browse the repository at this point in the history
…mary (#1103)

* Support storage_size_mib settings directly on read-replicas

* fix tests, update mongo version to 6

* update godo version

* fix terrafmt

* fix test
  • Loading branch information
dweinshenker authored Jan 18, 2024
1 parent 5865783 commit 91272bf
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 16 deletions.
5 changes: 5 additions & 0 deletions digitalocean/database/datasource_database_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func DataSourceDigitalOceanDatabaseReplica() *schema.Resource {
},
Set: util.HashStringIgnoreCase,
},

"storage_size_mib": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down
2 changes: 2 additions & 0 deletions digitalocean/database/datasource_database_replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func TestAccDataSourceDigitalOceanDatabaseReplica_Basic(t *testing.T) {
"data.digitalocean_database_replica.my_db_replica", "tags.#", "1"),
resource.TestCheckResourceAttrSet(
"data.digitalocean_database_replica.my_db_replica", "private_network_uuid"),
resource.TestCheckResourceAttr(
"data.digitalocean_database_replica.my_db_replica", "storage_size_mib", "30720"),
),
},
},
Expand Down
6 changes: 5 additions & 1 deletion digitalocean/database/resource_database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,11 @@ func resourceDigitalOceanDatabaseClusterUpdate(ctx context.Context, d *schema.Re
SizeSlug: d.Get("size").(string),
NumNodes: d.Get("node_count").(int),
}
if v, ok := d.GetOk("storage_size_mib"); ok {

// only include the storage_size_mib in the resize request if it has changed
// this avoids invalid values when plans sizes are increasing that require higher base levels of storage
// excluding this parameter will utilize default base storage levels for the given plan size
if v, ok := d.GetOk("storage_size_mib"); ok && d.HasChange("storage_size_mib") {
v, err := strconv.ParseUint(v.(string), 10, 64)
if err == nil {
opts.StorageSizeMib = v
Expand Down
2 changes: 1 addition & 1 deletion digitalocean/database/resource_database_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ const testAccCheckDigitalOceanDatabaseClusterConfigMongoDB = `
resource "digitalocean_database_cluster" "foobar" {
name = "%s"
engine = "mongodb"
version = "4"
version = "6"
size = "db-s-1vcpu-1gb"
region = "nyc3"
node_count = 1
Expand Down
24 changes: 23 additions & 1 deletion digitalocean/database/resource_database_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -122,6 +123,12 @@ func ResourceDigitalOceanDatabaseReplica() *schema.Resource {
},
Set: util.HashStringIgnoreCase,
},

"storage_size_mib": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
Expand All @@ -141,6 +148,13 @@ func resourceDigitalOceanDatabaseReplicaCreate(ctx context.Context, d *schema.Re
opts.PrivateNetworkUUID = v.(string)
}

if v, ok := d.GetOk("storage_size_mib"); ok {
v, err := strconv.ParseUint(v.(string), 10, 64)
if err == nil {
opts.StorageSizeMib = v
}
}

log.Printf("[DEBUG] DatabaseReplica create configuration: %#v", opts)

var replicaCluster *godo.DatabaseReplica
Expand Down Expand Up @@ -213,6 +227,7 @@ func resourceDigitalOceanDatabaseReplicaRead(ctx context.Context, d *schema.Reso
d.Set("user", replica.Connection.User)
d.Set("password", replica.Connection.Password)
d.Set("private_network_uuid", replica.PrivateNetworkUUID)
d.Set("storage_size_mib", strconv.FormatUint(replica.StorageSizeMib, 10))

return nil
}
Expand All @@ -223,12 +238,19 @@ func resourceDigitalOceanDatabaseReplicaUpdate(ctx context.Context, d *schema.Re
replicaID := d.Get("uuid").(string)
replicaName := d.Get("name").(string)

if d.HasChanges("size") {
if d.HasChanges("size", "storage_size_mib") {
opts := &godo.DatabaseResizeRequest{
SizeSlug: d.Get("size").(string),
NumNodes: 1, // Read-only replicas only support a single node configuration.
}

if v, ok := d.GetOk("storage_size_mib"); ok {
v, err := strconv.ParseUint(v.(string), 10, 64)
if err == nil {
opts.StorageSizeMib = v
}
}

resp, err := client.Databases.Resize(context.Background(), replicaID, opts)
if err != nil {
if resp != nil && resp.StatusCode == 404 {
Expand Down
15 changes: 10 additions & 5 deletions digitalocean/database/resource_database_replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func TestAccDigitalOceanDatabaseReplica_Resize(t *testing.T) {
testAccCheckDigitalOceanDatabaseReplicaAttributes(&databaseReplica, databaseReplicaName),
resource.TestCheckResourceAttr(
"digitalocean_database_replica.read-01", "size", "db-s-1vcpu-2gb"),
resource.TestCheckResourceAttr(
"digitalocean_database_replica.read-01", "storage_size_mib", "30720"),
resource.TestCheckResourceAttr(
"digitalocean_database_replica.read-01", "name", databaseReplicaName),
resource.TestCheckResourceAttrSet(
Expand All @@ -147,6 +149,8 @@ func TestAccDigitalOceanDatabaseReplica_Resize(t *testing.T) {
testAccCheckDigitalOceanDatabaseReplicaAttributes(&databaseReplica, databaseReplicaName),
resource.TestCheckResourceAttr(
"digitalocean_database_replica.read-01", "size", "db-s-2vcpu-4gb"),
resource.TestCheckResourceAttr(
"digitalocean_database_replica.read-01", "storage_size_mib", "61440"),
resource.TestCheckResourceAttr(
"digitalocean_database_replica.read-01", "name", databaseReplicaName),
resource.TestCheckResourceAttrSet(
Expand Down Expand Up @@ -236,11 +240,12 @@ resource "digitalocean_database_replica" "read-01" {

const testAccCheckDigitalOceanDatabaseReplicaConfigResized = `
resource "digitalocean_database_replica" "read-01" {
cluster_id = digitalocean_database_cluster.foobar.id
name = "%s"
region = "nyc3"
size = "db-s-2vcpu-4gb"
tags = ["staging"]
cluster_id = digitalocean_database_cluster.foobar.id
name = "%s"
region = "nyc3"
size = "db-s-2vcpu-4gb"
storage_size_mib = 61440
tags = ["staging"]
}`

const testAccCheckDigitalOceanDatabaseReplicaConfigWithVPC = `
Expand Down
4 changes: 2 additions & 2 deletions digitalocean/database/resource_database_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ const testAccCheckDigitalOceanDatabaseUserConfigMongo = `
resource "digitalocean_database_cluster" "foobar" {
name = "%s"
engine = "mongodb"
version = "4"
version = "6"
size = "db-s-1vcpu-1gb"
region = "nyc1"
node_count = 1
Expand All @@ -397,7 +397,7 @@ const testAccCheckDigitalOceanDatabaseUserConfigMongoMultiUser = `
resource "digitalocean_database_cluster" "foobar" {
name = "%s"
engine = "mongodb"
version = "4"
version = "6"
size = "db-s-1vcpu-1gb"
region = "nyc1"
node_count = 1
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/database_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ resource "digitalocean_database_cluster" "kafka-example" {
resource "digitalocean_database_cluster" "mongodb-example" {
name = "example-mongo-cluster"
engine = "mongodb"
version = "4"
version = "6"
size = "db-s-1vcpu-1gb"
region = "nyc3"
node_count = 1
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/digitalocean/terraform-provider-digitalocean

require (
github.com/aws/aws-sdk-go v1.42.18
github.com/digitalocean/godo v1.107.0
github.com/digitalocean/godo v1.108.0
github.com/hashicorp/awspolicyequivalence v1.5.0
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/digitalocean/godo v1.107.0 h1:P72IbmGFQvKOvyjVLyT59bmHxilA4E5hWi40rF4zNQc=
github.com/digitalocean/godo v1.107.0/go.mod h1:R6EmmWI8CT1+fCtjWY9UCB+L5uufuZH13wk3YhxycCs=
github.com/digitalocean/godo v1.108.0 h1:fWyMENvtxpCpva1UbKzOFnyAS04N1FNuBWWfPeTGquQ=
github.com/digitalocean/godo v1.108.0/go.mod h1:R6EmmWI8CT1+fCtjWY9UCB+L5uufuZH13wk3YhxycCs=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
Expand Down
4 changes: 4 additions & 0 deletions vendor/github.com/digitalocean/godo/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/digitalocean/godo/databases.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/digitalocean/godo/godo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ github.com/aws/aws-sdk-go/service/sts/stsiface
# github.com/davecgh/go-spew v1.1.1
## explicit
github.com/davecgh/go-spew/spew
# github.com/digitalocean/godo v1.107.0
# github.com/digitalocean/godo v1.108.0
## explicit; go 1.20
github.com/digitalocean/godo
github.com/digitalocean/godo/metrics
Expand Down

0 comments on commit 91272bf

Please sign in to comment.