-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dws): add new resource to copy automated snapshot (#5596)
* chore(dws/snapshot): extract query and delete methods as common method * feat(dws): add new resource to copy automated snapshot
- Loading branch information
1 parent
7f672a0
commit 6811712
Showing
7 changed files
with
378 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
subcategory: "GaussDB(DWS)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_dws_snapshot_copy" | ||
description: |- | ||
Use this resource to copy an automated snapshot to a manual snapshot within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_dws_snapshot_copy | ||
|
||
Use this resource to copy an automated snapshot to a manual snapshot within HuaweiCloud. | ||
|
||
-> 1. An automated snapshot can only correspond to one `huaweicloud_dws_snapshot_copy` resource. | ||
<br>2. Deleting this resource will delete the corresponding copied snapshot. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "automated_snapshot_id" {} | ||
variable "snapshot_name" {} | ||
variable "description" {} | ||
resource "huaweicloud_dws_snapshot_copy" "test" { | ||
snapshot_id = var.automated_snapshot_id | ||
name = var.snapshot_name | ||
description = var.description | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource. | ||
If omitted, the provider-level region will be used. | ||
Changing this creates a new resource. | ||
|
||
* `snapshot_id` - (Required, String, ForceNew) Specifies the ID of the automated snapshot to be copied. | ||
Changing this creates a new resource. | ||
|
||
* `name` - (Required, String, ForceNew) Specifies the name of the copy snapshot. | ||
Changing this creates a new resource. | ||
The valid length is limited from `4` to `64`, only English letters, digits, underscores (_) and hyphens (-) are allowed, | ||
and must start with a letter. | ||
The snapshot name must be unique. | ||
|
||
* `description` - (Optional, String, ForceNew) Specifies the description of the copy snapshot. | ||
Changing this creates a new resource. | ||
The maximum length is limited to `256` characters, and special characters (`!<>'=&"`) are not allowed. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The resource ID, also the ID of the copied snapshot. | ||
|
||
## Import | ||
|
||
The resource can be imported using the related `snapshot_id` and `id`, separated by a slash, e.g. | ||
|
||
```bash | ||
$ terraform import huaweicloud_dws_snapshot_copy.test <snapshot_id>/<id> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
huaweicloud/services/acceptance/dws/resource_huaweicloud_dws_snapshot_copy_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package dws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/dws" | ||
) | ||
|
||
func getCopiedSnapshotFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) { | ||
region := acceptance.HW_REGION_NAME | ||
client, err := cfg.NewServiceClient("dws", region) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating DWS client: %s", err) | ||
} | ||
|
||
return dws.GetSnapshotById(client, state.Primary.ID) | ||
} | ||
|
||
func TestAccSnapshotCopy_basic(t *testing.T) { | ||
var ( | ||
obj interface{} | ||
rName = "huaweicloud_dws_snapshot_copy.test" | ||
name = acceptance.RandomAccResourceNameWithDash() | ||
|
||
rc = acceptance.InitResourceCheck( | ||
rName, | ||
&obj, | ||
getCopiedSnapshotFunc, | ||
) | ||
) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
acceptance.TestAccPreCheckDwsAutomatedSnapshot(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
CheckDestroy: rc.CheckResourceDestroy(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testSnapshotCopy_basic_step1(name), | ||
Check: resource.ComposeTestCheckFunc( | ||
rc.CheckResourceExists(), | ||
resource.TestCheckResourceAttr(rName, "name", name), | ||
resource.TestCheckResourceAttr(rName, "snapshot_id", acceptance.HW_DWS_AUTOMATED_SNAPSHOT_ID), | ||
resource.TestCheckResourceAttr(rName, "description", "Copying a snapshot by terraform script"), | ||
), | ||
}, | ||
{ | ||
ResourceName: rName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateIdFunc: testAccSnapshotCopyImportStateFunc(rName), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccSnapshotCopyImportStateFunc(rName string) resource.ImportStateIdFunc { | ||
return func(s *terraform.State) (string, error) { | ||
rs, ok := s.RootModule().Resources[rName] | ||
if !ok { | ||
return "", fmt.Errorf("resource (%s) not found: %s", rName, rs) | ||
} | ||
|
||
if rs.Primary.Attributes["snapshot_id"] == "" { | ||
return "", fmt.Errorf("invalid format specified for import ID, want '<snapshot_id>/<id>', but got '%s/%s'", | ||
rs.Primary.Attributes["snapshot_id"], rs.Primary.ID) | ||
} | ||
return fmt.Sprintf("%s/%s", rs.Primary.Attributes["snapshot_id"], rs.Primary.ID), nil | ||
} | ||
} | ||
|
||
func testSnapshotCopy_basic_step1(name string) string { | ||
return fmt.Sprintf(` | ||
resource "huaweicloud_dws_snapshot_copy" "test" { | ||
snapshot_id = "%[1]s" | ||
name = "%[2]s" | ||
description = "Copying a snapshot by terraform script" | ||
} | ||
`, acceptance.HW_DWS_AUTOMATED_SNAPSHOT_ID, name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.