Skip to content

Commit

Permalink
feat(SDRS): Synchronized SDRS resources, unit test and document.
Browse files Browse the repository at this point in the history
  • Loading branch information
liwanting0517 committed Jan 16, 2024
1 parent 6fd010f commit 2174981
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/resources/sdrs_protectiongroup_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ resource "flexibleengine_sdrs_protectiongroup_v1" "group_1" {

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 parameter will create a new resource.

* `name` - (Required, String) The name of a protection group.

* `description` - (Optional, String, ForceNew) The description of a protection group. Changing this creates a new group.
Expand Down
3 changes: 3 additions & 0 deletions docs/resources/sdrs_replication_pair_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ resource "flexibleengine_sdrs_replication_pair_v1" "replication_1" {

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 parameter will create a new resource.

* `name` - (Required, String) The name of a replication pair. The name can contain a maximum of 64 bytes.
The value can contain only letters (a to z and A to Z), digits (0 to 9), decimal points (.),
underscores (_), and hyphens (-).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package acceptance

import (
"fmt"
"testing"

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

"github.com/chnsz/golangsdk/openstack/sdrs/v1/protectiongroups"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func getProtectionGroupResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) {
region := OS_REGION_NAME
client, err := cfg.SdrsV1Client(region)
if err != nil {
return nil, fmt.Errorf("error creating SDRS Client: %s", err)
}
return protectiongroups.Get(client, state.Primary.ID).Extract()
}

// Lack of testing for `enable`, will test it in resource replication pair
func TestAccProtectionGroup_basic(t *testing.T) {
var obj interface{}

name := acceptance.RandomAccResourceName()
rName := "flexibleengine_sdrs_protectiongroup_v1.test"

rc := acceptance.InitResourceCheck(
rName,
&obj,
getProtectionGroupResourceFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testProtectionGroup_basic(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "name", name),
resource.TestCheckResourceAttr(rName, "description", "test description"),
resource.TestCheckResourceAttrPair(rName, "source_availability_zone", "data.flexibleengine_availability_zones.test", "names.0"),
resource.TestCheckResourceAttrPair(rName, "target_availability_zone", "data.flexibleengine_availability_zones.test", "names.1"),
resource.TestCheckResourceAttrPair(rName, "domain_id", "data.flexibleengine_sdrs_domain_v1.test", "id"),
resource.TestCheckResourceAttrPair(rName, "source_vpc_id", "flexibleengine_vpc_v1.test", "id"),
),
},
{
Config: testProtectionGroup_basic_update(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "name", fmt.Sprintf("%s_update", name)),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testProtectionGroup_base(name string) string {
return fmt.Sprintf(`
%s
data "flexibleengine_sdrs_domain_v1" "test" {}
data "flexibleengine_availability_zones" "test" {}
`, testVpc(name))
}

func testProtectionGroup_basic(name string) string {
return fmt.Sprintf(`
%[1]s
resource "flexibleengine_sdrs_protectiongroup_v1" "test" {
name = "%[2]s"
source_availability_zone = data.flexibleengine_availability_zones.test.names[0]
target_availability_zone = data.flexibleengine_availability_zones.test.names[1]
domain_id = data.flexibleengine_sdrs_domain_v1.test.id
source_vpc_id = flexibleengine_vpc_v1.test.id
description = "test description"
}
`, testProtectionGroup_base(name), name)
}

func testProtectionGroup_basic_update(name string) string {
return fmt.Sprintf(`
%[1]s
resource "flexibleengine_sdrs_protectiongroup_v1" "test" {
name = "%[2]s_update"
source_availability_zone = data.flexibleengine_availability_zones.test.names[0]
target_availability_zone = data.flexibleengine_availability_zones.test.names[1]
domain_id = data.flexibleengine_sdrs_domain_v1.test.id
source_vpc_id = flexibleengine_vpc_v1.test.id
description = "test description"
}
`, testProtectionGroup_base(name), name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package acceptance

import (
"fmt"
"testing"

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

"github.com/chnsz/golangsdk/openstack/sdrs/v1/replications"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func getReplicationPairResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) {
region := OS_REGION_NAME
client, err := cfg.SdrsV1Client(region)
if err != nil {
return nil, fmt.Errorf("error creating SDRS Client: %s", err)
}
return replications.Get(client, state.Primary.ID).Extract()
}

func TestAccReplicationPair_basic(t *testing.T) {
var obj interface{}

name := acceptance.RandomAccResourceName()
rName := "flexibleengine_sdrs_replication_pair_v1.test"

rc := acceptance.InitResourceCheck(
rName,
&obj,
getReplicationPairResourceFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testReplicationPair_basic(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "name", name),
resource.TestCheckResourceAttr(rName, "description", "test description"),
resource.TestCheckResourceAttr(rName, "delete_target_volume", "true"),
resource.TestCheckResourceAttrPair(rName, "group_id", "flexibleengine_sdrs_protectiongroup_v1.test", "id"),
resource.TestCheckResourceAttrPair(rName, "volume_id", "flexibleengine_blockstorage_volume_v2.test", "id"),
resource.TestCheckResourceAttrSet(rName, "replication_model"),
resource.TestCheckResourceAttrSet(rName, "status"),
resource.TestCheckResourceAttrSet(rName, "target_volume_id"),
),
},
{
Config: testReplicationPair_basic_update(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "name", fmt.Sprintf("%s_update", name)),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"delete_target_volume",
},
},
},
})
}

