Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions internal/services/rdb/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package rdb
import (
"context"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -106,3 +109,19 @@ func getIPConfigUpdate(d *schema.ResourceData, ipFieldName string) (ipamConfig *

return ipamConfig, staticConfig
}

func ExtractEngineVersion(engine string) (int, error) {
re := regexp.MustCompile(`[-](\d+)`)
matches := re.FindStringSubmatch(engine)

if len(matches) < 2 {
return 0, fmt.Errorf("no version found in: %s", engine)
}

version, err := strconv.Atoi(matches[1])
if err != nil {
return 0, fmt.Errorf("failed to convert version to integer: %w", err)
}

return version, nil
}
36 changes: 36 additions & 0 deletions internal/services/rdb/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,39 @@ func TestPrivilegeV1SchemaUpgradeFunc(t *testing.T) {
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", v1Schema, actual)
}
}

func TestExtractEngineVersion(t *testing.T) {
tests := []struct {
engine string
expected int
expectingError bool
}{
{"postgresql-15", 15, false},
{"mysql-8.0", 8, false},
{"redis-6", 6, false},
{"mariadb-10.5", 10, false}, // Only extracts the major version
{"invalid-engine", 0, true}, // No version to extract
{"", 0, true}, // Empty string case
{"mongodb-3.6", 3, false}, // Extracts only the major version
}

for _, tt := range tests {
t.Run(tt.engine, func(t *testing.T) {
result, err := rdb.ExtractEngineVersion(tt.engine)

if tt.expectingError {
if err == nil {
t.Errorf("expected an error for engine %q, but got none", tt.engine)
}
} else {
if err != nil {
t.Errorf("did not expect an error for engine %q, but got: %s", tt.engine, err)
}

if result != tt.expected {
t.Errorf("expected version %d for engine %q, but got %d", tt.expected, tt.engine, result)
}
}
})
}
}
24 changes: 23 additions & 1 deletion internal/services/rdb/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
rdbtestfuncs "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/rdb/testfuncs"

Check failure on line 21 in internal/services/rdb/instance.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/rdb/testfuncs" imported as rdbtestfuncs and not used

Check failure on line 21 in internal/services/rdb/instance.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/rdb/testfuncs" imported as rdbtestfuncs and not used
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)
Expand Down Expand Up @@ -577,8 +578,13 @@
}

////////////////////
// Upgrade instance
// migrate instance
////////////////////

//if d.HasChange("engine") {
//
//}

upgradeInstanceRequests := []rdb.UpgradeInstanceRequest(nil)

rdbInstance, err := rdbAPI.GetInstance(&rdb.GetInstanceRequest{
Expand All @@ -592,6 +598,22 @@
diskIsFull := rdbInstance.Status == rdb.InstanceStatusDiskFull
volType := rdb.VolumeType(d.Get("volume_type").(string))

if d.HasChange("engine") {

lastEngine, err := ExtractEngineVersion(rdbInstance.Engine)
if err != nil {
return diag.FromErr(err)
}
newEngine, err := ExtractEngineVersion(d.Get("engine").(string))
if newEngine <= lastEngine {
return diag.FromErr(errors.New("volume_size_in_gb must be a multiple of 5"))
}
engine := rdbAPI.FetchLatestEngineVersion()

Check failure on line 611 in internal/services/rdb/instance.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

not enough arguments in call to rdbAPI.FetchLatestEngineVersion

Check failure on line 611 in internal/services/rdb/instance.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

assignment mismatch: 1 variable but rdbAPI.FetchLatestEngineVersion returns 2 values

Check failure on line 611 in internal/services/rdb/instance.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

declared and not used: engine

Check failure on line 611 in internal/services/rdb/instance.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

not enough arguments in call to rdbAPI.FetchLatestEngineVersion

Check failure on line 611 in internal/services/rdb/instance.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

assignment mismatch: 1 variable but rdbAPI.FetchLatestEngineVersion returns 2 values

Check failure on line 611 in internal/services/rdb/instance.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

declared and not used: engine

}
////////////////////
// Upgrade instance
////////////////////
// Volume type and size
if d.HasChanges("volume_type", "volume_size_in_gb") {
switch volType {
Expand Down
Loading