Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-lifshits committed Jan 9, 2025
1 parent fc622cf commit 2dc85c8
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 114 deletions.
50 changes: 31 additions & 19 deletions docs/resources/rds_backup_v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,61 @@ Manages a manual RDS backup.

## Example Usage

### Create a basic RDS backup

```hcl
resource "opentelekomcloud_rds_instance_v3" "instance" {
name = "test-instance"
engine = "mysql"
datastore = "percona"
flavor_ref = "rds.mysql.s1.large"
resource "opentelekomcloud_rds_backup_v3" "test" {
instance_id = opentelekomcloud_rds_instance_v3.instance.id
name = "rds-backup-test-01"
}
```

### Create a specific RDS databases backup for Microsoft SQL Server

```hcl
resource "opentelekomcloud_rds_backup_v3" "test" {
instance_id = opentelekomcloud_rds_instance_v3.instance.id
name = "rds-backup-test-01"
description = "manual"
databases = ["test", "test2"]
}
```

## Argument Reference

The following arguments are supported:

* `instance_id` - (Required) The ID of the RDS instance to which the backup belongs.
* `name` - (Required) The name of the backup.
* `description` - (Optional) Specifies the backup description.
It contains a maximum of 256 characters and cannot contain the following special characters: >!<"&'=
* `databases` - (Optional) Specifies a list of self-built Microsoft SQL Server databases that are partially backed up.
* `instance_id` - (Required, String, ForceNew) The ID of the RDS instance to which the backup belongs.

* `name` - (Required, String, ForceNew) The name of the backup.

* `databases` - (Optional, List, ForceNew) Specifies a list of self-built Microsoft SQL Server databases that are partially backed up.
(Only Microsoft SQL Server support partial backups.)

## Attributes Reference
The following attributes are exported:

In addition to the arguments listed above, the following computed attributes are exported:

* `id` - The ID of the backup.
* `instance_id` - The ID of the RDS instance to which the backup belongs.
* `name` - The name of the backup.
* `description` - The description of the backup.
* `databases` - The list of self-built Microsoft SQL Server databases that are partially backed up.
(Only Microsoft SQL Server support partial backups.)

* `begin_time` - Indicates the backup start time in the "yyyy-mm-ddThh:mm:ssZ" format,
where "T" indicates the start time of the time field, and "Z" indicates the time zone offset.
* `status` - Indicates the backup status. Value:

* `status` - Indicates the backup status. Values:
- BUILDING: Backup in progress
- COMPLETED: Backup completed
- FAILED: Backup failed
- DELETING: Backup being deleted
* `type` - Indicates the backup type. Value:

* `type` - Indicates the backup type. Values:
- auto: automated full backup
- manual: manual full backup
- fragment: differential full backup
- incremental: automated incremental backup

## Import

RDS backup can be imported using related RDS `instance_id` and their `backup_id`, separated by the slashes, e.g.

```bash
$ terraform import opentelekomcloud_rds_backup_v3.backup <instance_id>/<backup_id>
```
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,38 @@ import (
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
)

func getBackupResourceFunc(conf *cfg.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := conf.RdsV3Client(env.OS_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating RDS v3 client: %s", err)
}

opts := backups.ListOpts{
InstanceID: state.Primary.Attributes["instance_id"],
BackupID: state.Primary.ID,
}

backupList, err := backups.List(client, opts)

if err != nil {
return nil, fmt.Errorf("error listing backups: %s", err)
}

return backupList[0], nil
}

func TestAccResourceRDSV3Backup_basic(t *testing.T) {
resourceName := "opentelekomcloud_rds_backup_v3.test"
postfix := acctest.RandString(3)
var (
obj backups.Backup
rName = "opentelekomcloud_rds_backup_v3.test"
postfix = acctest.RandString(3)
)

rc := common.InitResourceCheck(
rName,
&obj,
getBackupResourceFunc,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
Expand All @@ -28,17 +57,42 @@ func TestAccResourceRDSV3Backup_basic(t *testing.T) {
{
Config: testAccResourceRDSV3BackupBasic(postfix),
Check: resource.ComposeTestCheckFunc(
testAccCheckRdsBackupV3Exists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", "tf_rds_backup_"+postfix),
resource.TestCheckResourceAttr(resourceName, "type", "manual"),
resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttrSet(resourceName, "id"),
rc.CheckResourceExists(),
testAccCheckRdsBackupV3Exists(rName),
resource.TestCheckResourceAttr(rName, "name", "tf_rds_backup_"+postfix),
resource.TestCheckResourceAttr(rName, "type", "manual"),
resource.TestCheckResourceAttr(rName, "status", "COMPLETED"),
resource.TestCheckResourceAttrSet(rName, "id"),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccBackupImportStateFunc(rName),
},
},
})
}

func testAccBackupImportStateFunc(rsName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
var instanceId, backupId string
rs, ok := s.RootModule().Resources[rsName]
if !ok {
return "", fmt.Errorf("the resource (%s) of RDF backup is not found in the tfstate", rsName)
}
instanceId = rs.Primary.Attributes["instance_id"]
backupId = rs.Primary.ID
if instanceId == "" || backupId == "" {
return "", fmt.Errorf("some import IDs are missing: "+
"'<instance_id>/<id>', but got '%s/%s'",
instanceId, backupId)
}
return fmt.Sprintf("%s/%s", instanceId, backupId), nil
}
}

func testAccCheckRdsBackupV3Destroy(s *terraform.State) error {
const backupDeleteRetryTimeout = 5 * time.Minute

Expand Down Expand Up @@ -123,7 +177,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "16"
port = "8635"
}
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
Expand All @@ -134,10 +188,6 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
size = 40
}
flavor = "rds.pg.c2.large"
backup_strategy {
start_time = "08:00-09:00"
keep_days = 1
}
}
resource "opentelekomcloud_rds_backup_v3" "test" {
Expand Down
Loading

0 comments on commit 2dc85c8

Please sign in to comment.