Skip to content

Commit

Permalink
[RDS] Allow restoring backup to a new instance (#1624)
Browse files Browse the repository at this point in the history
[RDS] Allow restoring backup to a new instance

Summary of the Pull Request
Resolve #1456
PR Checklist

 Refers to: #1456
 Tests added/passed.
 Documentation updated.
 Schema updated.
 Release notes added.

Acceptance Steps Performed
=== RUN   TestAccRdsInstanceV3RestoreBackup
--- PASS: TestAccRdsInstanceV3RestoreBackup (1190.48s)
PASS

Process finished with the exit code 0

Reviewed-by: None <None>
Reviewed-by: Rodion Gyrbu <fpsoff@outlook.com>
  • Loading branch information
outcatcher committed Feb 8, 2022
1 parent d975216 commit 3dfcb58
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 46 deletions.
51 changes: 51 additions & 0 deletions docs/resources/rds_instance_v3.md
Expand Up @@ -221,6 +221,44 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
}
```

### Restore backup to a new instance

```hcl
data "opentelekomcloud_rds_backup_v3" "backup" {
instance_id = var.rds_instance_id
type = "auto"
}
resource "opentelekomcloud_rds_instance_v3" "from_backup" {
name = "instance-restored"
availability_zone = opentelekomcloud_rds_instance_v3.instance.availability_zone
flavor = "rds.pg.c2.medium"
restore_point {
instance_id = data.opentelekomcloud_rds_backup_v3.backup.instance_id
backup_id = data.opentelekomcloud_rds_backup_v3.backup.id
}
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
port = "8635"
}
security_group_id = var.security_group_id
subnet_id = var.os_network_id
vpc_id = var.os_router_id
volume {
type = "COMMON"
size = 40
}
backup_strategy {
start_time = "08:00-09:00"
keep_days = 1
}
}
```

## Argument Reference

The following arguments are supported:
Expand Down Expand Up @@ -273,6 +311,8 @@ The following arguments are supported:

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

* `restore_point` - (Optional) Specifies the restoration information.

The `db` block supports:

* `password` - (Required) Specifies the database password. The value cannot be
Expand Down Expand Up @@ -325,6 +365,17 @@ The `backup_strategy` block supports:
the same and must be set to any of the following: 00, 15, 30, or
45. Example value: 08:15-09:15 23:00-00:00.

The `restore_point` block supports:

* `instance_id` - (Required) Specifies the original DB instance ID.

* `backup_id` - (Optional) Specifies the ID of the backup used to restore data.

* `restore_time` - (Optional) Specifies the time point of data restoration in the UNIX timestamp.
The unit is millisecond and the time zone is UTC.

-> Exactly one of `backup_id` and `restore_time` needs to be set.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -14,7 +14,7 @@ require (
github.com/jen20/awspolicyequivalence v1.1.0
github.com/jinzhu/copier v0.2.3
github.com/mitchellh/go-homedir v1.1.0
github.com/opentelekomcloud/gophertelekomcloud v0.5.7
github.com/opentelekomcloud/gophertelekomcloud v0.5.8-0.20220208083513-62ba2ae51c2f
github.com/unknwon/com v1.0.1
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
gopkg.in/yaml.v2 v2.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -297,8 +297,8 @@ github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opentelekomcloud/gophertelekomcloud v0.5.7 h1:eHylmpKKEXaqgCAhhIRn5kxEcw9LaKRYr6VmwsMSg3g=
github.com/opentelekomcloud/gophertelekomcloud v0.5.7/go.mod h1:pzEP1kduNwv+hrI9R6/DFU/NiX7Kr9NiFjpQ7kJQTsM=
github.com/opentelekomcloud/gophertelekomcloud v0.5.8-0.20220208083513-62ba2ae51c2f h1://HhQdBZvYgWiwi4cfvB3+AgifsLdx2Q9iJYRyP+jPc=
github.com/opentelekomcloud/gophertelekomcloud v0.5.8-0.20220208083513-62ba2ae51c2f/go.mod h1:pzEP1kduNwv+hrI9R6/DFU/NiX7Kr9NiFjpQ7kJQTsM=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
Expand Up @@ -60,7 +60,7 @@ data "opentelekomcloud_rds_backup_v3" "backup" {
`, testAccRdsInstanceV3Basic(postfix))
}

