Skip to content

Commit 07b5999

Browse files
authored
feat(RDS): add rds restore resource (#4870)
1 parent 3ab5a29 commit 07b5999

File tree

4 files changed

+417
-0
lines changed

4 files changed

+417
-0
lines changed

docs/resources/rds_restore.md

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
subcategory: "Relational Database Service (RDS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_rds_restore"
5+
description: |-
6+
Manages an RDS instance restore resource within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_rds_restore
10+
11+
Manages an RDS instance restore resource within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
### restore by backup_id
16+
17+
```hcl
18+
variable "target_instance_id" {}
19+
variable "source_instance_id" {}
20+
variable "backup_id" {}
21+
22+
resource "huaweicloud_rds_restore" "test" {
23+
target_instance_id = var.target_instance_id
24+
source_instance_id = var.source_instance_id
25+
type = "backup"
26+
backup_id = var.backup_id
27+
}
28+
```
29+
30+
### restore by timestamp
31+
32+
```hcl
33+
variable "target_instance_id" {}
34+
variable "source_instance_id" {}
35+
36+
resource "huaweicloud_rds_restore" "test" {
37+
target_instance_id = var.target_instance_id
38+
source_instance_id = var.source_instance_id
39+
type = "timestamp"
40+
restore_time = 1673852043000
41+
}
42+
```
43+
44+
## Argument Reference
45+
46+
The following arguments are supported:
47+
48+
* `region` - (Optional, String, ForceNew) The region in which to create the rds instance resource. If omitted, the
49+
provider-level region will be used. Changing this creates a new resource.
50+
51+
* `target_instance_id` - (Required, String, ForceNew) Specifies the target instance ID.
52+
53+
Changing this creates a new resource.
54+
55+
* `source_instance_id` - (Required, String, ForceNew) Specifies the source instance ID.
56+
57+
Changing this creates a new resource.
58+
59+
* `type` - (Optional, String, ForceNew) Specifies the restoration type. Value options:
60+
+ **backup**: indicates using backup files for restoration.
61+
+ **timestamp**: indicates the point-in-time restoration mode.
62+
63+
Changing this creates a new resource.
64+
65+
* `backup_id` - (Optional, String, ForceNew) Specifies the ID of the backup to be restored. This parameter must be
66+
specified when `type` is set to **backup** or left empty.
67+
68+
Changing this creates a new resource.
69+
70+
* `restore_time` - (Optional, Int, ForceNew) Specifies the time point of data restoration in the UNIX timestamp format.
71+
The unit is millisecond and the time zone is UTC. This parameter must be specified when `type` is set to **timestamp**.
72+
73+
Changing this creates a new resource.
74+
75+
* `database_name` - (Optional, Map, ForceNew) Specifies the databases that will be restored. This parameter applies only
76+
to the SQL Server DB engine. The key is the old database name, the value is the new database name. If this parameter is
77+
specified, you can restore all or specific databases and rename new databases. If this parameter is not specified, all
78+
databases are restored by default. You can enter multiple new database names and separate them with commas (,). The new
79+
database names can contain but cannot be the same as the original database names. Note the following when you are
80+
specifying new database names:
81+
+ New database names must be different from the original database names. If they are left blank, the original database
82+
names will be used for restoration by default.
83+
+ The case-sensitivity settings of the new databases are the same as those of the original databases. Make sure the new
84+
database names are unique.
85+
+ The total number of new and existing databases on the existing or original DB instances where data is restored cannot
86+
exceed the database quota specified by **rds_databases_quota**.
87+
+ New database names cannot contain the following fields (case-insensitive): **rdsadmin**, **master**, **msdb**,
88+
**tempdb**, **model** and **resource**.
89+
+ New database names must consist of **1** to **64** characters, including only letters, digits, underscores (_), and
90+
hyphens (-). If you want to restore data to multiple new databases, separate them with commas (,).
91+
+ New database names must be different from any database names on the original DB instance.
92+
+ New database names must be different from any database names on the existing or original DB instances where data is
93+
restored.
94+
95+
Changing this creates a new resource.
96+
97+
## Attribute Reference
98+
99+
In addition to all arguments above, the following attribute is exported:
100+
101+
* `id` - The resource ID. The value is the restore job ID.
102+
103+
## Timeouts
104+
105+
This resource provides the following timeouts configuration options:
106+
107+
* `create` - Default is 60 minutes.

huaweicloud/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,7 @@ func Provider() *schema.Provider {
14881488
"huaweicloud_rds_parametergroup": rds.ResourceRdsConfiguration(),
14891489
"huaweicloud_rds_read_replica_instance": rds.ResourceRdsReadReplicaInstance(),
14901490
"huaweicloud_rds_backup": rds.ResourceBackup(),
1491+
"huaweicloud_rds_restore": rds.ResourceRdsRestore(),
14911492
"huaweicloud_rds_cross_region_backup_strategy": rds.ResourceBackupStrategy(),
14921493
"huaweicloud_rds_sql_audit": rds.ResourceSQLAudit(),
14931494
"huaweicloud_rds_pg_plugin": rds.ResourceRdsPgPlugin(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package rds
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance/common"
11+
)
12+
13+
func TestAccRdsInstanceRestore_basic(t *testing.T) {
14+
name := acceptance.RandomAccResourceName()
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { acceptance.TestAccPreCheck(t) },
18+
ProviderFactories: acceptance.TestAccProviderFactories,
19+
CheckDestroy: nil,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccRdsInstanceRestoreConfig_basic(name),
23+
},
24+
},
25+
})
26+
}
27+
28+
func TestAccRdsInstanceRestore_withTimestamp(t *testing.T) {
29+
name := acceptance.RandomAccResourceName()
30+
31+
resource.ParallelTest(t, resource.TestCase{
32+
PreCheck: func() {
33+
acceptance.TestAccPreCheck(t)
34+
acceptance.TestAccPreCheckRdsInstanceId(t)
35+
},
36+
ProviderFactories: acceptance.TestAccProviderFactories,
37+
CheckDestroy: nil,
38+
Steps: []resource.TestStep{
39+
{
40+
Config: testAccRdsInstanceRestoreConfig_withTimestamp(name),
41+
},
42+
},
43+
})
44+
}
45+
46+
func testAccRdsInstanceRestoreConfig_basic(name string) string {
47+
return fmt.Sprintf(`
48+
%[1]s
49+
50+
data "huaweicloud_availability_zones" "test" {}
51+
52+
data "huaweicloud_rds_flavors" "test" {
53+
db_type = "MySQL"
54+
db_version = "8.0"
55+
instance_mode = "single"
56+
group_type = "dedicated"
57+
vcpus = 4
58+
}
59+
60+
resource "huaweicloud_rds_instance" "instance" {
61+
count = 2
62+
name = "%[2]s__${count.index}"
63+
flavor = data.huaweicloud_rds_flavors.test.flavors[0].name
64+
security_group_id = huaweicloud_networking_secgroup.test.id
65+
subnet_id = huaweicloud_vpc_subnet.test.id
66+
vpc_id = huaweicloud_vpc.test.id
67+
availability_zone = [data.huaweicloud_availability_zones.test.names[0]]
68+
69+
db {
70+
type = "MySQL"
71+
version = "8.0"
72+
}
73+
74+
volume {
75+
type = "CLOUDSSD"
76+
size = 40
77+
}
78+
}
79+
80+
resource "huaweicloud_rds_backup" "backup" {
81+
instance_id = huaweicloud_rds_instance.instance[0].id
82+
name = "%[2]s_backup"
83+
}
84+
85+
resource "huaweicloud_rds_restore" "test" {
86+
target_instance_id = huaweicloud_rds_instance.instance[1].id
87+
source_instance_id = huaweicloud_rds_instance.instance[0].id
88+
type = "backup"
89+
backup_id = huaweicloud_rds_backup.backup.id
90+
}
91+
`, common.TestBaseNetwork(name), name)
92+
}
93+
94+
func testAccRdsInstanceRestoreConfig_withTimestamp(name string) string {
95+
return fmt.Sprintf(`
96+
%[1]s
97+
98+
data "huaweicloud_availability_zones" "test" {}
99+
100+
data "huaweicloud_rds_flavors" "test" {
101+
db_type = "MySQL"
102+
db_version = "8.0"
103+
instance_mode = "single"
104+
group_type = "dedicated"
105+
vcpus = 4
106+
}
107+
108+
resource "huaweicloud_rds_instance" "instance" {
109+
name = "%[2]s"
110+
flavor = data.huaweicloud_rds_flavors.test.flavors[0].name
111+
security_group_id = huaweicloud_networking_secgroup.test.id
112+
subnet_id = huaweicloud_vpc_subnet.test.id
113+
vpc_id = huaweicloud_vpc.test.id
114+
availability_zone = [data.huaweicloud_availability_zones.test.names[0]]
115+
116+
db {
117+
type = "MySQL"
118+
version = "8.0"
119+
}
120+
121+
volume {
122+
type = "CLOUDSSD"
123+
size = 40
124+
}
125+
}
126+
127+
data "huaweicloud_rds_restore_time_ranges" "test" {
128+
instance_id = "%[3]s"
129+
}
130+
131+
resource "huaweicloud_rds_restore" "test" {
132+
target_instance_id = huaweicloud_rds_instance.instance.id
133+
source_instance_id = "%[3]s"
134+
type = "timestamp"
135+
restore_time = data.huaweicloud_rds_restore_time_ranges.test.restore_time[0].start_time
136+
}
137+
`, common.TestBaseNetwork(name), name, acceptance.HW_RDS_INSTANCE_ID)
138+
}

0 commit comments

Comments
 (0)