From f609b3e61c2c611615e3df1c4b9500bc24cde729 Mon Sep 17 00:00:00 2001 From: liwanting0517 Date: Mon, 15 Jan 2024 17:24:43 +0800 Subject: [PATCH] feat(SFS): Synchronized SFS resource, unit test and document. --- docs/resources/sfs_access_rule_v2.md | 3 + ...rce_flexibleengine_sfs_access_rule_test.go | 111 ++++++++++++++++++ flexibleengine/provider.go | 5 +- 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 flexibleengine/acceptance/resource_flexibleengine_sfs_access_rule_test.go diff --git a/docs/resources/sfs_access_rule_v2.md b/docs/resources/sfs_access_rule_v2.md index 962490e2f..ad9605b6e 100644 --- a/docs/resources/sfs_access_rule_v2.md +++ b/docs/resources/sfs_access_rule_v2.md @@ -58,6 +58,9 @@ resource "flexibleengine_sfs_access_rule_v2" "rule_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. + * `sfs_id` - (Required, String, ForceNew) Specifies the UUID of the shared file system. Changing this will create a new access rule. diff --git a/flexibleengine/acceptance/resource_flexibleengine_sfs_access_rule_test.go b/flexibleengine/acceptance/resource_flexibleengine_sfs_access_rule_test.go new file mode 100644 index 000000000..6c4ff5729 --- /dev/null +++ b/flexibleengine/acceptance/resource_flexibleengine_sfs_access_rule_test.go @@ -0,0 +1,111 @@ +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/sfs/v2/shares" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func getSfsAccessRuleResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) { + client, err := cfg.SfsV2Client(OS_REGION_NAME) + if err != nil { + return nil, fmt.Errorf("error creating SFS client: %s", err) + } + + resourceID := state.Primary.ID + sfsID := state.Primary.Attributes["sfs_id"] + rules, err := shares.ListAccessRights(client, sfsID).ExtractAccessRights() + if err != nil { + return nil, err + } + + for _, item := range rules { + if item.ID == resourceID { + return &item, nil + } + } + + return nil, fmt.Errorf("the sfs access rule %s does not exist", resourceID) +} + +func TestAccSFSAccessRuleV2_basic(t *testing.T) { + var rule shares.AccessRight + rName := acceptance.RandomAccResourceName() + resourceName := "flexibleengine_sfs_access_rule_v2.test" + + rc := acceptance.InitResourceCheck( + resourceName, + &rule, + getSfsAccessRuleResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: configAccSFSAccessRuleV2_basic(rName), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(resourceName, "access_level", "rw"), + resource.TestCheckResourceAttr(resourceName, "status", "active"), + ), + }, + { + Config: configAccSFSAccessRuleV2_ipAuth(rName), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(resourceName, "status", "active"), + ), + }, + }, + }) +} + +func configAccSFSAccessRuleV2_basic(rName string) string { + return fmt.Sprintf(` +data "flexibleengine_vpc_v1" "test" { + name = "tf-xxx" +} + +resource "flexibleengine_sfs_file_system_v2" "test" { + share_proto = "NFS" + size = 10 + name = "%s" + description = "sfs file system created by terraform testacc" +} + +resource "flexibleengine_sfs_access_rule_v2" "test" { + sfs_id = flexibleengine_sfs_file_system_v2.test.id + access_to = data.flexibleengine_vpc_v1.test.id +}`, rName) +} + +func configAccSFSAccessRuleV2_ipAuth(rName string) string { + return fmt.Sprintf(` +data "flexibleengine_vpc_v1" "test" { + name = "tf-xxx" +} + +resource "flexibleengine_sfs_file_system_v2" "test" { + share_proto = "NFS" + size = 10 + name = "%s" + description = "sfs file system created by terraform testacc" +} + +resource "flexibleengine_sfs_access_rule_v2" "test" { + sfs_id = flexibleengine_sfs_file_system_v2.test.id + access_to = join("#", [data.flexibleengine_vpc_v1.test.id, "192.168.10.0/24", "0", "no_all_squash,no_root_squash"]) +}`, rName) +} diff --git a/flexibleengine/provider.go b/flexibleengine/provider.go index 5a861c878..e416c2308 100644 --- a/flexibleengine/provider.go +++ b/flexibleengine/provider.go @@ -496,7 +496,6 @@ func Provider() *schema.Provider { "flexibleengine_vpc_peering_connection_accepter_v2": resourceVpcPeeringConnectionAccepterV2(), "flexibleengine_sfs_file_system_v2": resourceSFSFileSystemV2(), - "flexibleengine_sfs_access_rule_v2": resourceSFSAccessRuleV2(), "flexibleengine_rts_software_config_v1": resourceSoftwareConfigV1(), "flexibleengine_rts_stack_v1": resourceRTSStackV1(), @@ -688,7 +687,9 @@ func Provider() *schema.Provider { "flexibleengine_lb_member_v3": elb.ResourceMemberV3(), // v1.35.0 "flexibleengine_smn_topic_v2": smn.ResourceTopic(), // v1.39.0 "flexibleengine_smn_subscription_v2": smn.ResourceSubscription(), // v1.39.0 - "flexibleengine_sfs_turbo": sfs.ResourceSFSTurbo(), + + "flexibleengine_sfs_access_rule_v2": sfs.ResourceSFSAccessRuleV2(), + "flexibleengine_sfs_turbo": sfs.ResourceSFSTurbo(), // Deprecated resource "flexibleengine_as_group_v1": resourceASGroup(),