Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat.] RDS: PITR for existing rds instance #2778

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions docs/resources/rds_instance_v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,12 @@ The following arguments are supported:

* `tags` - (Optional) Tags key/value pairs to associate with the instance.

* `restore_point` - (Optional, ForceNew) Specifies the restoration information. By selecting this option a new RDS
instance will be created from separate instance backup. Structure is documented below.
* `restore_point` - (Optional, ForceNew) Specifies the restoration information. By selecting this option you can either
create a new RDS instance or restore backup from existing one. Structure is documented below.

* `restore_from_backup` - (Optional) Specifies whether to restore database to an instance described in current resource.
* `restore_from_backup` **DEPRECATED** - (Optional) Specifies whether to restore database to an instance described in current resource.
Structure is documented below.
Please use alternative parameter `restore_point`.

* `ssl_enable` - (Optional) Specifies whether SSL should be enabled for MySql instances.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ func TestAccRdsInstanceV3ElasticIP(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckRdsInstanceV3Exists(instanceV3ResourceName, &rdsInstance),
resource.TestCheckResourceAttr(instanceV3ResourceName, "name", "tf_rds_instance_"+postfix),
resource.TestCheckResourceAttr(instanceV3ResourceName, "db.0.version", "10"),
resource.TestCheckResourceAttr(instanceV3ResourceName, "db.0.version", "15"),
resource.TestCheckResourceAttr(instanceV3ResourceName, "public_ips.#", "1"),
),
},
{
Config: testAccRdsInstanceV3Basic(postfix),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(instanceV3ResourceName, "db.0.version", "10"),
resource.TestCheckResourceAttr(instanceV3ResourceName, "db.0.version", "15"),
resource.TestCheckResourceAttr(instanceV3ResourceName, "public_ips.#", "0"),
),
},
Expand Down Expand Up @@ -350,7 +350,7 @@ func TestAccRdsInstanceV3AutoScaling(t *testing.T) {
})
}

func TestAccRdsInstanceV3RestoreToPITR(t *testing.T) {
func TestAccRdsInstanceV3RestoreToPITR_NewInstance(t *testing.T) {
postfix := acctest.RandString(3)
var rdsInstance instances.InstanceResponse

Expand All @@ -369,13 +369,37 @@ func TestAccRdsInstanceV3RestoreToPITR(t *testing.T) {
Config: testAccRdsInstanceV3RestorePITRUpdate(postfix),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(instanceV3ResourceName, "flavor", "rds.pg.c2.large"),
resource.TestCheckResourceAttrSet(instanceV3ResourceName, "restored_backup_id"),
),
},
{
Config: testAccRdsInstanceV3RestorePITRBasic(postfix),
Check: resource.ComposeTestCheckFunc(
testAccCheckRdsInstanceV3Exists(instanceV3ResourceName, &rdsInstance),
),
},
},
})
}

func TestAccRdsInstanceV3RestoreToPITR_ExistingInstance(t *testing.T) {
postfix := acctest.RandString(3)
var rdsInstance instances.InstanceResponse

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckRdsInstanceV3Destroy,
Steps: []resource.TestStep{
{
Config: testAccRdsInstanceV3RestorePITRBasic(postfix),
Check: resource.ComposeTestCheckFunc(
testAccCheckRdsInstanceV3Exists(instanceV3ResourceName, &rdsInstance),
),
},
{
Config: testAccRdsInstanceV3RestorePITRUpdate(postfix),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(instanceV3ResourceName, "flavor", "rds.pg.c2.large"),
resource.TestCheckResourceAttrSet(instanceV3ResourceName, "restored_backup_id"),
),
},
Expand Down Expand Up @@ -539,7 +563,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "15"
port = "8635"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
Expand Down Expand Up @@ -602,7 +626,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "15"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
Expand All @@ -627,7 +651,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "15"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
Expand Down Expand Up @@ -658,7 +682,7 @@ resource "opentelekomcloud_rds_parametergroup_v3" "pg" {
}
datastore {
type = "postgresql"
version = "12"
version = "15"
}
}

Expand All @@ -670,7 +694,7 @@ resource "opentelekomcloud_rds_parametergroup_v3" "pg2" {
}
datastore {
type = "postgresql"
version = "12"
version = "15"
}
}