func testReplicationPair_base(name string) string {
return fmt.Sprintf(`
%[1]s
data "flexibleengine_availability_zones" "test" {}
data "flexibleengine_sdrs_domain_v1" "test" {}
resource "flexibleengine_sdrs_protectiongroup_v1" "test" {
name = "%[2]s"
source_availability_zone = data.flexibleengine_availability_zones.test.names[0]
target_availability_zone = data.flexibleengine_availability_zones.test.names[1]
domain_id = data.flexibleengine_sdrs_domain_v1.test.id
source_vpc_id = flexibleengine_vpc_v1.test.id
description = "test description"
}
resource "flexibleengine_blockstorage_volume_v2" "test" {
name = "%[2]s"
description = "test volume for sdrs replication pair"
availability_zone = data.flexibleengine_availability_zones.test.names[0]
volume_type = "SSD"
size = 100
}
`, testVpc(name), name)
}

func testReplicationPair_basic(name string) string {
return fmt.Sprintf(`
%[1]s
resource "flexibleengine_sdrs_replication_pair_v1" "test" {
name = "%[2]s"
group_id = flexibleengine_sdrs_protectiongroup_v1.test.id
volume_id = flexibleengine_blockstorage_volume_v2.test.id
description = "test description"
delete_target_volume = true
}
`, testReplicationPair_base(name), name)
}

func testReplicationPair_basic_update(name string) string {
return fmt.Sprintf(`
%[1]s
resource "flexibleengine_sdrs_replication_pair_v1" "test" {
name = "%[2]s_update"
group_id = flexibleengine_sdrs_protectiongroup_v1.test.id
volume_id = flexibleengine_blockstorage_volume_v2.test.id
description = "test description"
delete_target_volume = true
}
`, testReplicationPair_base(name), name)
}
6 changes: 4 additions & 2 deletions flexibleengine/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/nat"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/obs"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/rds"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/sdrs"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/sfs"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/smn"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/sms"
Expand Down Expand Up @@ -528,9 +529,7 @@ func Provider() *schema.Provider {
"flexibleengine_dds_instance_v3": resourceDdsInstanceV3(),

"flexibleengine_sdrs_drill_v1": resourceSdrsDrillV1(),
"flexibleengine_sdrs_protectiongroup_v1": resourceSdrsProtectiongroupV1(),
"flexibleengine_sdrs_protectedinstance_v1": resourceSdrsProtectedInstanceV1(),
"flexibleengine_sdrs_replication_pair_v1": resourceSdrsReplicationPairV1(),
"flexibleengine_sdrs_replication_attach_v1": resourceSdrsReplicationAttachV1(),

"flexibleengine_vpcep_approval": resourceVPCEndpointApproval(),
Expand Down Expand Up @@ -649,6 +648,9 @@ func Provider() *schema.Provider {
"flexibleengine_rds_database_privilege": rds.ResourceMysqlDatabasePrivilege(),
"flexibleengine_rds_sql_audit": rds.ResourceSQLAudit(),

"flexibleengine_sdrs_protectiongroup_v1": sdrs.ResourceProtectionGroup(),
"flexibleengine_sdrs_replication_pair_v1": sdrs.ResourceReplicationPair(),

"flexibleengine_sms_server_template": sms.ResourceServerTemplate(),
"flexibleengine_sms_task": sms.ResourceMigrateTask(),
"flexibleengine_swr_organization": swr.ResourceSWROrganization(),
Expand Down

0 comments on commit 2174981

Please sign in to comment.