diff --git a/docs/resources/fgs_function_topping.md b/docs/resources/fgs_function_topping.md new file mode 100644 index 0000000000..814761c635 --- /dev/null +++ b/docs/resources/fgs_function_topping.md @@ -0,0 +1,37 @@ +--- +subcategory: "FunctionGraph" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_fgs_function_topping" +description: |- + Using this function to top function within HuaweiCloud. +--- + +# huaweicloud_fgs_function_topping + +Using this function to top function within HuaweiCloud. + +## Example Usage + +### Topping function + +```hcl +variable "function_urn" {} + +resource "huaweicloud_fgs_function_topping" "test" { + function_urn = var.function_urn +} +``` + +## Argument Reference + +* `region` - (Optional, String, ForceNew) Specifies the region where the function is located. + If omitted, the provider-level region will be used. Changing this parameter will create a new resource. + +* `function_urn` - (Required, String, ForceNew) Specifies the URN of the function to be topped. + Changing this parameter will create a new resource. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The resource ID in UUID format. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 222f3d035d..fa63ed3c83 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -1782,6 +1782,7 @@ func Provider() *schema.Provider { "huaweicloud_fgs_dependency_version": fgs.ResourceDependencyVersion(), "huaweicloud_fgs_function": fgs.ResourceFgsFunctionV2(), "huaweicloud_fgs_function_event": fgs.ResourceFunctionEvent(), + "huaweicloud_fgs_function_topping": fgs.ResourceFunctionTopping(), "huaweicloud_fgs_function_trigger": fgs.ResourceFunctionTrigger(), "huaweicloud_ga_accelerator": ga.ResourceAccelerator(), diff --git a/huaweicloud/services/acceptance/fgs/resource_huaweicloud_fgs_function_topping_test.go b/huaweicloud/services/acceptance/fgs/resource_huaweicloud_fgs_function_topping_test.go new file mode 100644 index 0000000000..e7ea399522 --- /dev/null +++ b/huaweicloud/services/acceptance/fgs/resource_huaweicloud_fgs_function_topping_test.go @@ -0,0 +1,72 @@ +package fgs + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +// lintignore:AT001 +func TestAccFunctionTopping_basic(t *testing.T) { + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckFgsAgency(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccFunctionTopping_basic(), + }, + }, + }) +} + +func testAccFunctionTopping_basic() string { + name := acceptance.RandomAccResourceName() + + return fmt.Sprintf(` +variable "js_script_content" { + default = < { + const result = + { + 'repsonse_code': 200, + 'headers': + { + 'Content-Type': 'application/json' + }, + 'isBase64Encoded': false, + 'body': JSON.stringify(event) + } + return result +} +EOT +} + +resource "huaweicloud_fgs_function" "test" { + count = 3 + + name = format("%[1]s_%%d", count.index) + app = "default" + agency = "%[2]s" + handler = "index.handler" + memory_size = 128 + timeout = 3 + code_type = "inline" + runtime = "Node.js12.13" + func_code = base64encode(jsonencode(var.js_script_content)) +} + +resource "huaweicloud_fgs_function_topping" "test" { + depends_on = [huaweicloud_fgs_function.test] + + count = 3 + + function_urn = huaweicloud_fgs_function.test[count.index].urn +} +`, name, acceptance.HW_FGS_AGENCY_NAME) +} diff --git a/huaweicloud/services/fgs/resource_huaweicloud_fgs_function_topping.go b/huaweicloud/services/fgs/resource_huaweicloud_fgs_function_topping.go new file mode 100644 index 0000000000..de7c6f01ec --- /dev/null +++ b/huaweicloud/services/fgs/resource_huaweicloud_fgs_function_topping.go @@ -0,0 +1,113 @@ +package fgs + +import ( + "context" + "fmt" + "strings" + + "github.com/hashicorp/go-uuid" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/chnsz/golangsdk" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" +) + +type ToppingStatus string + +var ( + ToppingStatusEnable ToppingStatus = "true" + ToppingStatusDisable ToppingStatus = "false" +) + +// @API FunctionGraph PUT /v2/{project_id}/fgs/functions/{func_urn}/collect/{state} +func ResourceFunctionTopping() *schema.Resource { + return &schema.Resource{ + CreateContext: resourceFunctionToppingCreate, + ReadContext: resourceFunctionToppingRead, + DeleteContext: resourceFunctionToppingDelete, + + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + Description: `The region where the function is located.`, + }, + "function_urn": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `The URN of the function to be topped.`, + }, + }, + } +} + +func updateFunctionToppingStatus(client *golangsdk.ServiceClient, functionUrn, status string) error { + httpUrl := "v2/{project_id}/fgs/functions/{func_urn}/collect/{state}" + updatePath := client.Endpoint + httpUrl + updatePath = strings.ReplaceAll(updatePath, "{project_id}", client.ProjectID) + updatePath = strings.ReplaceAll(updatePath, "{func_urn}", functionUrn) + updatePath = strings.ReplaceAll(updatePath, "{state}", status) + + updateOpts := golangsdk.RequestOpts{ + KeepResponseBody: true, + MoreHeaders: map[string]string{ + "Content-Type": "application/json", + }, + } + _, err := client.Request("PUT", updatePath, &updateOpts) + if err != nil { + return fmt.Errorf("error updating function topping status (target is: %s): %s", status, err) + } + return nil +} + +func resourceFunctionToppingCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var ( + cfg = meta.(*config.Config) + region = cfg.GetRegion(d) + functionUrn = d.Get("function_urn").(string) + ) + + client, err := cfg.NewServiceClient("fgs", region) + if err != nil { + return diag.Errorf("error creating FunctionGraph client: %s", err) + } + + err = updateFunctionToppingStatus(client, functionUrn, string(ToppingStatusEnable)) + if err != nil { + return diag.FromErr(err) + } + + randUUID, err := uuid.GenerateUUID() + if err != nil { + return diag.Errorf("unable to generate ID: %s", err) + } + d.SetId(randUUID) + + return resourceFunctionToppingRead(ctx, d, meta) +} + +func resourceFunctionToppingRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { + return nil +} + +func resourceFunctionToppingDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var ( + cfg = meta.(*config.Config) + region = cfg.GetRegion(d) + functionUrn = d.Get("function_urn").(string) + ) + + client, err := cfg.NewServiceClient("fgs", region) + if err != nil { + return diag.Errorf("error creating FunctionGraph client: %s", err) + } + + err = updateFunctionToppingStatus(client, functionUrn, string(ToppingStatusDisable)) + return diag.FromErr(err) +}