Expand All @@ -680,7 +704,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "12"
version = "15"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
Expand Down Expand Up @@ -708,7 +732,7 @@ resource "opentelekomcloud_rds_parametergroup_v3" "pg" {
}
datastore {
type = "postgresql"
version = "10"
version = "15"
}
}

Expand All @@ -720,7 +744,7 @@ resource "opentelekomcloud_rds_parametergroup_v3" "pg2" {
}
datastore {
type = "postgresql"
version = "12"
version = "15"
}
}

Expand All @@ -730,7 +754,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "12"
version = "15"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
Expand All @@ -757,7 +781,7 @@ resource "opentelekomcloud_rds_parametergroup_v3" "pg" {
}
datastore {
type = "postgresql"
version = "10"
version = "15"
}
}

Expand All @@ -768,7 +792,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "15"
port = "8635"
}

Expand Down Expand Up @@ -799,7 +823,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "15"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
Expand All @@ -824,7 +848,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "15"
port = "8635"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
Expand Down Expand Up @@ -863,7 +887,7 @@ resource "opentelekomcloud_rds_instance_v3" "from_backup" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "15"
port = "8635"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
Expand Down Expand Up @@ -1020,7 +1044,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "15"
port = "8635"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
Expand All @@ -1039,7 +1063,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance_2" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "15"
port = "8635"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
Expand Down Expand Up @@ -1071,7 +1095,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "15"
port = "8635"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
Expand All @@ -1082,10 +1106,9 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
size = 40
}
flavor = "rds.pg.c2.large"
restore_from_backup {
source_instance_id = opentelekomcloud_rds_backup_v3.test.instance_id
backup_id = opentelekomcloud_rds_backup_v3.test.id
type = "backup"
restore_point {
instance_id = opentelekomcloud_rds_backup_v3.test.instance_id
backup_id = opentelekomcloud_rds_backup_v3.test.id
}


Expand All @@ -1097,7 +1120,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance_2" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "15"
port = "8635"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,35 +65,32 @@ func ResourceRdsInstanceV3() *schema.Resource {
"restore_point": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"restore_time": {
Type: schema.TypeInt,
Optional: true,
ExactlyOneOf: []string{"restore_point.0.backup_id"},
ForceNew: true,
},
"backup_id": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"restore_point.0.restore_time"},
ForceNew: true,
},
},
},
},
"restore_from_backup": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Computed: false,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Computed: false,
Deprecated: "Use `restore_point` instead",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"source_instance_id": {
Expand Down Expand Up @@ -869,6 +866,41 @@ func resourceRdsInstanceV3Update(ctx context.Context, d *schema.ResourceData, me
}
}

if d.HasChange("restore_point") {
rawPitr := d.Get("restore_point").([]interface{})
if len(rawPitr) > 0 {
pitr := rawPitr[0].(map[string]interface{})
pitrOpts := backups.RestorePITROpts{
Source: backups.Source{
BackupID: pitr["backup_id"].(string),
InstanceID: pitr["instance_id"].(string),
RestoreTime: int64(pitr["restore_time"].(int)),
},
Target: backups.Target{
InstanceID: d.Id(),
},
}
if pitrOpts.Source.BackupID != "" {
pitrOpts.Source.Type = "backup"
} else {
pitrOpts.Source.Type = "timestamp"
}

_, err = backups.RestorePITR(client, pitrOpts)
if err != nil {
return fmterr.Errorf("error in point in time restoration: %s ", err)
}

// Additional sleep is required to handle state transitions during PITR operations.
// During PITR application, the backend may undergo 2 sequential state changes instead of 1.
// Current waitForStateAvailable function terminates after detecting the first "Available" state.
time.Sleep(20 * time.Second)
if err := instances.WaitForStateAvailable(client, 1200, d.Id()); err != nil {
return diag.FromErr(err)
}
}
}

if d.HasChange("security_group_id") {
updateOpts := security.SetSecurityGroupOpts{
InstanceId: d.Id(),
Expand Down
10 changes: 10 additions & 0 deletions releasenotes/notes/rds_pitr-3efd980342aad212.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
enhancements:
- |
**[RDS]** Updated ``restore_point`` argument of ``opentelekomcloud_rds_instance_v3`` resource allowing to
restore RDS instance backup to existing RDS instance
(`#2778 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2778>`_)
deprecations:
- |
**[RDS]** Parameter ``restore_from_backup`` is deprecated in favor of ``restore_point`` for ``resource/opentelekomcloud_rds_instance_v3``
(`#2778 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2778>`_)
Loading