Skip to content

Commit

Permalink
feat(fgs/function): add new resource for function topping
Browse files Browse the repository at this point in the history
  • Loading branch information
Lance52259 committed Jan 20, 2025
1 parent 2c86918 commit 6967e6e
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/resources/fgs_function_topping.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = <<EOT
exports.handler = async (event, context) => {
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)
}
113 changes: 113 additions & 0 deletions huaweicloud/services/fgs/resource_huaweicloud_fgs_function_topping.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 6967e6e

Please sign in to comment.