func forceRdsBackup(t *testing.T, instanceID *string) string {
func forceRdsBackup(t *testing.T, instanceID *string) {
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.RdsV3Client(env.OS_REGION_NAME)
th.AssertNoErr(t, err)
Expand All @@ -81,7 +81,6 @@ func forceRdsBackup(t *testing.T, instanceID *string) string {
})
th.AssertNoErr(t, err)

var backupID string
err = golangsdk.WaitFor(600, func() (bool, error) {
pages, err := backups.List(client, backups.ListOpts{
InstanceID: *instanceID,
Expand All @@ -97,7 +96,6 @@ func forceRdsBackup(t *testing.T, instanceID *string) string {
return false, nil
}
backup := bList[0]
backupID = backup.ID
if backup.Status == backups.StatusCompleted {
return true, nil
}
Expand All @@ -107,6 +105,4 @@ func forceRdsBackup(t *testing.T, instanceID *string) string {
return false, nil
})
th.AssertNoErr(t, err)

return backupID
}
Expand Up @@ -6,12 +6,10 @@ import (
"regexp"
"testing"

"github.com/opentelekomcloud/gophertelekomcloud/openstack/rds/v3/instances"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/opentelekomcloud/gophertelekomcloud/openstack/rds/v3/instances"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/env"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
Expand Down Expand Up @@ -55,6 +53,41 @@ func TestAccRdsInstanceV3Basic(t *testing.T) {
})
}

func TestAccRdsInstanceV3RestoreBackup(t *testing.T) {
postfix := acctest.RandString(3)
var rdsInstance instances.RdsInstanceResponse
var restoredInstance instances.RdsInstanceResponse

restoredResourceName := "opentelekomcloud_rds_instance_v3.from_backup"

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckRdsInstanceV3Destroy,
Steps: []resource.TestStep{
{
Config: testAccRdsInstanceV3Basic(postfix),
Check: resource.ComposeTestCheckFunc(
testAccCheckRdsInstanceV3Exists(instanceV3ResourceName, &rdsInstance),
),
},
{
PreConfig: func() {
forceRdsBackup(t, &rdsInstance.Id)
},
Config: testAccRdsInstanceV3Restored(postfix),
Check: resource.ComposeTestCheckFunc(
testAccCheckRdsInstanceV3Exists(instanceV3ResourceName, &rdsInstance),
testAccCheckRdsInstanceV3Exists(restoredResourceName, &restoredInstance),
resource.TestCheckResourceAttr(restoredResourceName, "flavor", "rds.pg.c2.medium"),
resource.TestCheckResourceAttr(restoredResourceName, "volume.0.size", "40"),
resource.TestCheckResourceAttr(restoredResourceName, "tags.muh", "value-create"),
),
},
},
})
}

func TestAccRdsInstanceV3ElasticIP(t *testing.T) {
postfix := acctest.RandString(3)
var rdsInstance instances.RdsInstanceResponse
Expand Down Expand Up @@ -662,3 +695,75 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
}
`, common.DataSourceSecGroupDefault, common.DataSourceSubnet, postfix, env.OS_AVAILABILITY_ZONE)
}

func testAccRdsInstanceV3Restored(postfix string) string {
return fmt.Sprintf(`
%s
%s
resource "opentelekomcloud_rds_instance_v3" "instance" {
name = "tf_rds_instance_%s"
availability_zone = ["%s"]
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
port = "8635"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
volume {
type = "COMMON"
size = 40
}
flavor = "rds.pg.c2.medium"
backup_strategy {
start_time = "08:00-09:00"
keep_days = 1
}
tags = {
muh = "value-create"
kuh = "value-create"
}
}
data "opentelekomcloud_rds_backup_v3" "backup" {
instance_id = opentelekomcloud_rds_instance_v3.instance.id
type = "auto"
}
resource "opentelekomcloud_rds_instance_v3" "from_backup" {
name = "${opentelekomcloud_rds_instance_v3.instance.name}-restored"
availability_zone = opentelekomcloud_rds_instance_v3.instance.availability_zone
restore_point {
instance_id = opentelekomcloud_rds_instance_v3.instance.id
backup_id = data.opentelekomcloud_rds_backup_v3.backup.id
}
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
port = "8635"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
volume {
type = "COMMON"
size = 40
}
flavor = "rds.pg.c2.medium"
backup_strategy {
start_time = "08:00-09:00"
keep_days = 1
}
tags = {
muh = "value-create"
kuh = "value-create"
}
}
`, common.DataSourceSecGroupDefault, common.DataSourceSubnet, postfix, env.OS_AVAILABILITY_ZONE)
}
1 change: 1 addition & 0 deletions opentelekomcloud/services/rds/common.go
Expand Up @@ -2,4 +2,5 @@ package rds

const (
errCreateClient = "error creating RDSv3 client: %w"
keyClientV3 = "rdsv3-client"
)

0 comments on commit 3dfcb58

Please sign in to comment.