From b65c8588842f02c54b52bcfb4fed9d20f5ff5254 Mon Sep 17 00:00:00 2001 From: hellertang Date: Fri, 26 May 2023 20:53:49 +0800 Subject: [PATCH 1/3] update dc --- tencentcloud/provider.go | 1 + ...urce_tc_scf_reserved_concurrency_config.go | 172 ++++++++++++++++++ ...tc_scf_reserved_concurrency_config_test.go | 38 ++++ tencentcloud/service_tencentcloud_scf.go | 51 ++++++ 4 files changed, 262 insertions(+) create mode 100644 tencentcloud/resource_tc_scf_reserved_concurrency_config.go create mode 100644 tencentcloud/resource_tc_scf_reserved_concurrency_config_test.go diff --git a/tencentcloud/provider.go b/tencentcloud/provider.go index b50bb7fe0e..559a37391a 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -1982,6 +1982,7 @@ func Provider() *schema.Provider { "tencentcloud_scf_function": resourceTencentCloudScfFunction(), "tencentcloud_scf_function_version": resourceTencentCloudScfFunctionVersion(), "tencentcloud_scf_function_event_invoke_config": resourceTencentCloudScfFunctionEventInvokeConfig(), + "tencentcloud_scf_reserved_concurrency_config": resourceTencentCloudScfReservedConcurrencyConfig(), "tencentcloud_scf_namespace": resourceTencentCloudScfNamespace(), "tencentcloud_scf_layer": resourceTencentCloudScfLayer(), "tencentcloud_scf_function_alias": resourceTencentCloudScfFunctionAlias(), diff --git a/tencentcloud/resource_tc_scf_reserved_concurrency_config.go b/tencentcloud/resource_tc_scf_reserved_concurrency_config.go new file mode 100644 index 0000000000..27f5bdaeee --- /dev/null +++ b/tencentcloud/resource_tc_scf_reserved_concurrency_config.go @@ -0,0 +1,172 @@ +/* +Provides a resource to create a scf reserved_concurrency_config + +Example Usage + +```hcl +resource "tencentcloud_scf_reserved_concurrency_config" "reserved_concurrency_config" { + function_name = "test_function" + reserved_concurrency_mem = 128000 + namespace = "test_namespace" +} +``` + +Import + +scf reserved_concurrency_config can be imported using the id, e.g. + +``` +terraform import tencentcloud_scf_reserved_concurrency_config.reserved_concurrency_config reserved_concurrency_config_id +``` +*/ +package tencentcloud + +import ( + "context" + "fmt" + "log" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + scf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf/v20180416" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func resourceTencentCloudScfReservedConcurrencyConfig() *schema.Resource { + return &schema.Resource{ + Create: resourceTencentCloudScfReservedConcurrencyConfigCreate, + Read: resourceTencentCloudScfReservedConcurrencyConfigRead, + Delete: resourceTencentCloudScfReservedConcurrencyConfigDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "function_name": { + Required: true, + ForceNew: true, + Type: schema.TypeString, + Description: "Specifies the function of which you want to configure the reserved quota.", + }, + + "reserved_concurrency_mem": { + Required: true, + ForceNew: true, + Type: schema.TypeInt, + Description: "Reserved memory quota of the function. Note: the upper limit for the total reserved quota of the function is the user's total concurrency memory minus 12800.", + }, + + "namespace": { + Optional: true, + ForceNew: true, + Default: "default", + Type: schema.TypeString, + Description: "Function namespace. Default value: default.", + }, + }, + } +} + +func resourceTencentCloudScfReservedConcurrencyConfigCreate(d *schema.ResourceData, meta interface{}) error { + defer logElapsed("resource.tencentcloud_scf_reserved_concurrency_config.create")() + defer inconsistentCheck(d, meta)() + + logId := getLogId(contextNil) + + var ( + request = scf.NewPutReservedConcurrencyConfigRequest() + namespace string + functionName string + ) + if v, ok := d.GetOk("function_name"); ok { + functionName = v.(string) + request.FunctionName = helper.String(v.(string)) + } + + if v, ok := d.GetOkExists("reserved_concurrency_mem"); ok { + request.ReservedConcurrencyMem = helper.IntUint64(v.(int)) + } + + if v, ok := d.GetOk("namespace"); ok { + namespace = v.(string) + request.Namespace = helper.String(v.(string)) + } + + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { + result, e := meta.(*TencentCloudClient).apiV3Conn.UseScfClient().PutReservedConcurrencyConfig(request) + if e != nil { + return retryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s create scf ReservedConcurrencyConfig failed, reason:%+v", logId, err) + return err + } + + d.SetId(namespace + FILED_SP + functionName) + + return resourceTencentCloudScfReservedConcurrencyConfigRead(d, meta) +} + +func resourceTencentCloudScfReservedConcurrencyConfigRead(d *schema.ResourceData, meta interface{}) error { + defer logElapsed("resource.tencentcloud_scf_reserved_concurrency_config.read")() + defer inconsistentCheck(d, meta)() + + logId := getLogId(contextNil) + + ctx := context.WithValue(context.TODO(), logIdKey, logId) + + service := ScfService{client: meta.(*TencentCloudClient).apiV3Conn} + + idSplit := strings.Split(d.Id(), FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + namespace := idSplit[0] + functionName := idSplit[1] + + reservedConcurrencyConfig, err := service.DescribeScfReservedConcurrencyConfigById(ctx, namespace, functionName) + if err != nil { + return err + } + + if reservedConcurrencyConfig == nil { + d.SetId("") + log.Printf("[WARN]%s resource `ScfReservedConcurrencyConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) + return nil + } + + _ = d.Set("namespace", namespace) + _ = d.Set("function_name", functionName) + + if reservedConcurrencyConfig.Response.ReservedMem != nil { + _ = d.Set("reserved_concurrency_mem", reservedConcurrencyConfig.Response.ReservedMem) + } + + return nil +} + +func resourceTencentCloudScfReservedConcurrencyConfigDelete(d *schema.ResourceData, meta interface{}) error { + defer logElapsed("resource.tencentcloud_scf_reserved_concurrency_config.delete")() + defer inconsistentCheck(d, meta)() + + logId := getLogId(contextNil) + ctx := context.WithValue(context.TODO(), logIdKey, logId) + + service := ScfService{client: meta.(*TencentCloudClient).apiV3Conn} + idSplit := strings.Split(d.Id(), FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + namespace := idSplit[0] + functionName := idSplit[1] + + if err := service.DeleteScfReservedConcurrencyConfigById(ctx, namespace, functionName); err != nil { + return err + } + + return nil +} diff --git a/tencentcloud/resource_tc_scf_reserved_concurrency_config_test.go b/tencentcloud/resource_tc_scf_reserved_concurrency_config_test.go new file mode 100644 index 0000000000..1deae74846 --- /dev/null +++ b/tencentcloud/resource_tc_scf_reserved_concurrency_config_test.go @@ -0,0 +1,38 @@ +package tencentcloud + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccTencentCloudScfReservedConcurrencyConfigResource_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccScfReservedConcurrencyConfig, + Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_scf_reserved_concurrency_config.reserved_concurrency_config", "id")), + }, + { + ResourceName: "tencentcloud_scf_reserved_concurrency_config.reserved_concurrency_config", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +const testAccScfReservedConcurrencyConfig = ` + +resource "tencentcloud_scf_reserved_concurrency_config" "reserved_concurrency_config" { + function_name = "test_function" + reserved_concurrency_mem = 128000 + namespace = "test_namespace" +} + +` diff --git a/tencentcloud/service_tencentcloud_scf.go b/tencentcloud/service_tencentcloud_scf.go index c7442aa365..2bb51e48e6 100644 --- a/tencentcloud/service_tencentcloud_scf.go +++ b/tencentcloud/service_tencentcloud_scf.go @@ -738,3 +738,54 @@ func (me *ScfService) DescribeScfFunctionEventInvokeConfigById(ctx context.Conte FunctionEventInvokeConfig = response.Response.AsyncTriggerConfig return } + +func (me *ScfService) DescribeScfReservedConcurrencyConfigById(ctx context.Context, namespace string, functionName string) (reservedConcurrencyConfig *scf.GetReservedConcurrencyConfigResponse, errRet error) { + logId := getLogId(ctx) + + request := scf.NewGetReservedConcurrencyConfigRequest() + request.Namespace = &namespace + request.FunctionName = &functionName + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + response, err := me.client.UseScfClient().GetReservedConcurrencyConfig(request) + if err != nil { + errRet = err + return + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + reservedConcurrencyConfig = response + return +} + +func (me *ScfService) DeleteScfReservedConcurrencyConfigById(ctx context.Context, namespace string, functionName string) (errRet error) { + logId := getLogId(ctx) + + request := scf.NewDeleteReservedConcurrencyConfigRequest() + request.Namespace = &namespace + request.FunctionName = &functionName + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + response, err := me.client.UseScfClient().DeleteReservedConcurrencyConfig(request) + if err != nil { + errRet = err + return + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + return +} From e2a9722eeefbe689b130047256591dc3be20c039 Mon Sep 17 00:00:00 2001 From: hellertang Date: Mon, 29 May 2023 09:03:29 +0800 Subject: [PATCH 2/3] update doc --- tencentcloud/provider.go | 1 + tencentcloud/resource_tc_scf_reserved_concurrency_config.go | 4 ++-- .../resource_tc_scf_reserved_concurrency_config_test.go | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tencentcloud/provider.go b/tencentcloud/provider.go index 559a37391a..d12281bdfe 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -637,6 +637,7 @@ Serverless Cloud Function(SCF) tencentcloud_scf_function tencentcloud_scf_function_version tencentcloud_scf_function_event_invoke_config + tencentcloud_scf_reserved_concurrency_config tencentcloud_scf_namespace tencentcloud_scf_layer tencentcloud_scf_function_alias diff --git a/tencentcloud/resource_tc_scf_reserved_concurrency_config.go b/tencentcloud/resource_tc_scf_reserved_concurrency_config.go index 27f5bdaeee..10cdbb484d 100644 --- a/tencentcloud/resource_tc_scf_reserved_concurrency_config.go +++ b/tencentcloud/resource_tc_scf_reserved_concurrency_config.go @@ -5,9 +5,9 @@ Example Usage ```hcl resource "tencentcloud_scf_reserved_concurrency_config" "reserved_concurrency_config" { - function_name = "test_function" + function_name = "keep-1676351130" reserved_concurrency_mem = 128000 - namespace = "test_namespace" + namespace = "default" } ``` diff --git a/tencentcloud/resource_tc_scf_reserved_concurrency_config_test.go b/tencentcloud/resource_tc_scf_reserved_concurrency_config_test.go index 1deae74846..9441b146a8 100644 --- a/tencentcloud/resource_tc_scf_reserved_concurrency_config_test.go +++ b/tencentcloud/resource_tc_scf_reserved_concurrency_config_test.go @@ -30,9 +30,9 @@ func TestAccTencentCloudScfReservedConcurrencyConfigResource_basic(t *testing.T) const testAccScfReservedConcurrencyConfig = ` resource "tencentcloud_scf_reserved_concurrency_config" "reserved_concurrency_config" { - function_name = "test_function" + function_name = "keep-1676351130" reserved_concurrency_mem = 128000 - namespace = "test_namespace" + namespace = "default" } ` From 5fc7387248c57ad7165a1a50020f56e4a67cb86f Mon Sep 17 00:00:00 2001 From: hellertang Date: Mon, 29 May 2023 09:04:27 +0800 Subject: [PATCH 3/3] update doc --- ..._reserved_concurrency_config.html.markdown | 47 +++++++++++++++++++ website/tencentcloud.erb | 3 ++ 2 files changed, 50 insertions(+) create mode 100644 website/docs/r/scf_reserved_concurrency_config.html.markdown diff --git a/website/docs/r/scf_reserved_concurrency_config.html.markdown b/website/docs/r/scf_reserved_concurrency_config.html.markdown new file mode 100644 index 0000000000..1ac2c09ddb --- /dev/null +++ b/website/docs/r/scf_reserved_concurrency_config.html.markdown @@ -0,0 +1,47 @@ +--- +subcategory: "Serverless Cloud Function(SCF)" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_scf_reserved_concurrency_config" +sidebar_current: "docs-tencentcloud-resource-scf_reserved_concurrency_config" +description: |- + Provides a resource to create a scf reserved_concurrency_config +--- + +# tencentcloud_scf_reserved_concurrency_config + +Provides a resource to create a scf reserved_concurrency_config + +## Example Usage + +```hcl +resource "tencentcloud_scf_reserved_concurrency_config" "reserved_concurrency_config" { + function_name = "keep-1676351130" + reserved_concurrency_mem = 128000 + namespace = "default" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `function_name` - (Required, String, ForceNew) Specifies the function of which you want to configure the reserved quota. +* `reserved_concurrency_mem` - (Required, Int, ForceNew) Reserved memory quota of the function. Note: the upper limit for the total reserved quota of the function is the user's total concurrency memory minus 12800. +* `namespace` - (Optional, String, ForceNew) Function namespace. Default value: default. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - ID of the resource. + + + +## Import + +scf reserved_concurrency_config can be imported using the id, e.g. + +``` +terraform import tencentcloud_scf_reserved_concurrency_config.reserved_concurrency_config reserved_concurrency_config_id +``` + diff --git a/website/tencentcloud.erb b/website/tencentcloud.erb index edea0ad4cd..06fda89efe 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -2070,6 +2070,9 @@
  • tencentcloud_scf_namespace
  • +
  • + tencentcloud_scf_reserved_concurrency_config +