Skip to content

Commit

Permalink
refactor(fgs/async): using auto-gen style to replace old coding (#6328)
Browse files Browse the repository at this point in the history
* test(fgs/async): adjust the basic test

* test(fgs/anysc): new test to coverage dest configuration

* refactor(fgs/async): using auto-gen style to replace old coding
  • Loading branch information
Lance52259 authored Feb 14, 2025
1 parent 01e62b5 commit 706234c
Show file tree
Hide file tree
Showing 2 changed files with 272 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,27 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/chnsz/golangsdk/openstack/fgs/v2/function"

"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/acceptance/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/fgs"
)

func getAsyncInvokeConfigFunc(conf *config.Config, state *terraform.ResourceState) (interface{}, error) {
c, err := conf.FgsV2Client(acceptance.HW_REGION_NAME)
func getAsyncInvokeConfigFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := cfg.NewServiceClient("fgs", acceptance.HW_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating FunctionGraph v2 client: %s", err)
return nil, fmt.Errorf("error creating FunctionGraph client: %s", err)
}
return function.GetAsyncInvokeConfig(c, state.Primary.ID)
return fgs.GetAsyncIncokeConfigurations(client, state.Primary.ID)
}

func TestAccAsyncInvokeConfig_basic(t *testing.T) {
var cfg function.AsyncInvokeConfig
name := acceptance.RandomAccResourceNameWithDash()
rName := "huaweicloud_fgs_async_invoke_configuration.test"

rc := acceptance.InitResourceCheck(
rName,
&cfg,
getAsyncInvokeConfigFunc,
var (
obj interface{}

name = acceptance.RandomAccResourceNameWithDash()

rName = "huaweicloud_fgs_async_invoke_configuration.test"
rc = acceptance.InitResourceCheck(rName, &obj, getAsyncInvokeConfigFunc)
)

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -44,7 +41,7 @@ func TestAccAsyncInvokeConfig_basic(t *testing.T) {
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccAsyncInvokeConfig_basic_step1(name, acceptance.HW_FGS_AGENCY_NAME),
Config: testAccAsyncInvokeConfig_basic_step1(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttrPair(rName, "function_urn",
Expand All @@ -61,7 +58,7 @@ func TestAccAsyncInvokeConfig_basic(t *testing.T) {
),
},
{
Config: testAccAsyncInvokeConfig_basic_step2(name, acceptance.HW_FGS_AGENCY_NAME),
Config: testAccAsyncInvokeConfig_basic_step2(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttrPair(rName, "function_urn",
Expand All @@ -86,28 +83,46 @@ func TestAccAsyncInvokeConfig_basic(t *testing.T) {
})
}

func testAccAsyncInvokeConfig_basic_step1(name, agency string) string {
func testAccAsyncInvokeConfig_base(name string) string {
return fmt.Sprintf(`
resource "huaweicloud_obs_bucket" "test" {
bucket = "%[1]s"
acl = "private"
force_destroy = true
}
variable "function_code_content" {
type = string
default = <<EOT
def main():
print("Hello, World!")
resource "huaweicloud_smn_topic" "test" {
name = "%[1]s"
if __name__ == "__main__":
main()
EOT
}
resource "huaweicloud_fgs_function" "test" {
name = "%[1]s"
app = "default"
agency = "%[2]s"
description = "fuction test"
handler = "index.handler"
memory_size = 128
timeout = 3
runtime = "Python2.7"
code_type = "inline"
func_code = "e42a37a22f4988ba7a681e3042e5c7d13c04e6c1"
agency = "%[2]s"
func_code = base64encode(var.function_code_content)
}
`, name, acceptance.HW_FGS_AGENCY_NAME)
}

func testAccAsyncInvokeConfig_basic_step1(name string) string {
return fmt.Sprintf(`
%[1]s
resource "huaweicloud_obs_bucket" "test" {
bucket = "%[2]s"
acl = "private"
force_destroy = true
}
resource "huaweicloud_smn_topic" "test" {
name = "%[2]s"
}
resource "huaweicloud_fgs_async_invoke_configuration" "test" {
Expand All @@ -132,10 +147,10 @@ resource "huaweicloud_fgs_async_invoke_configuration" "test" {
})
}
}
`, name, agency)
`, testAccAsyncInvokeConfig_base(name), name)
}

func testAccAsyncInvokeConfig_basic_step2(name, agency string) string {
func testAccAsyncInvokeConfig_basic_step2(name string) string {
return fmt.Sprintf(`
%[1]s
Expand All @@ -147,24 +162,13 @@ resource "huaweicloud_dis_stream" "test" {
resource "huaweicloud_fgs_function" "failure_transport" {
name = "%[2]s-failure-transport"
app = "default"
description = "fuction test"
handler = "index.handler"
memory_size = 128
timeout = 3
runtime = "Python2.7"
code_type = "inline"
func_code = "e42a37a22f4988ba7a681e3042e5c7d13c04e6c1"
}
resource "huaweicloud_fgs_function" "test" {
name = "%[2]s"
app = "default"
handler = "index.handler"
memory_size = 128
timeout = 3
runtime = "Python2.7"
code_type = "inline"
func_code = "e42a37a22f4988ba7a681e3042e5c7d13c04e6c1"
agency = "%[3]s"
func_code = base64encode(var.function_code_content)
}
resource "huaweicloud_fgs_async_invoke_configuration" "test" {
Expand All @@ -186,5 +190,84 @@ resource "huaweicloud_fgs_async_invoke_configuration" "test" {
})
}
}
`, common.TestBaseNetwork(name), name, agency)
`, testAccAsyncInvokeConfig_base(name), name)
}

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

name = acceptance.RandomAccResourceNameWithDash()

rName = "huaweicloud_fgs_async_invoke_configuration.test"
rc = acceptance.InitResourceCheck(rName, &obj, getAsyncInvokeConfigFunc)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
// Please read the instructions carefully before use to ensure sufficient permissions.
acceptance.TestAccPreCheckFgsAgency(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccAsyncInvokeConfig_withoutDestConfigs_step1(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttrPair(rName, "function_urn",
"huaweicloud_fgs_function.test", "urn"),
resource.TestCheckResourceAttr(rName, "max_async_event_age_in_seconds", "3500"),
resource.TestCheckResourceAttr(rName, "max_async_retry_attempts", "2"),
resource.TestCheckResourceAttr(rName, "enable_async_status_log", "true"),
resource.TestMatchResourceAttr(rName, "created_at",
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
),
},
{
Config: testAccAsyncInvokeConfig_withoutDestConfigs_step2(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttrPair(rName, "function_urn",
"huaweicloud_fgs_function.test", "urn"),
resource.TestCheckResourceAttr(rName, "max_async_event_age_in_seconds", "4000"),
resource.TestCheckResourceAttr(rName, "max_async_retry_attempts", "0"),
resource.TestCheckResourceAttr(rName, "enable_async_status_log", "false"),
resource.TestMatchResourceAttr(rName, "updated_at",
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAsyncInvokeConfig_withoutDestConfigs_step1(name string) string {
return fmt.Sprintf(`
%[1]s
resource "huaweicloud_fgs_async_invoke_configuration" "test" {
function_urn = huaweicloud_fgs_function.test.urn
max_async_event_age_in_seconds = 3500
max_async_retry_attempts = 2
enable_async_status_log = true
}
`, testAccAsyncInvokeConfig_base(name))
}

func testAccAsyncInvokeConfig_withoutDestConfigs_step2(name string) string {
return fmt.Sprintf(`
%[1]s
resource "huaweicloud_fgs_async_invoke_configuration" "test" {
function_urn = huaweicloud_fgs_function.test.urn
max_async_event_age_in_seconds = 4000
max_async_retry_attempts = 0
}
`, testAccAsyncInvokeConfig_base(name))
}
Loading

0 comments on commit 706234c

Please sign in to comment.