-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fgs/function): add new resource for function topping
- Loading branch information
1 parent
2c86918
commit 6967e6e
Showing
4 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
huaweicloud/services/acceptance/fgs/resource_huaweicloud_fgs_function_topping_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
113
huaweicloud/services/fgs/resource_huaweicloud_fgs_function_topping.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |